Browse Source

Added dedupe functionality

master
Drew Short 5 years ago
parent
commit
082971395f
  1. 21
      src/command/dedupe.rs
  2. 10
      src/lib/m3u.rs

21
src/command/dedupe.rs

@ -1,7 +1,20 @@
use clap::{Arg, Error, ErrorKind};
use clap_nested::{Command};
use crate::lib::m3u::{Playlist};
use crate::lib::m3u::{Playlist, PlaylistTrack};
use std::collections::HashSet;
fn dedupe(playlist_tracks: Vec<PlaylistTrack>) -> Vec<PlaylistTrack> {
let mut seen_tracks: HashSet<String> = HashSet::new();
let mut dedupe_playlist_tracks = Vec::new();
for track in playlist_tracks {
if ! seen_tracks.contains(&track.track) {
seen_tracks.insert(String::from(&track.track));
dedupe_playlist_tracks.push(track);
}
}
dedupe_playlist_tracks
}
pub fn get_command<'a>() -> Command<'a, str>{
Command::new("dedupe")
@ -20,10 +33,12 @@ pub fn get_command<'a>() -> Command<'a, str>{
Some(playlists) => {
for path in playlists {
let playlist = Playlist::read(path)?;
let dedupe_playlist_tracks: Vec<PlaylistTrack> = dedupe(*playlist.tracks);
let dedupe_playlist = Playlist::new(&path, dedupe_playlist_tracks);
if ! dry_run {
playlist.write(path)?
dedupe_playlist.write(path)?
} else {
println!("{:?}", playlist);
println!("{:?}", dedupe_playlist);
}
}
Ok(())

10
src/lib/m3u.rs

@ -11,9 +11,9 @@ fn read_lines<P>(path: P) -> io::Result<io::Lines<io::BufReader<File>>>
#[derive(Debug)]
pub struct PlaylistTrack {
duration: String,
display: String,
track: String
pub duration: String,
pub display: String,
pub track: String
}
impl PlaylistTrack {
@ -32,8 +32,8 @@ impl PlaylistTrack {
#[derive(Debug)]
pub struct Playlist {
path: String,
tracks: Box<Vec<PlaylistTrack>>
pub path: String,
pub tracks: Box<Vec<PlaylistTrack>>
}
impl Playlist {

Loading…
Cancel
Save