diff --git a/src/command/dedupe.rs b/src/command/dedupe.rs
index a3ff954..0744628 100644
--- a/src/command/dedupe.rs
+++ b/src/command/dedupe.rs
@@ -21,6 +21,7 @@ pub fn get_command<'a>() -> Command<'a, str>{
for path in playlists {
let playlist = Playlist::read(path)?;
println!("{:?}", playlist);
+ playlist.write(path)?
}
Ok(())
},
diff --git a/src/lib/m3u.rs b/src/lib/m3u.rs
index 429cf31..d21fcb6 100644
--- a/src/lib/m3u.rs
+++ b/src/lib/m3u.rs
@@ -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
(filename: P) -> io::Result>>
+fn read_lines(path: P) -> io::Result>>
where P: AsRef, {
- 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) -> Playlist {
+ Playlist {
+ path: String::from(path),
+ tracks: Box::new(tracks)
+ }
+ }
+
pub fn read(path: &str) -> Result {
- let playlist_path: String = String::from(path);
let mut playlist_tracks: Vec = 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(())
}
}
\ No newline at end of file