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>, pub library: MusicLibrary, } impl MusicController { /// Creates new MusicController with config at given path pub fn new(config_path: &PathBuf) -> Result> { 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> { 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` pub fn query_library( &self, query_string: &String, target_tags: Vec, _search_location: bool, sort_by: Vec, ) -> Option> { self.library .query_tracks(query_string, &target_tags, &sort_by) } }