]> git.r.bdr.sh - rbdr/lyricli/blame - src/sources/mod.rs
Add macos sources
[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
738ec06d
RBR
8// #[cfg(not(target_os = "macos"))]
9// mod rhythmbox;
10// #[cfg(not(target_os = "macos"))]
11// mod quod_libe;
12// #[cfg(not(target_os = "macos"))]
13// mod strawberry;
14// #[cfg(not(target_os = "macos"))]
15// mod tauon;
16
44e7b4de
RBR
17#[cfg(target_os = "macos")]
18use apple_music::AppleMusic;
19#[cfg(target_os = "macos")]
20use spotify::Spotify;
738ec06d
RBR
21
22// #[cfg(not(target_os = "macos"))]
23// use rhythmbox::Rhythmbox;
24// #[cfg(not(target_os = "macos"))]
25// use quod_libet::QuodLibet;
26// #[cfg(not(target_os = "macos"))]
27// use strawberry::Strawberry;
28// #[cfg(not(target_os = "macos"))]
29// use tauon::Tauon;
30
31use crate::Track;
32
33pub trait LyricsSource {
34 fn name(&self) -> String;
35
36 fn current_track(&self) -> Option<Track>;
37
38 fn disable(&self) -> Result<()>;
39 fn enable(&self) -> Result<()>;
40 fn reset(&self) -> Result<()>;
41}
42
44e7b4de
RBR
43pub fn list() -> Vec<String> {
44 available_sources().into_iter().map(|source| source.name()).collect()
738ec06d
RBR
45}
46
44e7b4de
RBR
47pub fn enable(source_name: &String) -> Result<()> {
48 let sources = available_sources();
49 for source in sources {
50 if &source.name() == source_name {
51 return source.enable()
52 }
53 }
54 Err(Error::new(Other, "No such source was available."))
738ec06d
RBR
55}
56
44e7b4de
RBR
57pub fn disable(source_name: &String) -> Result<()> {
58 let sources = available_sources();
59 for source in sources {
60 if &source.name() == source_name {
61 return source.disable()
62 }
63 }
64 Err(Error::new(Other, "No such source was available."))
738ec06d
RBR
65}
66
44e7b4de
RBR
67pub fn reset(source_name: &String) -> Result<()> {
68 let sources = available_sources();
69 for source in sources {
70 if &source.name() == source_name {
71 return source.reset()
72 }
73 }
74 Err(Error::new(Other, "No such source was available."))
738ec06d
RBR
75}
76
77pub fn get_track() -> Option<Track> {
44e7b4de
RBR
78 let sources = available_sources();
79 for source in sources {
80 if let Some(track) = source.current_track() {
81 return Some(track);
82 }
83 }
738ec06d
RBR
84 return None
85}
86
87pub fn available_sources() -> Vec<Box<dyn LyricsSource>> {
88 let mut sources: Vec<Box<dyn LyricsSource>> = Vec::new();
89 #[cfg(target_os = "macos")]
90 {
44e7b4de
RBR
91 sources.push(Box::new(AppleMusic::new()));
92 sources.push(Box::new(Spotify::new()));
738ec06d
RBR
93 }
94
95 #[cfg(not(target_os = "macos"))]
96 {
97 // sources.push(Box::new(Rhythmbox::new()));
98 // sources.push(Box::new(QuodLibet::new()));
99 // sources.push(Box::new(Strawberry::new()));
100 // sources.push(Box::new(Tauon::new()));
101 }
102
103 sources
104}