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.

50 lines
1.8 KiB

4 years ago
4 years ago
4 years ago
  1. use clap::{Arg, Error, ErrorKind};
  2. use clap_nested::{Command};
  3. use crate::lib::m3u::{Playlist, PlaylistTrack};
  4. use std::collections::HashSet;
  5. fn dedupe(playlist_tracks: Vec<PlaylistTrack>) -> Vec<PlaylistTrack> {
  6. let mut seen_tracks: HashSet<String> = HashSet::new();
  7. let mut dedupe_playlist_tracks = Vec::new();
  8. for track in playlist_tracks {
  9. if ! seen_tracks.contains(&track.track) {
  10. seen_tracks.insert(String::from(&track.track));
  11. dedupe_playlist_tracks.push(track);
  12. }
  13. }
  14. dedupe_playlist_tracks
  15. }
  16. pub fn get_command<'a>() -> Command<'a, str>{
  17. Command::new("dedupe")
  18. .description("dedupe a m3u playlist")
  19. .options(|app| {
  20. app.arg(Arg::with_name("playlist")
  21. .help("Path of the playlist(s) to de-duplicate")
  22. .index(1)
  23. .multiple(true)
  24. .required(true)
  25. )
  26. })
  27. .runner(| _args, matches| {
  28. let dry_run = matches.is_present("dry-run");
  29. match matches.values_of("playlist") {
  30. Some(playlists) => {
  31. for path in playlists {
  32. let playlist = Playlist::read(path)?;
  33. let dedupe_playlist_tracks: Vec<PlaylistTrack> = dedupe(*playlist.tracks);
  34. let dedupe_playlist = Playlist::new(&path, dedupe_playlist_tracks);
  35. if ! dry_run {
  36. dedupe_playlist.write(path)?
  37. } else {
  38. println!("{:?}", dedupe_playlist);
  39. }
  40. }
  41. Ok(())
  42. },
  43. None => Err(
  44. Error::with_description("playlist must be provided", ErrorKind::EmptyValue))
  45. }
  46. })
  47. }