]>
Commit | Line | Data |
---|---|---|
44e7b4de RBR |
1 | use std::io::{Result, Error, ErrorKind::Other}; |
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 RBR |
31 | pub fn list() -> Vec<String> { |
32 | available_sources().into_iter().map(|source| source.name()).collect() | |
738ec06d RBR |
33 | } |
34 | ||
44e7b4de RBR |
35 | pub 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.")) | |
738ec06d RBR |
43 | } |
44 | ||
44e7b4de RBR |
45 | pub 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.")) | |
738ec06d RBR |
53 | } |
54 | ||
44e7b4de RBR |
55 | pub 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.")) | |
738ec06d RBR |
63 | } |
64 | ||
65 | pub fn get_track() -> Option<Track> { | |
44e7b4de RBR |
66 | let sources = available_sources(); |
67 | for source in sources { | |
68 | if let Some(track) = source.current_track() { | |
69 | return Some(track); | |
70 | } | |
71 | } | |
738ec06d RBR |
72 | return None |
73 | } | |
74 | ||
75 | pub fn available_sources() -> Vec<Box<dyn LyricsSource>> { | |
76 | let mut sources: Vec<Box<dyn LyricsSource>> = Vec::new(); | |
77 | #[cfg(target_os = "macos")] | |
78 | { | |
44e7b4de RBR |
79 | sources.push(Box::new(AppleMusic::new())); |
80 | sources.push(Box::new(Spotify::new())); | |
738ec06d RBR |
81 | } |
82 | ||
83 | #[cfg(not(target_os = "macos"))] | |
84 | { | |
040b91a7 | 85 | sources.push(Box::new(Dbus::new())); |
738ec06d RBR |
86 | } |
87 | ||
88 | sources | |
89 | } |