From c2591bc2a2b0f436f760fba6ad5ec5b6c55b3346 Mon Sep 17 00:00:00 2001 From: Justine Date: Mon, 12 Aug 2024 13:09:02 +0200 Subject: [PATCH] Added check functionnality to playlist loading, added docstrings --- src/main.rs | 20 +++++--------------- src/myplaylist.yml | 29 +++++++++++++++++++++++++++++ src/playlist.rs | 35 ++++++++++++++++++++++++++++++----- src/songmeta.rs | 5 ++++- 4 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 src/myplaylist.yml diff --git a/src/main.rs b/src/main.rs index b78a796..ca29c8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,23 +5,13 @@ pub mod playlist; use crate::playlist::playlist::*; fn main() { - let mysong = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/A_classer/Bleach/12 Big Cheese.mp3")).unwrap(); - let mysong2 = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/Folk/Galaverna - Dodsdans/Galaverna - Dodsdans - 01 Dods....flac")).unwrap(); - let mysong3 = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/Jeux/Superliminal/Matt Christensen - Superliminal/12. Astral.mp3")).unwrap(); + //let mysong = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/A_classer/Bleach/12 Big Cheese.mp3")).unwrap(); + //let mysong2 = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/Folk/Galaverna - Dodsdans/Galaverna - Dodsdans - 01 Dods....flac")).unwrap(); + //let mysong3 = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/Jeux/Superliminal/Matt Christensen - Superliminal/12. Astral.mp3")).unwrap(); - //let playlist = Playlist::from_file(&String::from("./cool.yml")); + let playlist = Playlist::from_file("./myplaylist.yml", true); - let mut playlist = Playlist { - songs: vec![mysong, mysong2, mysong3], - }; - - println!("RAND {:#?}", playlist); - playlist.sort(); - println!("SORT {:#?}", playlist); - playlist.shuffle(); - println!("RAND {:#?}", playlist); - playlist.sort(); - println!("SORT{:#?}", playlist); + dbg!(playlist); } diff --git a/src/myplaylist.yml b/src/myplaylist.yml new file mode 100644 index 0000000..e7ca2e4 --- /dev/null +++ b/src/myplaylist.yml @@ -0,0 +1,29 @@ +--- +songs: +- title: Dods... + artist: Galaverna + album: Dodsdans + track: 1 + duration: + secs: 148 + nanos: 0 + cover_path: /home/justine/NAS/Musique/Folk/Galaverna - Dodsdans/cover.jpg + path: /home/justine/NAS/Musique/Folk/Galaverna - Dodsdans/Galaverna - Dodsdans - 01 Dods....flac +- title: Astral + artist: Matt Christensen + album: Superliminal (Original Game Soundtrack) + track: 12 + duration: + secs: 311 + nanos: 0 + cover_path: /home/justine/NAS/Musique/Jeux/Superliminal/Matt Christensen - Superliminal/cover.jpg + path: /home/justine/NAS/Musique/Jeux/Superliminal/Matt Christensen - Superliminal/12. Astral.mp3 +- title: Big Cheese + artist: Nirvana + album: Bleach + track: 12 + duration: + secs: 221 + nanos: 0 + cover_path: /home/justine/NAS/Musique/A_classer/Bleach/cover.jpg + path: /home/justine/NAS/Musique/A_classer/Bleach/12 Big Cheese.mp3 diff --git a/src/playlist.rs b/src/playlist.rs index 214c6ab..b41e598 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -1,3 +1,9 @@ +///Module tied to the SongMeta module. +///Handles storing songmetas into a playlist that is ready to be used by a music player. +///Includes writing and reading from a yaml file for ease-of-use. +///Any functions of a music player that act on its playlist go in here, including sorting and +///shuffling. + pub mod playlist { use std::path::{Path,PathBuf}; @@ -54,7 +60,9 @@ pub mod playlist { ///Returns a playlist from a file. ///File must be yaml - pub fn from_file(path: &String) -> Result> { + ///If check is true, we remove from the playlist the files that are not present on the + ///filesystem. + pub fn from_file(path: &str, check: bool) -> Result> { let p = Path::new(path); @@ -65,13 +73,16 @@ pub mod playlist { //Read the yaml let content = fs::read_to_string(p)?; - let res: Playlist = serde_yaml::from_str(&content)?; + let mut res: Playlist = serde_yaml::from_str(&content)?; + + //Check ? + if check { res.check_sources(); } return Ok(res); } - ///Writes the playlist with its index to a file with the path p + ///Writes the playlist with its index to a file ///Path must be fully qualified - pub fn to_file(&self, path: &String) -> Result<(), Box>{ + pub fn to_file(&self, path: &str) -> Result<(), Box>{ let p = Path::new(&path); //Check parent @@ -92,6 +103,20 @@ pub mod playlist { return Ok(()); } + ///Checks the existence of every song in the playlist on the filesystem. + ///Removes the ones that do not exist anymore. + pub fn check_sources(&mut self) { + let mut recipient: Vec = Vec::new(); + for song in &self.songs { + let p = Path::new(&song.path); + if p.exists() { + recipient.push(song.clone()); + } + } + self.songs = recipient; + } + + ///shortcut to myplaylist.songs.push(SongMeta::frompath(&String::from("/a/file/path"))) pub fn add_song_from_path(&mut self, path: &String) -> Result<(), Box> { let song = SongMeta::frompath(path)?; @@ -139,4 +164,4 @@ pub mod playlist { }); } } -} \ No newline at end of file +} diff --git a/src/songmeta.rs b/src/songmeta.rs index 2d5fe27..a482cea 100644 --- a/src/songmeta.rs +++ b/src/songmeta.rs @@ -1,3 +1,5 @@ +///Module used to read audio files into SongMetas, which is a collection of data about the file. +///This is made primarily for music and stores informationa about artist, album, etc. pub mod songmeta { use std::path::{PathBuf, Path}; @@ -34,7 +36,8 @@ pub mod songmeta { //----------------------------STRUCT - ///Stores metadata about a song. + ///Stores metadata about a song, as well as the path of the file and eventually of the song's + ///album art (foudn automatically by searching for "cover.png" #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SongMeta { pub title: String,