]> git.r.bdr.sh - rbdr/lyricli/blame - src/sources/mod.rs
Add manpage
[rbdr/lyricli] / src / sources / mod.rs
CommitLineData
44e7b4de
RBR
1use std::io::{Result, Error, ErrorKind::Other};
2
3#[cfg(target_os = "macos")]
4mod apple_music;
5#[cfg(target_os = "macos")]
6mod spotify;
738ec06d 7
040b91a7
RBR
8#[cfg(target_os = "linux")]
9mod dbus;
738ec06d 10
44e7b4de
RBR
11#[cfg(target_os = "macos")]
12use apple_music::AppleMusic;
13#[cfg(target_os = "macos")]
14use spotify::Spotify;
738ec06d 15
040b91a7
RBR
16#[cfg(target_os = "linux")]
17use dbus::Dbus;
738ec06d
RBR
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
44e7b4de
RBR
31pub fn list() -> Vec<String> {
32 available_sources().into_iter().map(|source| source.name()).collect()
738ec06d
RBR
33}
34
44e7b4de
RBR
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."))
738ec06d
RBR
43}
44
44e7b4de
RBR
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."))
738ec06d
RBR
53}
54
44e7b4de
RBR
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."))
738ec06d
RBR
63}
64
65pub 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
75pub 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}