diff --git a/src/config/config.rs b/src/config/config.rs index a54186f..ec2a521 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -11,12 +11,8 @@ struct ConfigLibrary { pub uuid: Uuid } impl ConfigLibrary { - fn new() -> Self { - ConfigLibrary { - name: String::new(), - path: PathBuf::default(), - uuid: Uuid::new_v4() - } + pub fn new() -> Self { + ConfigLibrary::default() } pub fn open(&self) -> Result { match File::open(self.path.as_path()) { @@ -39,6 +35,7 @@ pub struct Config { pub path: PathBuf, default_library: Uuid, pub libraries: Vec, + volume: f32, } impl Config { @@ -61,15 +58,17 @@ impl Config { } 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")?; + 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 config = to_string_pretty(self)?; file.write_all(&config.as_bytes())?; - fs::rename("dango_temp_config_save.json", self.path.as_path())?; + fs::rename(writer, self.path.as_path())?; Ok(()) } - pub fn load(path: PathBuf) -> Result { + pub fn load_file(path: PathBuf) -> Result { let mut file: File = File::open(path)?; let mut bun: String = String::new(); _ = file.read_to_string(&mut bun); diff --git a/src/music_storage/library.rs b/src/music_storage/library.rs index 873865f..a9ffe7a 100644 --- a/src/music_storage/library.rs +++ b/src/music_storage/library.rs @@ -13,6 +13,7 @@ use file_format::{FileFormat, Kind}; use glib::filename_to_uri; use lofty::{AudioFile, ItemKey, ItemValue, ParseOptions, Probe, TagType, TaggedFileExt}; use rcue::parser::parse_from_file; +use uuid::Uuid; use std::fs; use std::path::{Path, PathBuf}; use walkdir::WalkDir; @@ -316,16 +317,27 @@ const BLOCKED_EXTENSIONS: [&str; 4] = ["vob", "log", "txt", "sf2"]; #[derive(Debug)] pub struct MusicLibrary { + pub name: String, + pub uuid: Uuid, pub library: Vec, } impl MusicLibrary { + pub fn with_uuid(uuid: Uuid, path: PathBuf) -> Result> { + MusicLibrary { + name: String::default(), + 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>) -> Result> { + pub fn init(config: Arc>, uuid: Uuid) -> Result> { let global_config = &*config.read().unwrap(); let mut library: Vec = Vec::new(); let mut backup_path = global_config.db_path.clone();