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
}
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<File, Error> {
match File::open(self.path.as_path()) {
@ -39,6 +35,7 @@ pub struct Config {
pub path: PathBuf,
default_library: Uuid,
pub libraries: Vec<ConfigLibrary>,
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<Self, Error> {
pub fn load_file(path: PathBuf) -> Result<Self, Error> {
let mut file: File = File::open(path)?;
let mut bun: String = String::new();
_ = file.read_to_string(&mut bun);

View file

@ -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<Song>,
}
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
///
/// 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>>) -> 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 mut library: Vec<Song> = Vec::new();
let mut backup_path = global_config.db_path.clone();