]> git.r.bdr.sh - rbdr/lyricli/blob - src/sources/mod.rs
Run lint / format
[rbdr/lyricli] / src / sources / mod.rs
1 use std::io::{Error, ErrorKind::Other, Result};
2
3 #[cfg(target_os = "macos")]
4 mod apple_music;
5 #[cfg(target_os = "macos")]
6 mod spotify;
7
8 #[cfg(target_os = "linux")]
9 mod dbus;
10
11 #[cfg(target_os = "macos")]
12 use apple_music::AppleMusic;
13 #[cfg(target_os = "macos")]
14 use spotify::Spotify;
15
16 #[cfg(target_os = "linux")]
17 use dbus::Dbus;
18
19 use crate::Track;
20
21 pub trait LyricsSource {
22 fn name(&self) -> String;
23
24 fn current_track(&self) -> Option<Track>;
25
26 fn disable(&self) -> Result<()>;
27 fn enable(&self) -> Result<()>;
28 fn reset(&self) -> Result<()>;
29 }
30
31 pub fn list() -> Vec<String> {
32 available_sources()
33 .into_iter()
34 .map(|source| source.name())
35 .collect()
36 }
37
38 pub fn enable(source_name: &String) -> Result<()> {
39 let sources = available_sources();
40 for source in sources {
41 if &source.name() == source_name {
42 return source.enable();
43 }
44 }
45 Err(Error::new(Other, "No such source was available."))
46 }
47
48 pub fn disable(source_name: &String) -> Result<()> {
49 let sources = available_sources();
50 for source in sources {
51 if &source.name() == source_name {
52 return source.disable();
53 }
54 }
55 Err(Error::new(Other, "No such source was available."))
56 }
57
58 pub fn reset(source_name: &String) -> Result<()> {
59 let sources = available_sources();
60 for source in sources {
61 if &source.name() == source_name {
62 return source.reset();
63 }
64 }
65 Err(Error::new(Other, "No such source was available."))
66 }
67
68 pub fn get_track() -> Option<Track> {
69 let sources = available_sources();
70 for source in sources {
71 if let Some(track) = source.current_track() {
72 return Some(track);
73 }
74 }
75 return None;
76 }
77
78 pub fn available_sources() -> Vec<Box<dyn LyricsSource>> {
79 let mut sources: Vec<Box<dyn LyricsSource>> = Vec::new();
80 #[cfg(target_os = "macos")]
81 {
82 sources.push(Box::new(AppleMusic::new()));
83 sources.push(Box::new(Spotify::new()));
84 }
85
86 #[cfg(not(target_os = "macos"))]
87 {
88 sources.push(Box::new(Dbus::new()));
89 }
90
91 sources
92 }