]>
Commit | Line | Data |
---|---|---|
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; | |
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().into_iter().map(|source| source.name()).collect() | |
33 | } | |
34 | ||
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.")) | |
43 | } | |
44 | ||
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.")) | |
53 | } | |
54 | ||
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.")) | |
63 | } | |
64 | ||
65 | pub 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 | ||
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 | { | |
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 | } |