Changed MusicLibrary and config file handling

This commit is contained in:
MrDulfin 2024-01-17 15:22:28 -05:00
parent 4546082e54
commit c057352e9b
2 changed files with 22 additions and 11 deletions

View file

@ -11,12 +11,8 @@ struct ConfigLibrary {
pub uuid: Uuid pub uuid: Uuid
} }
impl ConfigLibrary { impl ConfigLibrary {
fn new() -> Self { pub fn new() -> Self {
ConfigLibrary { ConfigLibrary::default()
name: String::new(),
path: PathBuf::default(),
uuid: Uuid::new_v4()
}
} }
pub fn open(&self) -> Result<File, Error> { pub fn open(&self) -> Result<File, Error> {
match File::open(self.path.as_path()) { match File::open(self.path.as_path()) {
@ -39,6 +35,7 @@ pub struct Config {
pub path: PathBuf, pub path: PathBuf,
default_library: Uuid, default_library: Uuid,
pub libraries: Vec<ConfigLibrary>, pub libraries: Vec<ConfigLibrary>,
volume: f32,
} }
impl Config { impl Config {
@ -61,15 +58,17 @@ impl Config {
} }
Err("No default library!".to_string()) Err("No default library!".to_string())
} }
pub fn save(&self) -> Result<(), Error> { pub fn to_file(&self) -> Result<(), Error> {
let mut file = OpenOptions::new().create(true).truncate(true).read(true).write(true).open("dango_temp_config_save.json")?; 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)?; let config = to_string_pretty(self)?;
file.write_all(&config.as_bytes())?; 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(()) Ok(())
} }
pub fn load(path: PathBuf) -> Result<Self, Error> { pub fn load_file(path: PathBuf) -> Result<Self, Error> {
let mut file: File = File::open(path)?; let mut file: File = File::open(path)?;
let mut bun: String = String::new(); let mut bun: String = String::new();
_ = file.read_to_string(&mut bun); _ = file.read_to_string(&mut bun);

View file

@ -13,6 +13,7 @@ use file_format::{FileFormat, Kind};
use glib::filename_to_uri; use glib::filename_to_uri;
use lofty::{AudioFile, ItemKey, ItemValue, ParseOptions, Probe, TagType, TaggedFileExt}; use lofty::{AudioFile, ItemKey, ItemValue, ParseOptions, Probe, TagType, TaggedFileExt};
use rcue::parser::parse_from_file; use rcue::parser::parse_from_file;
use uuid::Uuid;
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use walkdir::WalkDir; use walkdir::WalkDir;
@ -316,16 +317,27 @@ const BLOCKED_EXTENSIONS: [&str; 4] = ["vob", "log", "txt", "sf2"];
#[derive(Debug)] #[derive(Debug)]
pub struct MusicLibrary { pub struct MusicLibrary {
pub name: String,
pub uuid: Uuid,
pub library: Vec<Song>, pub library: Vec<Song>,
} }
impl MusicLibrary { impl MusicLibrary {
pub fn with_uuid(uuid: Uuid, path: PathBuf) -> Result<Self, Box<dyn Error>> {
MusicLibrary {
name: String::default(),
uuid,
library: Vec::new(),
};
todo!()
}
/// Initialize the database /// Initialize the database
/// ///
/// If the database file already exists, return the [MusicLibrary], otherwise create /// 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 database first. This needs to be run before anything else to retrieve
/// the [MusicLibrary] Vec /// the [MusicLibrary] Vec
pub fn init(config: Arc<RwLock<Config>>) -> Result<Self, Box<dyn Error>> { pub fn init(config: Arc<RwLock<Config>>, uuid: Uuid) -> Result<Self, Box<dyn Error>> {
let global_config = &*config.read().unwrap(); let global_config = &*config.read().unwrap();
let mut library: Vec<Song> = Vec::new(); let mut library: Vec<Song> = Vec::new();
let mut backup_path = global_config.db_path.clone(); let mut backup_path = global_config.db_path.clone();