mirror of
https://github.com/Dangoware/dmp-core.git
synced 2025-04-20 01:22:53 -05:00
created more functions for the config
This commit is contained in:
parent
634b12d353
commit
4546082e54
4 changed files with 71 additions and 11 deletions
|
@ -35,3 +35,5 @@ urlencoding = "2.1.3"
|
||||||
m3u8-rs = "5.0.5"
|
m3u8-rs = "5.0.5"
|
||||||
thiserror = "1.0.56"
|
thiserror = "1.0.56"
|
||||||
font = "0.27.0"
|
font = "0.27.0"
|
||||||
|
uuid = { version = "1.6.1", features = ["v4", "serde"]}
|
||||||
|
serde_json = "1.0.111"
|
||||||
|
|
|
@ -1,15 +1,44 @@
|
||||||
use std::{path::PathBuf, marker::PhantomData};
|
use std::{path::{PathBuf, Path}, marker::PhantomData, fs::{File, OpenOptions, self}, io::{Error, Write, Read}, default};
|
||||||
|
|
||||||
use crate::music_storage::library::MusicLibrary;
|
use serde::{Serialize, Deserialize};
|
||||||
|
use serde_json::{to_string, to_string_pretty};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
struct ConfigLibrary {
|
struct ConfigLibrary {
|
||||||
name: String,
|
pub name: String,
|
||||||
path: PathBuf,
|
pub path: PathBuf,
|
||||||
|
pub uuid: Uuid
|
||||||
}
|
}
|
||||||
#[derive(Debug, Default)]
|
impl ConfigLibrary {
|
||||||
|
fn new() -> Self {
|
||||||
|
ConfigLibrary {
|
||||||
|
name: String::new(),
|
||||||
|
path: PathBuf::default(),
|
||||||
|
uuid: Uuid::new_v4()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn open(&self) -> Result<File, Error> {
|
||||||
|
match File::open(self.path.as_path()) {
|
||||||
|
Ok(ok) => Ok(ok),
|
||||||
|
Err(e) => Err(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Default for ConfigLibrary {
|
||||||
|
fn default() -> Self {
|
||||||
|
ConfigLibrary {
|
||||||
|
name: String::default(),
|
||||||
|
path: PathBuf::default(),
|
||||||
|
uuid: Uuid::new_v4()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
libraries: Vec<ConfigLibrary>
|
pub path: PathBuf,
|
||||||
|
default_library: Uuid,
|
||||||
|
pub libraries: Vec<ConfigLibrary>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
@ -17,7 +46,34 @@ impl Config {
|
||||||
Config::default()
|
Config::default()
|
||||||
}
|
}
|
||||||
//TODO: Add new function for test tube
|
//TODO: Add new function for test tube
|
||||||
pub fn load(&self) {
|
pub fn set_default_library(&self, uuid: Uuid) {
|
||||||
|
self.default_library = uuid;
|
||||||
|
}
|
||||||
|
//TODO: make this a ConfigError type
|
||||||
|
pub fn default_library(&self) -> Result<&ConfigLibrary, String> {
|
||||||
|
for library in &self.libraries {
|
||||||
|
if library.uuid == self.default_library {
|
||||||
|
return Ok(library)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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")?;
|
||||||
|
let config = to_string_pretty(self)?;
|
||||||
|
|
||||||
|
file.write_all(&config.as_bytes())?;
|
||||||
|
fs::rename("dango_temp_config_save.json", self.path.as_path())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn load(path: PathBuf) -> Result<Self, Error> {
|
||||||
|
let mut file: File = File::open(path)?;
|
||||||
|
let mut bun: String = String::new();
|
||||||
|
_ = file.read_to_string(&mut bun);
|
||||||
|
let ny: Config = serde_json::from_str::<Config>(&bun)?;
|
||||||
|
Ok(ny)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
//! player. It manages queues, playback, library access, and
|
//! player. It manages queues, playback, library access, and
|
||||||
//! other functions
|
//! other functions
|
||||||
|
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
music_player::Player,
|
music_player::Player,
|
||||||
music_storage::library::Song,
|
music_storage::library::Song,
|
||||||
|
@ -16,7 +18,7 @@ struct Queue {
|
||||||
|
|
||||||
pub struct Controller {
|
pub struct Controller {
|
||||||
queues: Vec<Queue>,
|
queues: Vec<Queue>,
|
||||||
config: Config,
|
config: Arc<RwLock<Config>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Controller {
|
impl Controller {
|
||||||
|
|
|
@ -119,7 +119,7 @@ impl MusicCollection for Playlist<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn tracks(&self) -> Vec<Song> {
|
fn tracks(&self) -> Vec<Song> {
|
||||||
self.tracks
|
self.tracks.to_owned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Default for Playlist<'_> {
|
impl Default for Playlist<'_> {
|
||||||
|
@ -141,7 +141,7 @@ fn list_to_m3u8() {
|
||||||
));
|
));
|
||||||
let mut a = Playlist::new();
|
let mut a = Playlist::new();
|
||||||
let c = lib.to_songs();
|
let c = lib.to_songs();
|
||||||
let mut b = c.iter().map(|song| song).collect::<Vec<&Song>>();
|
let mut b = c.iter().map(|song| song.to_owned()).collect::<Vec<Song>>();
|
||||||
a.tracks.append(&mut b);
|
a.tracks.append(&mut b);
|
||||||
a.to_m3u8()
|
a.to_m3u8()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue