mirror of
https://github.com/Dangoware/dango-music-player.git
synced 2025-04-19 10:02:53 -05:00
Merge branch 'main' of https://github.com/Dangoware/dmp-core
This commit is contained in:
commit
6da574e7cb
2 changed files with 0 additions and 107 deletions
|
@ -1,57 +0,0 @@
|
||||||
use std::fs;
|
|
||||||
use std::fs::read_to_string;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Eq)]
|
|
||||||
pub struct Config {
|
|
||||||
pub db_path: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Config {
|
|
||||||
fn default() -> Self {
|
|
||||||
let path = PathBuf::from("./music_database");
|
|
||||||
|
|
||||||
Config {
|
|
||||||
db_path: path,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
|
||||||
/// Creates and saves a new config with default values
|
|
||||||
pub fn new(config_file: &PathBuf) -> std::io::Result<Config> {
|
|
||||||
let config = Config::default();
|
|
||||||
config.save(config_file)?;
|
|
||||||
|
|
||||||
Ok(config)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Loads config from given file path
|
|
||||||
pub fn from(config_file: &PathBuf) -> std::result::Result<Config, toml::de::Error> {
|
|
||||||
toml::from_str(
|
|
||||||
&read_to_string(config_file)
|
|
||||||
.expect("Failed to initalize music config: File not found!"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Saves config to given path
|
|
||||||
/// Saves -> temp file, if successful, removes old config, and renames temp to given path
|
|
||||||
pub fn save(&self, config_file: &PathBuf) -> std::io::Result<()> {
|
|
||||||
let toml = toml::to_string_pretty(self).unwrap();
|
|
||||||
|
|
||||||
let mut temp_file = config_file.clone();
|
|
||||||
temp_file.set_extension("tomltemp");
|
|
||||||
|
|
||||||
fs::write(&temp_file, toml)?;
|
|
||||||
|
|
||||||
// If configuration file already exists, delete it
|
|
||||||
if fs::metadata(config_file).is_ok() {
|
|
||||||
fs::remove_file(config_file)?
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::rename(temp_file, config_file)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,50 +0,0 @@
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::sync::{Arc, RwLock};
|
|
||||||
|
|
||||||
use crate::music_controller::config::Config;
|
|
||||||
use crate::music_storage::library::{MusicLibrary, Song, Tag};
|
|
||||||
|
|
||||||
pub struct MusicController {
|
|
||||||
pub config: Arc<RwLock<Config>>,
|
|
||||||
pub library: MusicLibrary,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MusicController {
|
|
||||||
/// Creates new MusicController with config at given path
|
|
||||||
pub fn new(config_path: &PathBuf) -> Result<MusicController, Box<dyn std::error::Error>> {
|
|
||||||
let config = Arc::new(RwLock::new(Config::new(config_path)?));
|
|
||||||
let library = match MusicLibrary::init(config.clone()) {
|
|
||||||
Ok(library) => library,
|
|
||||||
Err(error) => return Err(error),
|
|
||||||
};
|
|
||||||
|
|
||||||
let controller = MusicController { config, library };
|
|
||||||
|
|
||||||
Ok(controller)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates new music controller from a config at given path
|
|
||||||
pub fn from(config_path: &PathBuf) -> Result<MusicController, Box<dyn std::error::Error>> {
|
|
||||||
let config = Arc::new(RwLock::new(Config::from(config_path)?));
|
|
||||||
let library = match MusicLibrary::init(config.clone()) {
|
|
||||||
Ok(library) => library,
|
|
||||||
Err(error) => return Err(error),
|
|
||||||
};
|
|
||||||
|
|
||||||
let controller = MusicController { config, library };
|
|
||||||
|
|
||||||
Ok(controller)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Queries the [MusicLibrary], returning a `Vec<Song>`
|
|
||||||
pub fn query_library(
|
|
||||||
&self,
|
|
||||||
query_string: &String,
|
|
||||||
target_tags: Vec<Tag>,
|
|
||||||
_search_location: bool,
|
|
||||||
sort_by: Vec<Tag>,
|
|
||||||
) -> Option<Vec<&Song>> {
|
|
||||||
self.library
|
|
||||||
.query_tracks(query_string, &target_tags, &sort_by)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue