]> git.r.bdr.sh - rbdr/lyricli/blame_incremental - src/sources/mod.rs
Update dependencies
[rbdr/lyricli] / src / sources / mod.rs
... / ...
CommitLineData
1use std::io::{Error, ErrorKind::Other, Result};
2
3#[cfg(target_os = "macos")]
4mod apple_music;
5#[cfg(target_os = "macos")]
6mod spotify;
7
8#[cfg(target_os = "linux")]
9mod dbus;
10
11#[cfg(target_os = "macos")]
12use apple_music::AppleMusic;
13#[cfg(target_os = "macos")]
14use spotify::Spotify;
15
16#[cfg(target_os = "linux")]
17use dbus::Dbus;
18
19use crate::Track;
20
21pub 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
31pub fn list() -> Vec<String> {
32 available_sources()
33 .into_iter()
34 .map(|source| source.name())
35 .collect()
36}
37
38pub 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
48pub 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
58pub 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
68pub 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 None
76}
77
78pub 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}