From 4546082e5446031e7f51f8ef20fb175e4f10ac7b Mon Sep 17 00:00:00 2001 From: MrDulfin Date: Wed, 17 Jan 2024 09:07:06 -0500 Subject: [PATCH] created more functions for the config --- Cargo.toml | 2 + src/config/config.rs | 72 ++++++++++++++++++++++++++---- src/music_controller/controller.rs | 4 +- src/music_storage/playlist.rs | 4 +- 4 files changed, 71 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5a2fa1e..aaf4409 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,3 +35,5 @@ urlencoding = "2.1.3" m3u8-rs = "5.0.5" thiserror = "1.0.56" font = "0.27.0" +uuid = { version = "1.6.1", features = ["v4", "serde"]} +serde_json = "1.0.111" diff --git a/src/config/config.rs b/src/config/config.rs index 76ab38a..a54186f 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -1,15 +1,44 @@ -use std::{path::PathBuf, marker::PhantomData}; +use std::{path::{PathBuf, Path}, marker::PhantomData, fs::{File, OpenOptions, self}, io::{Error, Write, Read}, default}; -use crate::music_storage::library::MusicLibrary; +use serde::{Serialize, Deserialize}; +use serde_json::{to_string, to_string_pretty}; +use uuid::Uuid; -#[derive(Debug, Default)] +#[derive(Debug, Clone, Serialize, Deserialize)] struct ConfigLibrary { - name: String, - path: PathBuf, + pub name: String, + pub path: PathBuf, + pub uuid: Uuid } -#[derive(Debug, Default)] +impl ConfigLibrary { + fn new() -> Self { + ConfigLibrary { + name: String::new(), + path: PathBuf::default(), + uuid: Uuid::new_v4() + } + } + pub fn open(&self) -> Result { + match File::open(self.path.as_path()) { + Ok(ok) => Ok(ok), + Err(e) => Err(e) + } + } +} +impl Default for ConfigLibrary { + fn default() -> Self { + ConfigLibrary { + name: String::default(), + path: PathBuf::default(), + uuid: Uuid::new_v4() + } + } +} +#[derive(Debug, Default, Serialize, Deserialize)] pub struct Config { - libraries: Vec + pub path: PathBuf, + default_library: Uuid, + pub libraries: Vec, } impl Config { @@ -17,7 +46,34 @@ impl Config { Config::default() } //TODO: Add new function for test tube - pub fn load(&self) { + pub fn set_default_library(&self, uuid: Uuid) { + self.default_library = uuid; + } + //TODO: make this a ConfigError type + pub fn default_library(&self) -> Result<&ConfigLibrary, String> { + for library in &self.libraries { + if library.uuid == self.default_library { + return Ok(library) + } + else { + continue; + } + } + Err("No default library!".to_string()) + } + pub fn save(&self) -> Result<(), Error> { + let mut file = OpenOptions::new().create(true).truncate(true).read(true).write(true).open("dango_temp_config_save.json")?; + let config = to_string_pretty(self)?; + file.write_all(&config.as_bytes())?; + fs::rename("dango_temp_config_save.json", self.path.as_path())?; + Ok(()) + } + pub fn load(path: PathBuf) -> Result { + let mut file: File = File::open(path)?; + let mut bun: String = String::new(); + _ = file.read_to_string(&mut bun); + let ny: Config = serde_json::from_str::(&bun)?; + Ok(ny) } } diff --git a/src/music_controller/controller.rs b/src/music_controller/controller.rs index 94509d8..7f62871 100644 --- a/src/music_controller/controller.rs +++ b/src/music_controller/controller.rs @@ -2,6 +2,8 @@ //! player. It manages queues, playback, library access, and //! other functions +use std::sync::{Arc, RwLock}; + use crate::{ music_player::Player, music_storage::library::Song, @@ -16,7 +18,7 @@ struct Queue { pub struct Controller { queues: Vec, - config: Config, + config: Arc>, } impl Controller { diff --git a/src/music_storage/playlist.rs b/src/music_storage/playlist.rs index 62b3aa9..cc8166c 100644 --- a/src/music_storage/playlist.rs +++ b/src/music_storage/playlist.rs @@ -119,7 +119,7 @@ impl MusicCollection for Playlist<'_> { } } fn tracks(&self) -> Vec { - self.tracks + self.tracks.to_owned() } } impl Default for Playlist<'_> { @@ -141,7 +141,7 @@ fn list_to_m3u8() { )); let mut a = Playlist::new(); let c = lib.to_songs(); - let mut b = c.iter().map(|song| song).collect::>(); + let mut b = c.iter().map(|song| song.to_owned()).collect::>(); a.tracks.append(&mut b); a.to_m3u8() }