Random + shuffle sur la playlist implémenté et testé et update proto

This commit is contained in:
Justine
2024-08-11 18:56:53 +02:00
parent 283d0b2065
commit 49eb71b326
5 changed files with 118 additions and 6 deletions

View File

@ -7,17 +7,21 @@ 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 playlist = Playlist::from_file(&String::from("./cool.yml"));
let playlist = Playlist {
songs: vec![mysong, mysong2],
let mut playlist = Playlist {
songs: vec![mysong, mysong2, mysong3],
};
assert_eq!(false, playlist.check_index(2));
let playlist = Playlist::new();
assert_eq!(false, playlist.check_index(0));
assert_eq!(false, playlist.check_index(10));
println!("RAND {:#?}", playlist);
playlist.sort();
println!("SORT {:#?}", playlist);
playlist.shuffle();
println!("RAND {:#?}", playlist);
playlist.sort();
println!("SORT{:#?}", playlist);
}

View File

@ -7,6 +7,8 @@ pub mod playlist {
use serde::{Deserialize, Serialize};
use crate::songmeta::songmeta::*;
use std::io::Write;
use rand::thread_rng;
use rand::prelude::SliceRandom;
@ -116,5 +118,25 @@ pub mod playlist {
self.songs.remove(index);
return Ok(());
}
///Shuffles the playlist
pub fn shuffle(&mut self) {
//Return if playlist is empty
if self.songs.len() == 0 {
return;
}
self.songs.shuffle(&mut thread_rng());
}
///Sorts the playlist by artist, album, trackorder, title
pub fn sort(&mut self) {
self.songs.sort_by(|a, b| {
a.artist
.cmp(&b.artist)
.then_with(|| a.album.cmp(&b.album))
.then_with(|| a.track.cmp(&b.track))
.then_with(|| a.title.cmp(&b.title))
});
}
}
}

View File

@ -143,5 +143,12 @@ impl AudioPlayer {
//Stoppe la lecture en cours et vide le sink renvoie rien
fn stop(&mut self);
//Charge un fichier dans la playlist renvoie un result
fn load_file(&mut self);
//Charge un dossier entier dans la playlist renvoie un result (récursif ou pas je sais pas peut-être faire les deux)
fn load_folder(&mut self);
}