From a60a57463df9d56a6d01ee0853928268a6228b70 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Tue, 3 Mar 2020 20:36:05 -0600 Subject: [PATCH] Added tooling for reading m3u playlists --- src/command/dedupe.rs | 7 +++++-- src/lib.rs | 0 src/lib/m3u.rs | 33 +++++++++++++++++++++++++++++++++ src/lib/mod.rs | 1 + src/main.rs | 1 + test.m3u | 7 +++++++ 6 files changed, 47 insertions(+), 2 deletions(-) delete mode 100644 src/lib.rs create mode 100644 src/lib/m3u.rs create mode 100644 src/lib/mod.rs create mode 100644 test.m3u diff --git a/src/command/dedupe.rs b/src/command/dedupe.rs index 8cbaa43..a3ff954 100644 --- a/src/command/dedupe.rs +++ b/src/command/dedupe.rs @@ -1,6 +1,8 @@ use clap::{Arg, Error, ErrorKind}; use clap_nested::{Command}; +use crate::lib::m3u::{Playlist}; + pub fn get_command<'a>() -> Command<'a, str>{ Command::new("dedupe") .description("dedupe a m3u playlist") @@ -16,8 +18,9 @@ pub fn get_command<'a>() -> Command<'a, str>{ // println!("Running dedupe, logging = {}", args); match matches.values_of("playlist") { Some(playlists) => { - for playlist in playlists { - println!("{}", playlist) + for path in playlists { + let playlist = Playlist::read(path)?; + println!("{:?}", playlist); } Ok(()) }, diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib/m3u.rs b/src/lib/m3u.rs new file mode 100644 index 0000000..429cf31 --- /dev/null +++ b/src/lib/m3u.rs @@ -0,0 +1,33 @@ +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; +use std::string::String; + +fn read_lines

(filename: P) -> io::Result>> + where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +#[derive(Debug)] +pub struct Playlist { + path: String, + tracks: Box> +} + +impl Playlist { + 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| { + if read_line.is_ok() { + let line = read_line.unwrap(); + if line.len() > 0 && !line.starts_with("#") { + playlist_tracks.push(line) + } + } + }); + Ok(Playlist { path: playlist_path, tracks: Box::new(playlist_tracks) }) + } +} \ No newline at end of file diff --git a/src/lib/mod.rs b/src/lib/mod.rs new file mode 100644 index 0000000..033f1ae --- /dev/null +++ b/src/lib/mod.rs @@ -0,0 +1 @@ +pub mod m3u; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 866f9b2..93a93fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use clap::{Arg}; use clap_nested::{Commander}; mod command; +mod lib; const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); diff --git a/test.m3u b/test.m3u new file mode 100644 index 0000000..84aab3b --- /dev/null +++ b/test.m3u @@ -0,0 +1,7 @@ +#EXTM3U + +#EXTINF:123, Sample artist - Sample title +C:\Documents and Settings\I\My Music\Sample.mp3 + +#EXTINF:321,Example Artist - Example title +C:\Documents and Settings\I\My Music\Greatest Hits\Example.ogg \ No newline at end of file