broke code

This commit is contained in:
MrDulfin 2024-01-19 05:33:58 -05:00
parent c057352e9b
commit b937ac55f1
3 changed files with 73 additions and 41 deletions

View file

@ -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<ConfigLibrary>,
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<ConfigLibrary, ConfigError> {
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
}

View file

@ -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<Song>,
}
#[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<Self, Box<dyn Error>> {
pub fn new() -> Self {
MusicLibrary {
name: String::default(),
uuid: Uuid::default(),
library: Vec::new(),
}
}
pub fn with_uuid(uuid: Uuid, path: PathBuf) -> Result<Self, Box<dyn Error>> {
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<RwLock<Config>>, uuid: Uuid) -> Result<Self, Box<dyn Error>> {
pub fn init(config: Arc<RwLock<Config>>, uuid: Option<Uuid>) -> Result<Self, Box<dyn Error>> {
let global_config = &*config.read().unwrap();
let mut library: Vec<Song> = Vec::new();
let mut backup_path = global_config.db_path.clone();
backup_path.unwrap().set_extension("bkp");
match global_config.db_path.unwrap().exists() {
let mut library = MusicLibrary::new();
if let Some(uuid) = uuid {
match global_config.library_exists(&uuid) {
true => {
library = read_library(global_config.db_path.unwrap().clone())?;
}
library.library = read_library(global_config.get_library(&uuid)?.path)?;
},
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)?;
}
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<dyn Error>> {
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()),
}

View file

@ -38,13 +38,10 @@ pub(super) fn read_library(path: PathBuf) -> Result<Vec<Song>, Box<dyn Error>> {
pub(super) fn write_library(
library: &Vec<Song>,
path: PathBuf,
take_backup: bool,
) -> Result<(), Box<dyn Error>> {
// 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(())