Added check functionnality to playlist loading, added docstrings

This commit is contained in:
Justine 2024-08-12 13:09:02 +02:00
parent 49eb71b326
commit c2591bc2a2
4 changed files with 68 additions and 21 deletions

View File

@ -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);
}

29
src/myplaylist.yml Normal file
View File

@ -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

View File

@ -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<Self, Box<dyn Error>> {
///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<Self, Box<dyn Error>> {
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<dyn Error>>{
pub fn to_file(&self, path: &str) -> Result<(), Box<dyn Error>>{
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<SongMeta> = 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<dyn Error>> {
let song = SongMeta::frompath(path)?;
@ -139,4 +164,4 @@ pub mod playlist {
});
}
}
}
}

View File

@ -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,