Playlist implémentée testée
This commit is contained in:
16
src/main.rs
16
src/main.rs
@ -1,10 +1,18 @@
|
||||
pub mod songmeta;
|
||||
use crate::songmeta::songmeta::*;
|
||||
|
||||
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"));
|
||||
dbg!(mysong);
|
||||
let mysong2 = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/Folk/Galaverna - Dodsdans/Galaverna - Dodsdans - 01 Dods....flac"));
|
||||
|
||||
|
||||
//playlist.remove(0).unwrap();
|
||||
let playlist = Playlist::from_file(&String::from("./cool.yml"));
|
||||
|
||||
dbg!(playlist);
|
||||
}
|
||||
|
||||
|
||||
let mysong2 = SongMeta::frompath(&String::from("/home/justine/NAS/Musique/Folk/Galaverna - Dodsdans/Galaverna - Dodsdans - 07 Smell of ember.flac"));
|
||||
dbg!(mysong2);
|
||||
}
|
||||
130
src/playlist.rs
130
src/playlist.rs
@ -1,31 +1,117 @@
|
||||
pub mod songmeta;
|
||||
use crate::songmeta::songmeta::*;
|
||||
pub mod playlist {
|
||||
|
||||
///Stores a list of SongMetas
|
||||
#[derive(Debug, Clone)]
|
||||
struct Playlist {
|
||||
songs: Vec<SongMeta>,
|
||||
}
|
||||
use std::path::{Path,PathBuf};
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::fs;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::songmeta::songmeta::*;
|
||||
use std::io::Write;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum PlaylistError {
|
||||
IndexNotFound,
|
||||
SomethingElse,
|
||||
}
|
||||
|
||||
impl Playlist {
|
||||
///Returns a new empty playlist.
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
songs: vec![],
|
||||
|
||||
|
||||
//----------------ERROR
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum PlaylistError {
|
||||
IndexNotFound,
|
||||
FileNotFound,
|
||||
FileCantWrite,
|
||||
}
|
||||
|
||||
impl fmt::Display for PlaylistError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
PlaylistError::IndexNotFound => write!(f, "Playlist does not have that index"),
|
||||
PlaylistError::FileNotFound => write!(f, "Given playlist file is not readable or does not exist"),
|
||||
PlaylistError::FileCantWrite => write!(f, "Can't write playlist file at the given path. Is it valid ?"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///Returns a playlist from a file containing a list of audiofile paths.
|
||||
fn from_file(path: &Path) -> Self;
|
||||
|
||||
///Removes a song at index (if possible)
|
||||
fn pop() -> Result<(), PlayListError>;
|
||||
impl Error for PlaylistError {}
|
||||
|
||||
|
||||
//----------------STRUCT
|
||||
|
||||
///Stores a list of SongMetas to be used with a player.
|
||||
///Index starts at zero !
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Playlist {
|
||||
pub songs: Vec<SongMeta>,
|
||||
}
|
||||
|
||||
impl Playlist {
|
||||
///Returns a new empty playlist.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
songs: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
///Returns a playlist from a file.
|
||||
///File must be yaml
|
||||
pub fn from_file(path: &String) -> Result<Self, Box<dyn Error>> {
|
||||
|
||||
let p = Path::new(path);
|
||||
|
||||
//Check existence
|
||||
if !p.exists() {
|
||||
return Err(Box::new(PlaylistError::FileNotFound));
|
||||
}
|
||||
|
||||
//Read the yaml
|
||||
let content = fs::read_to_string(p)?;
|
||||
let res: Playlist = serde_yaml::from_str(&content)?;
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
///Writes the playlist with its index to a file with the path p
|
||||
///Path must be fully qualified
|
||||
pub fn to_file(&self, path: &String) -> Result<(), Box<dyn Error>>{
|
||||
let p = Path::new(&path);
|
||||
|
||||
//Check parent
|
||||
let mut parent = PathBuf::from(path);
|
||||
let _ = &parent.pop();
|
||||
if !&parent.exists() {
|
||||
return Err(Box::new(PlaylistError::FileCantWrite));
|
||||
}
|
||||
|
||||
//Serialize and write
|
||||
let serialized = serde_yaml::to_string(&self.clone())?;
|
||||
let mut file = fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.append(false)
|
||||
.create(true)
|
||||
.open(&p)?;
|
||||
file.write_all(format!("---\n{}", serialized).as_bytes())?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
///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)?;
|
||||
self.songs.push(song);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
///Checks if a number is a valid index in this playlist.
|
||||
pub fn check_index(&self, index: usize) -> bool {
|
||||
if index > self.songs.len() {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
///Removes a song at index (if possible)
|
||||
pub fn remove(&mut self, index: usize) -> Result<(), Box<dyn Error>> {
|
||||
if !self.check_index(index.clone()) {
|
||||
return Err(Box::new(PlaylistError::IndexNotFound));
|
||||
}
|
||||
self.songs.remove(index);
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
35
src/proto.rs
35
src/proto.rs
@ -8,11 +8,7 @@ use rodio::{Decoder, OutputStream, Sink, Source};
|
||||
//-----------------------------------------Structs
|
||||
|
||||
|
||||
///Stores a list of SongMetas
|
||||
#[derive(Debug, Clone)]
|
||||
struct Playlist {
|
||||
songs: Vec<SongMeta>,
|
||||
}
|
||||
|
||||
|
||||
struct AudioPlayer {
|
||||
sink: Sink,
|
||||
@ -27,33 +23,10 @@ enum PlayerError {
|
||||
SomethingElse,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum PlaylistError {
|
||||
IndexNotFound,
|
||||
SomethingElse,
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------Impls
|
||||
|
||||
|
||||
impl Playlist {
|
||||
///Returns a new empty playlist.
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
songs: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
///Returns a playlist from a file containing a list of audiofile paths.
|
||||
fn from_file(path: &Path) -> Self;
|
||||
|
||||
///Removes a song at index (if possible)
|
||||
fn pop() -> Result<(), PlayListError>;
|
||||
|
||||
|
||||
}
|
||||
|
||||
impl AudioPlayer {
|
||||
|
||||
@ -87,11 +60,17 @@ impl AudioPlayer {
|
||||
self.playlist.append(&mut playlist);
|
||||
}
|
||||
|
||||
//renvoie un result, avance d'une chanson
|
||||
fn skip_forward(&mut self);
|
||||
//renvoie un result, recule d'une chanson
|
||||
fn skip_backwards(&mut self);
|
||||
//met en pause renvoie un result
|
||||
fn pause(&mut self);
|
||||
//Enleve la pause ou lit la première chanson de la playlist renvoie un result
|
||||
fn play(&mut self);
|
||||
//Change le volume envoie un result
|
||||
fn set_vol(&mut self, vol: f32);
|
||||
//Saute à la chanson donnée renvoie un result
|
||||
fn goto(&mut self, index: usize) -> Result<(), PlayerError>;
|
||||
}
|
||||
|
||||
|
||||
@ -7,19 +7,12 @@ pub mod songmeta {
|
||||
use std::ffi::OsStr;
|
||||
use metadata::media_file::MediaFileMetadata;
|
||||
use std::time::Duration;
|
||||
use file_format::FileFormat;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::Write;
|
||||
|
||||
///Stores metadata about a song.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SongMeta {
|
||||
pub title: String,
|
||||
pub artist: String,
|
||||
pub album: String,
|
||||
pub track: usize,
|
||||
pub duration: Duration,
|
||||
pub cover_path: Option<String>,
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
//----------------------------ERROR
|
||||
#[derive(Debug)]
|
||||
pub enum SongError {
|
||||
///Given path is not existing or is not a media file.
|
||||
@ -39,6 +32,20 @@ pub mod songmeta {
|
||||
|
||||
impl Error for SongError {}
|
||||
|
||||
//----------------------------STRUCT
|
||||
|
||||
///Stores metadata about a song.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SongMeta {
|
||||
pub title: String,
|
||||
pub artist: String,
|
||||
pub album: String,
|
||||
pub track: usize,
|
||||
pub duration: Duration,
|
||||
pub cover_path: Option<String>,
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
|
||||
impl SongMeta {
|
||||
|
||||
@ -46,6 +53,10 @@ pub mod songmeta {
|
||||
///Returns an error if need be.
|
||||
///Also looks for the path of the cover, which may be None.
|
||||
pub fn frompath(path: &String) -> Result<Self, Box<dyn Error>> {
|
||||
if !SongMeta::validate_file(path) {
|
||||
return Err(Box::new(SongError::NotAudio));
|
||||
}
|
||||
|
||||
let fpath = Path::new(path);
|
||||
let md = match MediaFileMetadata::new(&fpath) {
|
||||
Ok(v) => v,
|
||||
@ -142,5 +153,30 @@ pub mod songmeta {
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
///Validates that the file at path is audio.
|
||||
fn validate_file(path: &String) -> bool {
|
||||
let valid_formats = vec![
|
||||
"audio/mpeg",
|
||||
"audio/x-flac",
|
||||
"audio/ogg",
|
||||
"audio/vnd.wave",
|
||||
"audio/aac",
|
||||
];
|
||||
|
||||
let fmt = match FileFormat::from_file(path.as_str()) {
|
||||
Ok(v) => v,
|
||||
Err(_) => {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
if valid_formats.contains(&fmt.media_type()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user