|
|
@ -1,11 +1,11 @@ |
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, BufRead};
|
|
|
|
use std::io::{self, BufRead, Write};
|
|
|
|
use std::path::Path;
|
|
|
|
use std::string::String;
|
|
|
|
|
|
|
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
|
|
|
fn read_lines<P>(path: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
|
|
|
where P: AsRef<Path>, {
|
|
|
|
let file = File::open(filename)?;
|
|
|
|
let file = File::open(path)?;
|
|
|
|
Ok(io::BufReader::new(file).lines())
|
|
|
|
}
|
|
|
|
|
|
|
@ -16,8 +16,14 @@ pub struct Playlist { |
|
|
|
}
|
|
|
|
|
|
|
|
impl Playlist {
|
|
|
|
pub fn new(path: &str, tracks: Vec<String>) -> Playlist {
|
|
|
|
Playlist {
|
|
|
|
path: String::from(path),
|
|
|
|
tracks: Box::new(tracks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read(path: &str) -> Result<Playlist, io::Error> {
|
|
|
|
let playlist_path: String = String::from(path);
|
|
|
|
let mut playlist_tracks: Vec<String> = Vec::new();
|
|
|
|
let lines = read_lines(path)?;
|
|
|
|
lines.for_each(|read_line| {
|
|
|
@ -28,6 +34,16 @@ impl Playlist { |
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(Playlist { path: playlist_path, tracks: Box::new(playlist_tracks) })
|
|
|
|
Ok(Playlist::new(path, playlist_tracks))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(&self, path: &str) -> Result<(), io::Error> {
|
|
|
|
let mut file = File::create(path)?;
|
|
|
|
file.write("#EXTM3U\n".as_bytes())?;
|
|
|
|
for track in &*self.tracks {
|
|
|
|
file.write(track.as_bytes())?;
|
|
|
|
file.write("\n".as_bytes())?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|