From 4a7a6096d644f66e0673ed48a78ec85928f7e0ab Mon Sep 17 00:00:00 2001 From: MrDulfin Date: Fri, 9 Feb 2024 23:52:24 -0500 Subject: [PATCH] addded small changes --- src/config/config.rs | 2 +- src/music_storage/library.rs | 6 ++-- src/music_storage/playlist.rs | 61 +++++++++++++++++++++-------------- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/config/config.rs b/src/config/config.rs index e3f021f..9314d65 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -92,6 +92,7 @@ impl ConfigLibraries { #[derive(Debug, Default, Serialize, Deserialize, Clone)] pub struct Config { pub path: PathBuf, + pub backup_folder: Option, pub libraries: ConfigLibraries, pub volume: f32, } @@ -144,7 +145,6 @@ pub enum ConfigError { } - #[test] fn config_test() { let lib_a = ConfigLibrary::new(PathBuf::from("test-config/library1"), String::from("library1"), None); diff --git a/src/music_storage/library.rs b/src/music_storage/library.rs index 6b216e3..69acb27 100644 --- a/src/music_storage/library.rs +++ b/src/music_storage/library.rs @@ -6,6 +6,7 @@ use crate::config::config::Config; use std::collections::BTreeMap; use std::error::Error; use std::ops::ControlFlow::{Break, Continue}; +use std::ops::Deref; // Files use file_format::{FileFormat, Kind}; @@ -273,7 +274,7 @@ impl Song { /// creates a `Vec` from a cue file - pub fn from_cue(cuesheet: &Path) -> Result<(Vec<(Self, &PathBuf)>), Box> { + pub fn from_cue(cuesheet: &Path) -> Result<(Vec<(Self, PathBuf)>), Box> { let mut tracks = Vec::new(); let cue_data = parse_from_file(&cuesheet.to_string_lossy(), false).unwrap(); @@ -286,6 +287,7 @@ impl Song { for file in cue_data.files.iter() { let audio_location = &parent_dir.join(file.file.clone()); + if !audio_location.exists() { continue; } @@ -389,7 +391,7 @@ impl Song { tags, album_art, }; - tracks.push((new_song, audio_location)); + tracks.push((new_song, audio_location.clone())); } } Ok((tracks)) diff --git a/src/music_storage/playlist.rs b/src/music_storage/playlist.rs index c9ad352..c2c6b74 100644 --- a/src/music_storage/playlist.rs +++ b/src/music_storage/playlist.rs @@ -5,6 +5,8 @@ use chrono::Duration; use uuid::Uuid; // use walkdir::Error; +use crate::music_controller::controller::Controller; + use super::{ library::{AlbumArt, Song, Tag}, music_collection::MusicCollection, db_reader::{ @@ -15,12 +17,17 @@ use super::{ use m3u8_rs::{MediaPlaylist, MediaPlaylistType, MediaSegment, Playlist as List2, MasterPlaylist}; - +#[derive(Debug, Clone)] +pub enum SortOrder { + Manual, + Tag(Tag) +} #[derive(Debug, Clone)] pub struct Playlist<'a> { title: String, cover: Option<&'a AlbumArt>, tracks: Vec, + sort_order: SortOrder, play_count: i32, play_time: Duration, } @@ -35,10 +42,10 @@ impl<'a> Playlist<'a> { self.play_time } pub fn set_tracks(&mut self, tracks: Vec) { - self.tracks = songs; + self.tracks = tracks; } pub fn add_track(&mut self, track: Uuid) -> Result<(), Error> { - self.tracks.push(song); + self.tracks.push(track); Ok(()) } pub fn remove_track(&mut self, index: i32) -> Result<(), Error> { @@ -67,16 +74,18 @@ impl<'a> Playlist<'a> { }); false } - pub fn to_m3u8(&mut self) { - let seg = &self - .tracks + pub fn to_m3u8(&mut self, tracks: Vec) { + let seg = tracks .iter() .map({ - |track| MediaSegment { - uri: track.location.to_string().into(), - duration: track.duration.as_millis() as f32, - title: Some(track.tags.get_key_value(&Tag::Title).unwrap().1.into()), - ..Default::default() + |track| { + + MediaSegment { + uri: track.location.to_string().into(), + duration: track.duration.as_millis() as f32, + title: Some(track.tags.get_key_value(&Tag::Title).unwrap().1.into()), + ..Default::default() + } } }) .collect::>(); @@ -124,8 +133,6 @@ impl<'a> Playlist<'a> { todo!() } -} -impl MusicCollection for Playlist<'_> { fn title(&self) -> &String { &self.title } @@ -135,30 +142,34 @@ impl MusicCollection for Playlist<'_> { None => None, } } - fn tracks(&self) -> Vec { + fn tracks(&self) -> Vec { self.tracks.to_owned() } } + + + impl Default for Playlist<'_> { fn default() -> Self { Playlist { title: String::default(), cover: None, tracks: Vec::default(), + sort_order: SortOrder::Manual, play_count: 0, play_time: Duration::zero(), } } } -#[test] -fn list_to_m3u8() { - let lib = ITunesLibrary::from_file(Path::new( - "F:\\Music\\Mp3\\Music Main\\iTunes Music Library.xml", - )); - let mut a = Playlist::new(); - let c = lib.to_songs(); - let mut b = c.iter().map(|song| song.to_owned()).collect::>(); - a.tracks.append(&mut b); - a.to_m3u8() -} +// #[test] +// fn list_to_m3u8() { +// let lib = ITunesLibrary::from_file(Path::new( +// "F:\\Music\\Mp3\\Music Main\\iTunes Music Library.xml", +// )); +// let mut a = Playlist::new(); +// let c = lib.to_songs(); +// let mut b = c.iter().map(|song| song.to_owned()).collect::>(); +// a.tracks.append(&mut b); +// a.to_m3u8() +// }