]> git.r.bdr.sh - rbdr/lyricli/blame_incremental - src/sources/mod.rs
Remove the openssl stuff
[rbdr/lyricli] / src / sources / mod.rs
... / ...
CommitLineData
1use std::io::{Result, Error, ErrorKind::Other};
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().into_iter().map(|source| source.name()).collect()
33}
34
35pub fn enable(source_name: &String) -> Result<()> {
36 let sources = available_sources();
37 for source in sources {
38 if &source.name() == source_name {
39 return source.enable()
40 }
41 }
42 Err(Error::new(Other, "No such source was available."))
43}
44
45pub fn disable(source_name: &String) -> Result<()> {
46 let sources = available_sources();
47 for source in sources {
48 if &source.name() == source_name {
49 return source.disable()
50 }
51 }
52 Err(Error::new(Other, "No such source was available."))
53}
54
55pub fn reset(source_name: &String) -> Result<()> {
56 let sources = available_sources();
57 for source in sources {
58 if &source.name() == source_name {
59 return source.reset()
60 }
61 }
62 Err(Error::new(Other, "No such source was available."))
63}
64
65pub fn get_track() -> Option<Track> {
66 let sources = available_sources();
67 for source in sources {
68 if let Some(track) = source.current_track() {
69 return Some(track);
70 }
71 }
72 return None
73}
74
75pub fn available_sources() -> Vec<Box<dyn LyricsSource>> {
76 let mut sources: Vec<Box<dyn LyricsSource>> = Vec::new();
77 #[cfg(target_os = "macos")]
78 {
79 sources.push(Box::new(AppleMusic::new()));
80 sources.push(Box::new(Spotify::new()));
81 }
82
83 #[cfg(not(target_os = "macos"))]
84 {
85 sources.push(Box::new(Dbus::new()));
86 }
87
88 sources
89}