From b937ac55f1fec63db176037a637d69e255aaba60 Mon Sep 17 00:00:00 2001 From: MrDulfin Date: Fri, 19 Jan 2024 05:33:58 -0500 Subject: [PATCH] broke code --- src/config/config.rs | 46 +++++++++++++++++++++------ src/music_storage/library.rs | 61 +++++++++++++++++++++--------------- src/music_storage/utils.rs | 7 ----- 3 files changed, 73 insertions(+), 41 deletions(-) diff --git a/src/config/config.rs b/src/config/config.rs index ec2a521..b3ce860 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -2,10 +2,11 @@ use std::{path::{PathBuf, Path}, marker::PhantomData, fs::{File, OpenOptions, se use serde::{Serialize, Deserialize}; use serde_json::{to_string, to_string_pretty}; +use thiserror::Error; use uuid::Uuid; #[derive(Debug, Clone, Serialize, Deserialize)] -struct ConfigLibrary { +pub struct ConfigLibrary { pub name: String, pub path: PathBuf, pub uuid: Uuid @@ -35,33 +36,52 @@ pub struct Config { pub path: PathBuf, default_library: Uuid, pub libraries: Vec, + pub library_folder: PathBuf, volume: f32, } impl Config { + pub fn new() -> Self { + Config { + libraries: vec![ConfigLibrary::default()], + ..Default::default() + } + } pub fn new_main() -> Self { Config::default() } //TODO: Add new function for test tube - pub fn set_default_library(&self, uuid: Uuid) { - self.default_library = uuid; + pub fn set_default_library(mut self, uuid: &Uuid) { + self.default_library = *uuid; } - //TODO: make this a ConfigError type - pub fn default_library(&self) -> Result<&ConfigLibrary, String> { + pub fn get_default_library(&self) -> Result<&ConfigLibrary, ConfigError> { for library in &self.libraries { if library.uuid == self.default_library { return Ok(library) } - else { - continue; + } + Err(ConfigError::NoDefaultLibrary) + } + pub fn get_library(&self, uuid: &Uuid) -> Result { + for library in &self.libraries { + if &library.uuid == uuid { + return Ok(library.to_owned()) } } - Err("No default library!".to_string()) + Err(ConfigError::NoConfigLibrary(*uuid)) + } + pub fn library_exists(&self, uuid: &Uuid) -> bool { + for library in &self.libraries { + if &library.uuid == uuid { + return true + } + } + false } pub fn to_file(&self) -> Result<(), Error> { let mut writer = self.path.clone(); writer.set_extension("tmp"); - let mut file = OpenOptions::new().create(true).truncate(true).read(true).write(true).open(writer)?; + let mut file = OpenOptions::new().create(true).truncate(true).read(true).write(true).open(&writer)?; let config = to_string_pretty(self)?; file.write_all(&config.as_bytes())?; @@ -76,3 +96,11 @@ impl Config { Ok(ny) } } + +#[derive(Error, Debug)] +pub enum ConfigError { + #[error("No Library Found for {0}!")] + NoConfigLibrary(Uuid), + #[error("There is no Default Library for this Config")] + NoDefaultLibrary +} diff --git a/src/music_storage/library.rs b/src/music_storage/library.rs index a9ffe7a..2bd3145 100644 --- a/src/music_storage/library.rs +++ b/src/music_storage/library.rs @@ -2,6 +2,7 @@ use super::utils::{find_images, normalize, read_library, write_library}; use super::music_collection::MusicCollection; use crate::config::config::Config; +use crate::music_storage::library; // Various std things use std::collections::BTreeMap; @@ -321,52 +322,62 @@ pub struct MusicLibrary { pub uuid: Uuid, pub library: Vec, } - +#[test] + fn test_() { + let a = MusicLibrary::init(Arc::new(RwLock::from(Config::new())), None).unwrap(); + dbg!(a); + } impl MusicLibrary { - pub fn with_uuid(uuid: Uuid, path: PathBuf) -> Result> { + pub fn new() -> Self { MusicLibrary { name: String::default(), + uuid: Uuid::default(), + library: Vec::new(), + } + } + pub fn with_uuid(uuid: Uuid, path: PathBuf) -> Result> { + MusicLibrary { + name: String::new(), uuid, library: Vec::new(), }; todo!() } + /// Initialize the database /// /// If the database file already exists, return the [MusicLibrary], otherwise create /// the database first. This needs to be run before anything else to retrieve /// the [MusicLibrary] Vec - pub fn init(config: Arc>, uuid: Uuid) -> Result> { + pub fn init(config: Arc>, uuid: Option) -> Result> { let global_config = &*config.read().unwrap(); - let mut library: Vec = Vec::new(); - let mut backup_path = global_config.db_path.clone(); - backup_path.unwrap().set_extension("bkp"); - match global_config.db_path.unwrap().exists() { - true => { - 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.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.unwrap().to_path_buf(), false)?; + let mut library = MusicLibrary::new(); + + if let Some(uuid) = uuid { + match global_config.library_exists(&uuid) { + true => { + library.library = read_library(global_config.get_library(&uuid)?.path)?; + }, + false => { + // Create the database if it does not exist + write_library(&library.library, global_config.path.clone())?; + library = MusicLibrary::with_uuid(uuid, global_config.path.parent().unwrap().to_path_buf())?; } - } - }; - - Ok(Self { library }) + }; + }else { + write_library(&library.library, global_config.path.clone())?; + } + Ok(library) } /// Serializes the database out to the file specified in the config pub fn save(&self, config: &Config) -> Result<(), Box> { - match config.db_path.unwrap().try_exists() { - Ok(exists) => { - write_library(&self.library, config.db_path.unwrap().to_path_buf(), exists)?; + let path = config.get_library(&self.uuid)?.path; + match path.try_exists() { + Ok(_) => { + write_library(&self.library, path)?; } Err(error) => return Err(error.into()), } diff --git a/src/music_storage/utils.rs b/src/music_storage/utils.rs index a25313a..c42a6b5 100644 --- a/src/music_storage/utils.rs +++ b/src/music_storage/utils.rs @@ -38,13 +38,10 @@ pub(super) fn read_library(path: PathBuf) -> Result, Box> { pub(super) fn write_library( library: &Vec, path: PathBuf, - take_backup: bool, ) -> Result<(), Box> { // Create 2 new names for the file, a temporary one for writing out, and a backup let mut writer_name = path.clone(); writer_name.set_extension("tmp"); - let mut backup_name = path.clone(); - backup_name.set_extension("bkp"); // Create a new BufWriter on the file and make a snap frame encoer for it too let writer = BufWriter::new(fs::File::create(&writer_name)?); @@ -58,10 +55,6 @@ pub(super) fn write_library( .with_little_endian() .with_variable_int_encoding(), )?; - - if path.exists() && take_backup { - fs::rename(&path, backup_name)?; - } fs::rename(writer_name, &path)?; Ok(())