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)]
pub struct Config {
db_path: Option<PathBuf>,
pub db_path: Option<PathBuf>,
}
impl Config {

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
use super::utils::{find_images, normalize, read_library, write_library};
use super::music_collection::MusicCollection;
use crate::config::config::Config;
// Various std things
use std::collections::BTreeMap;
@ -269,6 +270,16 @@ pub struct Album<'a> {
#[allow(clippy::len_without_is_empty)]
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
pub fn artist(&self) -> Option<&String> {
self.artist
@ -283,6 +294,14 @@ impl Album<'_> {
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
pub fn len(&self) -> usize {
let mut total = 0;
@ -292,23 +311,6 @@ impl Album<'_> {
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"];
@ -327,20 +329,20 @@ impl MusicLibrary {
let global_config = &*config.read().unwrap();
let mut library: Vec<Song> = Vec::new();
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 => {
library = read_library(global_config.db_path.clone())?;
library = read_library(global_config.db_path.unwrap().clone())?;
}
false => {
// Create the database if it does not exist
// possibly from the backup file
if backup_path.exists() {
library = read_library(backup_path.clone())?;
write_library(&library, global_config.db_path.to_path_buf(), false)?;
if backup_path.unwrap().exists() {
library = read_library(backup_path.unwrap().clone())?;
write_library(&library, global_config.db_path.unwrap().to_path_buf(), false)?;
} 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
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) => {
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()),
}

View file

@ -3,5 +3,5 @@ use crate::music_storage::library::{AlbumArt, Song};
pub trait MusicCollection {
fn title(&self) -> &String;
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 walkdir::Error;
use super::{
db_reader::extern_library::ExternalLibrary,
library::{self, AlbumArt, Song, Tag},
music_collection::MusicCollection,
library::{AlbumArt, Song, Tag},
music_collection::MusicCollection, db_reader::{
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 nom::IResult;
@ -18,7 +18,7 @@ use m3u8_rs::{MediaPlaylist, MediaPlaylistType, MediaSegment};
pub struct Playlist<'a> {
title: String,
cover: Option<&'a AlbumArt>,
tracks: Vec<&'a Song>,
tracks: Vec<Song>,
play_count: i32,
play_time: Duration,
}
@ -32,11 +32,11 @@ impl<'a> Playlist<'a> {
pub fn play_time(&self) -> chrono::Duration {
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;
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);
Ok(())
}
@ -79,14 +79,7 @@ impl<'a> Playlist<'a> {
|track| MediaSegment {
uri: track.location.to_string().into(),
duration: track.duration.as_millis() as f32,
title: Some(
track
.tags
.get_key_value(&Tag::Title)
.unwrap()
.1
.into(),
),
title: Some(track.tags.get_key_value(&Tag::Title).unwrap().1.into()),
..Default::default()
}
})
@ -125,8 +118,8 @@ impl MusicCollection for Playlist<'_> {
None => None,
}
}
fn tracks(&self) -> Vec<&Song> {
self.tracks.clone()
fn tracks(&self) -> Vec<Song> {
self.tracks
}
}
impl Default for Playlist<'_> {