//! 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 std::thread::spawn; use thiserror::Error; use crossbeam_channel::unbounded; use std::error::Error; use uuid::Uuid; use crate::config::ConfigError; use crate::music_player::player::{Player, PlayerCommand, PlayerError}; use crate::{ 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: Arc>, } #[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)?; let controller = Controller { queue: Queue::default(), config: config_.clone(), library, player: Arc::new(RwLock::new(P::new()?)), }; let player = Arc::clone(&controller.player); let controller_thread = spawn(move || { match player.read().unwrap().message_channel().recv().unwrap() { PlayerCommand::AboutToFinish => {}, PlayerCommand::EndOfStream => { player.write().unwrap().enqueue_next(todo!()); }, _ => {} } }); Ok(controller) } 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 { use crate::{config::tests::read_config_lib, music_player::gstreamer::GStreamer}; use super::Controller; #[test] fn construct_controller() { let config = read_config_lib(); let controller = Controller::::start("test-config/config_test.json").unwrap(); } }