]>
Commit | Line | Data |
---|---|---|
4064639d | 1 | use std::io::{Error, ErrorKind::Other, Result}; |
44e7b4de RBR |
2 | |
3 | #[cfg(target_os = "macos")] | |
4 | mod apple_music; | |
5 | #[cfg(target_os = "macos")] | |
6 | mod spotify; | |
738ec06d | 7 | |
040b91a7 RBR |
8 | #[cfg(target_os = "linux")] |
9 | mod dbus; | |
738ec06d | 10 | |
44e7b4de RBR |
11 | #[cfg(target_os = "macos")] |
12 | use apple_music::AppleMusic; | |
13 | #[cfg(target_os = "macos")] | |
14 | use spotify::Spotify; | |
738ec06d | 15 | |
040b91a7 RBR |
16 | #[cfg(target_os = "linux")] |
17 | use dbus::Dbus; | |
738ec06d RBR |
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 | ||
44e7b4de | 31 | pub fn list() -> Vec<String> { |
4064639d RBR |
32 | available_sources() |
33 | .into_iter() | |
34 | .map(|source| source.name()) | |
35 | .collect() | |
738ec06d RBR |
36 | } |
37 | ||
44e7b4de RBR |
38 | pub fn enable(source_name: &String) -> Result<()> { |
39 | let sources = available_sources(); | |
40 | for source in sources { | |
41 | if &source.name() == source_name { | |
4064639d | 42 | return source.enable(); |
44e7b4de RBR |
43 | } |
44 | } | |
45 | Err(Error::new(Other, "No such source was available.")) | |
738ec06d RBR |
46 | } |
47 | ||
44e7b4de RBR |
48 | pub fn disable(source_name: &String) -> Result<()> { |
49 | let sources = available_sources(); | |
50 | for source in sources { | |
51 | if &source.name() == source_name { | |
4064639d | 52 | return source.disable(); |
44e7b4de RBR |
53 | } |
54 | } | |
55 | Err(Error::new(Other, "No such source was available.")) | |
738ec06d RBR |
56 | } |
57 | ||
44e7b4de RBR |
58 | pub fn reset(source_name: &String) -> Result<()> { |
59 | let sources = available_sources(); | |
60 | for source in sources { | |
61 | if &source.name() == source_name { | |
4064639d | 62 | return source.reset(); |
44e7b4de RBR |
63 | } |
64 | } | |
65 | Err(Error::new(Other, "No such source was available.")) | |
738ec06d RBR |
66 | } |
67 | ||
68 | pub fn get_track() -> Option<Track> { | |
44e7b4de RBR |
69 | let sources = available_sources(); |
70 | for source in sources { | |
71 | if let Some(track) = source.current_track() { | |
72 | return Some(track); | |
73 | } | |
74 | } | |
4064639d | 75 | return None; |
738ec06d RBR |
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 | { | |
44e7b4de RBR |
82 | sources.push(Box::new(AppleMusic::new())); |
83 | sources.push(Box::new(Spotify::new())); | |
738ec06d RBR |
84 | } |
85 | ||
86 | #[cfg(not(target_os = "macos"))] | |
87 | { | |
040b91a7 | 88 | sources.push(Box::new(Dbus::new())); |
738ec06d RBR |
89 | } |
90 | ||
91 | sources | |
92 | } |