use crate::configuration::Configuration; use std::io::{Error, Result}; #[cfg(target_os = "macos")] mod apple_music; #[cfg(target_os = "macos")] mod spotify; #[cfg(target_os = "macos")] mod swinsian; #[cfg(target_os = "linux")] mod mpris; #[cfg(target_os = "macos")] use apple_music::AppleMusic; #[cfg(target_os = "macos")] use spotify::Spotify; #[cfg(target_os = "macos")] use swinsian::Swinsian; #[cfg(target_os = "linux")] use mpris::Mpris; use crate::Track; pub trait LyricsSource { fn name(&self) -> String; fn current_track(&self) -> Option; fn disable(&self) -> Result<()>; fn enable(&self) -> Result<()>; fn reset(&self) -> Result<()>; } pub fn list() -> Vec { available_sources() .into_iter() .map(|source| source.name()) .collect() } pub fn enable(source_name: &String) -> Result<()> { let sources = available_sources(); for source in sources { if &source.name() == source_name { return source.enable(); } } Err(Error::other("No such source was available.")) } pub fn disable(source_name: &String) -> Result<()> { let sources = available_sources(); for source in sources { if &source.name() == source_name { return source.disable(); } } Err(Error::other("No such source was available.")) } pub fn reset(source_name: &String) -> Result<()> { let sources = available_sources(); for source in sources { if &source.name() == source_name { return source.reset(); } } Err(Error::other("No such source was available.")) } pub fn get_track(configuration: &Configuration) -> Option { let sources = available_sources(); for source in sources { if configuration.is_enabled(&source.name()) && let Some(track) = source.current_track() { return Some(track); } } None } pub fn available_sources() -> Vec> { let mut sources: Vec> = Vec::new(); #[cfg(target_os = "macos")] { sources.push(Box::new(AppleMusic::new())); sources.push(Box::new(Spotify::new())); sources.push(Box::new(Swinsian::new())); } #[cfg(not(target_os = "macos"))] { sources.push(Box::new(Mpris::new())); } sources }