Minor work starting on controller.rs

This commit is contained in:
G2-Games 2024-01-17 06:18:09 -06:00
parent 4e33d20d18
commit c14ab1f04b
5 changed files with 70 additions and 52 deletions

View file

@ -2,7 +2,7 @@ use std::{path::PathBuf, marker::PhantomData};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Config { pub struct Config {
db_path: Option<PathBuf>, pub db_path: Option<PathBuf>,
} }
impl Config { impl Config {
@ -13,4 +13,4 @@ impl Config {
pub fn load(&self) { pub fn load(&self) {
} }
} }

View file

@ -1 +1,24 @@
//! The [Controller] is the input and output for the entire
//! player. It manages queues, playback, library access, and
//! other functions
use crate::{
music_player::Player,
music_storage::library::Song,
config::config::Config
};
struct Queue {
player: Player,
name: String,
songs: Vec<Song>,
}
pub struct Controller {
queues: Vec<Queue>,
config: Config,
}
impl Controller {
// more stuff to come
}

View file

@ -1,6 +1,7 @@
use super::music_collection::MusicCollection;
// Crate things // Crate things
use super::utils::{find_images, normalize, read_library, write_library}; use super::utils::{find_images, normalize, read_library, write_library};
use super::music_collection::MusicCollection;
use crate::config::config::Config;
// Various std things // Various std things
use std::collections::BTreeMap; use std::collections::BTreeMap;
@ -269,6 +270,16 @@ pub struct Album<'a> {
#[allow(clippy::len_without_is_empty)] #[allow(clippy::len_without_is_empty)]
impl Album<'_> { impl Album<'_> {
//returns the Album title
fn title(&self) -> &String {
self.title
}
/// Returns the album cover as an AlbumArt struct, if it exists
fn cover(&self) -> Option<&AlbumArt> {
self.cover
}
/// Returns the Album Artist, if they exist /// Returns the Album Artist, if they exist
pub fn artist(&self) -> Option<&String> { pub fn artist(&self) -> Option<&String> {
self.artist self.artist
@ -283,6 +294,14 @@ impl Album<'_> {
Some(self.discs.get(&disc)?[index]) Some(self.discs.get(&disc)?[index])
} }
fn tracks(&self) -> Vec<&Song> {
let mut songs = Vec::new();
for disc in &self.discs {
songs.append(&mut disc.1.clone())
}
songs
}
/// Returns the number of songs in the album /// Returns the number of songs in the album
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
let mut total = 0; let mut total = 0;
@ -292,23 +311,6 @@ impl Album<'_> {
total total
} }
} }
impl MusicCollection for Album<'_> {
//returns the Album title
fn title(&self) -> &String {
self.title
}
/// Returns the album cover as an AlbumArt struct, if it exists
fn cover(&self) -> Option<&AlbumArt> {
self.cover
}
fn tracks(&self) -> Vec<&Song> {
let mut songs = Vec::new();
for disc in &self.discs {
songs.append(&mut disc.1.clone())
}
songs
}
}
const BLOCKED_EXTENSIONS: [&str; 4] = ["vob", "log", "txt", "sf2"]; const BLOCKED_EXTENSIONS: [&str; 4] = ["vob", "log", "txt", "sf2"];
@ -327,20 +329,20 @@ impl MusicLibrary {
let global_config = &*config.read().unwrap(); let global_config = &*config.read().unwrap();
let mut library: Vec<Song> = Vec::new(); let mut library: Vec<Song> = Vec::new();
let mut backup_path = global_config.db_path.clone(); let mut backup_path = global_config.db_path.clone();
backup_path.set_extension("bkp"); backup_path.unwrap().set_extension("bkp");
match global_config.db_path.exists() { match global_config.db_path.unwrap().exists() {
true => { true => {
library = read_library(global_config.db_path.clone())?; library = read_library(global_config.db_path.unwrap().clone())?;
} }
false => { false => {
// Create the database if it does not exist // Create the database if it does not exist
// possibly from the backup file // possibly from the backup file
if backup_path.exists() { if backup_path.unwrap().exists() {
library = read_library(backup_path.clone())?; library = read_library(backup_path.unwrap().clone())?;
write_library(&library, global_config.db_path.to_path_buf(), false)?; write_library(&library, global_config.db_path.unwrap().to_path_buf(), false)?;
} else { } else {
write_library(&library, global_config.db_path.to_path_buf(), false)?; write_library(&library, global_config.db_path.unwrap().to_path_buf(), false)?;
} }
} }
}; };
@ -350,9 +352,9 @@ impl MusicLibrary {
/// Serializes the database out to the file specified in the config /// Serializes the database out to the file specified in the config
pub fn save(&self, config: &Config) -> Result<(), Box<dyn Error>> { pub fn save(&self, config: &Config) -> Result<(), Box<dyn Error>> {
match config.db_path.try_exists() { match config.db_path.unwrap().try_exists() {
Ok(exists) => { Ok(exists) => {
write_library(&self.library, config.db_path.to_path_buf(), exists)?; write_library(&self.library, config.db_path.unwrap().to_path_buf(), exists)?;
} }
Err(error) => return Err(error.into()), Err(error) => return Err(error.into()),
} }

View file

@ -3,5 +3,5 @@ use crate::music_storage::library::{AlbumArt, Song};
pub trait MusicCollection { pub trait MusicCollection {
fn title(&self) -> &String; fn title(&self) -> &String;
fn cover(&self) -> Option<&AlbumArt>; fn cover(&self) -> Option<&AlbumArt>;
fn tracks(&self) -> Vec<&Song>; fn tracks(&self) -> Vec<Song>;
} }

View file

@ -1,15 +1,15 @@
use std::path::Path;
use chrono::Duration; use chrono::Duration;
use walkdir::Error; use walkdir::Error;
use super::{ use super::{
db_reader::extern_library::ExternalLibrary, library::{AlbumArt, Song, Tag},
library::{self, AlbumArt, Song, Tag}, music_collection::MusicCollection, db_reader::{
music_collection::MusicCollection, xml::reader::XmlLibrary,
extern_library::ExternalLibrary
},
}; };
use crate::music_storage::db_reader::xml::reader::XmlLibrary;
use std::io::Read;
use std::{default, path::Path, path::PathBuf, thread::AccessError};
use m3u8_rs::{MediaPlaylist, MediaPlaylistType, MediaSegment}; use m3u8_rs::{MediaPlaylist, MediaPlaylistType, MediaSegment};
// use nom::IResult; // use nom::IResult;
@ -18,7 +18,7 @@ use m3u8_rs::{MediaPlaylist, MediaPlaylistType, MediaSegment};
pub struct Playlist<'a> { pub struct Playlist<'a> {
title: String, title: String,
cover: Option<&'a AlbumArt>, cover: Option<&'a AlbumArt>,
tracks: Vec<&'a Song>, tracks: Vec<Song>,
play_count: i32, play_count: i32,
play_time: Duration, play_time: Duration,
} }
@ -32,11 +32,11 @@ impl<'a> Playlist<'a> {
pub fn play_time(&self) -> chrono::Duration { pub fn play_time(&self) -> chrono::Duration {
self.play_time self.play_time
} }
pub fn set_tracks(&mut self, songs: Vec<&'a Song>) -> Result<(), Error> { pub fn set_tracks(&mut self, songs: Vec<Song>) -> Result<(), Error> {
self.tracks = songs; self.tracks = songs;
Ok(()) Ok(())
} }
pub fn add_track(&mut self, song: &'a Song) -> Result<(), Error> { pub fn add_track(&mut self, song: Song) -> Result<(), Error> {
self.tracks.push(song); self.tracks.push(song);
Ok(()) Ok(())
} }
@ -79,14 +79,7 @@ impl<'a> Playlist<'a> {
|track| MediaSegment { |track| MediaSegment {
uri: track.location.to_string().into(), uri: track.location.to_string().into(),
duration: track.duration.as_millis() as f32, duration: track.duration.as_millis() as f32,
title: Some( title: Some(track.tags.get_key_value(&Tag::Title).unwrap().1.into()),
track
.tags
.get_key_value(&Tag::Title)
.unwrap()
.1
.into(),
),
..Default::default() ..Default::default()
} }
}) })
@ -125,8 +118,8 @@ impl MusicCollection for Playlist<'_> {
None => None, None => None,
} }
} }
fn tracks(&self) -> Vec<&Song> { fn tracks(&self) -> Vec<Song> {
self.tracks.clone() self.tracks
} }
} }
impl Default for Playlist<'_> { impl Default for Playlist<'_> {
@ -148,7 +141,7 @@ fn list_to_m3u8() {
)); ));
let mut a = Playlist::new(); let mut a = Playlist::new();
let c = lib.to_songs(); let c = lib.to_songs();
let mut b = c.iter().map( |song| song ).collect::<Vec<&Song>>(); let mut b = c.iter().map(|song| song).collect::<Vec<&Song>>();
a.tracks.append(&mut b); a.tracks.append(&mut b);
a.to_m3u8() a.to_m3u8()
} }