//! The [Controller] is the input and output for the entire //! player. It manages queues, playback, library access, and //! other functions use crossbeam_channel; use crossbeam_channel::{Receiver, Sender}; use std::path::PathBuf; use std::sync::{Arc, RwLock}; use thiserror::Error; use crossbeam_channel::unbounded; use std::error::Error; use uuid::Uuid; use crate::config::config::ConfigError; use crate::music_player::player::{Player, PlayerError}; use crate::{ config::config::Config, music_controller::queue::Queue, music_storage::library::MusicLibrary, }; use super::queue::QueueError; pub struct Controller { pub queue: Queue, pub config: Arc>, pub library: MusicLibrary, pub player: Box

, } #[derive(Error, Debug)] pub enum ControllerError { #[error("{0:?}")] QueueError(#[from] QueueError), #[error("{0:?}")] PlayerError(#[from] PlayerError), #[error("{0:?}")] ConfigError(#[from] ConfigError), } #[derive(Debug)] pub(super) struct MailMan { pub tx: Sender, rx: Receiver, } impl MailMan { pub fn new() -> Self { let (tx, rx) = unbounded::(); MailMan { tx, rx } } } impl MailMan { pub fn double() -> (MailMan, MailMan) { let (tx, rx) = unbounded::(); let (tx1, rx1) = unbounded::(); (MailMan { tx, rx: rx1 }, MailMan { tx: tx1, rx }) } pub fn send(&self, mail: T) -> Result<(), Box> { self.tx.send(mail).unwrap(); Ok(()) } pub fn recv(&self) -> Result> { let u = self.rx.recv()?; Ok(u) } } #[allow(unused_variables)] impl Controller

{ pub fn start(config_path: T) -> Result> where std::path::PathBuf: std::convert::From, P: Player, { let config_path = PathBuf::from(config_path); let config = Config::read_file(config_path)?; let uuid = config.libraries.get_default()?.uuid; let config_ = Arc::new(RwLock::from(config)); let library = MusicLibrary::init(config_.clone(), uuid)?; Ok(Controller { queue: Queue::default(), config: config_.clone(), library, player: Box::new(P::new()?), }) } pub fn q_add(&mut self, item: &Uuid, source: super::queue::PlayerLocation, by_human: bool) { let item = self.library.query_uuid(item).unwrap().0.to_owned(); self.queue.add_item(item, source, by_human) } } #[cfg(test)] mod test_super {}