mirror of
https://github.com/Dangoware/dango-music-player.git
synced 2025-04-19 10:02:53 -05:00
broke code
This commit is contained in:
parent
c057352e9b
commit
b937ac55f1
3 changed files with 73 additions and 41 deletions
|
@ -2,10 +2,11 @@ use std::{path::{PathBuf, Path}, marker::PhantomData, fs::{File, OpenOptions, se
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use serde_json::{to_string, to_string_pretty};
|
use serde_json::{to_string, to_string_pretty};
|
||||||
|
use thiserror::Error;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
struct ConfigLibrary {
|
pub struct ConfigLibrary {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub uuid: Uuid
|
pub uuid: Uuid
|
||||||
|
@ -35,33 +36,52 @@ pub struct Config {
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
default_library: Uuid,
|
default_library: Uuid,
|
||||||
pub libraries: Vec<ConfigLibrary>,
|
pub libraries: Vec<ConfigLibrary>,
|
||||||
|
pub library_folder: PathBuf,
|
||||||
volume: f32,
|
volume: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Config {
|
||||||
|
libraries: vec![ConfigLibrary::default()],
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn new_main() -> Self {
|
pub fn new_main() -> Self {
|
||||||
Config::default()
|
Config::default()
|
||||||
}
|
}
|
||||||
//TODO: Add new function for test tube
|
//TODO: Add new function for test tube
|
||||||
pub fn set_default_library(&self, uuid: Uuid) {
|
pub fn set_default_library(mut self, uuid: &Uuid) {
|
||||||
self.default_library = uuid;
|
self.default_library = *uuid;
|
||||||
}
|
}
|
||||||
//TODO: make this a ConfigError type
|
pub fn get_default_library(&self) -> Result<&ConfigLibrary, ConfigError> {
|
||||||
pub fn default_library(&self) -> Result<&ConfigLibrary, String> {
|
|
||||||
for library in &self.libraries {
|
for library in &self.libraries {
|
||||||
if library.uuid == self.default_library {
|
if library.uuid == self.default_library {
|
||||||
return Ok(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> {
|
pub fn to_file(&self) -> Result<(), Error> {
|
||||||
let mut writer = self.path.clone();
|
let mut writer = self.path.clone();
|
||||||
writer.set_extension("tmp");
|
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)?;
|
let config = to_string_pretty(self)?;
|
||||||
|
|
||||||
file.write_all(&config.as_bytes())?;
|
file.write_all(&config.as_bytes())?;
|
||||||
|
@ -76,3 +96,11 @@ impl Config {
|
||||||
Ok(ny)
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
use super::utils::{find_images, normalize, read_library, write_library};
|
use super::utils::{find_images, normalize, read_library, write_library};
|
||||||
use super::music_collection::MusicCollection;
|
use super::music_collection::MusicCollection;
|
||||||
use crate::config::config::Config;
|
use crate::config::config::Config;
|
||||||
|
use crate::music_storage::library;
|
||||||
|
|
||||||
// Various std things
|
// Various std things
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
@ -321,52 +322,62 @@ pub struct MusicLibrary {
|
||||||
pub uuid: Uuid,
|
pub uuid: Uuid,
|
||||||
pub library: Vec<Song>,
|
pub library: Vec<Song>,
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_() {
|
||||||
|
let a = MusicLibrary::init(Arc::new(RwLock::from(Config::new())), None).unwrap();
|
||||||
|
dbg!(a);
|
||||||
|
}
|
||||||
impl MusicLibrary {
|
impl MusicLibrary {
|
||||||
pub fn with_uuid(uuid: Uuid, path: PathBuf) -> Result<Self, Box<dyn Error>> {
|
pub fn new() -> Self {
|
||||||
MusicLibrary {
|
MusicLibrary {
|
||||||
name: String::default(),
|
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,
|
uuid,
|
||||||
library: Vec::new(),
|
library: Vec::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
todo!()
|
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>>, 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 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();
|
||||||
true => {
|
|
||||||
library = read_library(global_config.db_path.unwrap().clone())?;
|
if let Some(uuid) = uuid {
|
||||||
}
|
match global_config.library_exists(&uuid) {
|
||||||
false => {
|
true => {
|
||||||
// Create the database if it does not exist
|
library.library = read_library(global_config.get_library(&uuid)?.path)?;
|
||||||
// possibly from the backup file
|
},
|
||||||
if backup_path.unwrap().exists() {
|
false => {
|
||||||
library = read_library(backup_path.unwrap().clone())?;
|
// Create the database if it does not exist
|
||||||
write_library(&library, global_config.db_path.unwrap().to_path_buf(), false)?;
|
write_library(&library.library, global_config.path.clone())?;
|
||||||
} else {
|
library = MusicLibrary::with_uuid(uuid, global_config.path.parent().unwrap().to_path_buf())?;
|
||||||
write_library(&library, global_config.db_path.unwrap().to_path_buf(), false)?;
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
}else {
|
||||||
|
write_library(&library.library, global_config.path.clone())?;
|
||||||
Ok(Self { library })
|
}
|
||||||
|
Ok(library)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes the database out to the file specified in the config
|
/// Serializes the database out to the file specified in the config
|
||||||
pub fn save(&self, config: &Config) -> Result<(), Box<dyn Error>> {
|
pub fn save(&self, config: &Config) -> Result<(), Box<dyn Error>> {
|
||||||
match config.db_path.unwrap().try_exists() {
|
let path = config.get_library(&self.uuid)?.path;
|
||||||
Ok(exists) => {
|
match path.try_exists() {
|
||||||
write_library(&self.library, config.db_path.unwrap().to_path_buf(), exists)?;
|
Ok(_) => {
|
||||||
|
write_library(&self.library, path)?;
|
||||||
}
|
}
|
||||||
Err(error) => return Err(error.into()),
|
Err(error) => return Err(error.into()),
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,13 +38,10 @@ pub(super) fn read_library(path: PathBuf) -> Result<Vec<Song>, Box<dyn Error>> {
|
||||||
pub(super) fn write_library(
|
pub(super) fn write_library(
|
||||||
library: &Vec<Song>,
|
library: &Vec<Song>,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
take_backup: bool,
|
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> Result<(), Box<dyn Error>> {
|
||||||
// Create 2 new names for the file, a temporary one for writing out, and a backup
|
// Create 2 new names for the file, a temporary one for writing out, and a backup
|
||||||
let mut writer_name = path.clone();
|
let mut writer_name = path.clone();
|
||||||
writer_name.set_extension("tmp");
|
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
|
// 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)?);
|
let writer = BufWriter::new(fs::File::create(&writer_name)?);
|
||||||
|
@ -58,10 +55,6 @@ pub(super) fn write_library(
|
||||||
.with_little_endian()
|
.with_little_endian()
|
||||||
.with_variable_int_encoding(),
|
.with_variable_int_encoding(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if path.exists() && take_backup {
|
|
||||||
fs::rename(&path, backup_name)?;
|
|
||||||
}
|
|
||||||
fs::rename(writer_name, &path)?;
|
fs::rename(writer_name, &path)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue