You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
49 lines
1.3 KiB
use std::fs::File;
|
|
use std::io::{self, BufRead, Write};
|
|
use std::path::Path;
|
|
use std::string::String;
|
|
|
|
fn read_lines<P>(path: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
|
where P: AsRef<Path>, {
|
|
let file = File::open(path)?;
|
|
Ok(io::BufReader::new(file).lines())
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Playlist {
|
|
path: String,
|
|
tracks: Box<Vec<String>>
|
|
}
|
|
|
|
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 mut playlist_tracks: Vec<String> = Vec::new();
|
|
let lines = read_lines(path)?;
|
|
lines.for_each(|read_line| {
|
|
if read_line.is_ok() {
|
|
let line = read_line.unwrap();
|
|
if line.len() > 0 && !line.starts_with("#") {
|
|
playlist_tracks.push(line)
|
|
}
|
|
}
|
|
});
|
|
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(())
|
|
}
|
|
}
|