blob: 087a87f4a6cfb553888320ea1784746d17f590d8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
use std::io::{Result, Error, ErrorKind::Other};
#[cfg(target_os = "macos")]
mod apple_music;
#[cfg(target_os = "macos")]
mod spotify;
#[cfg(target_os = "linux")]
mod dbus;
#[cfg(target_os = "macos")]
use apple_music::AppleMusic;
#[cfg(target_os = "macos")]
use spotify::Spotify;
#[cfg(target_os = "linux")]
use dbus::Dbus;
use crate::Track;
pub trait LyricsSource {
fn name(&self) -> String;
fn current_track(&self) -> Option<Track>;
fn disable(&self) -> Result<()>;
fn enable(&self) -> Result<()>;
fn reset(&self) -> Result<()>;
}
pub fn list() -> Vec<String> {
available_sources().into_iter().map(|source| source.name()).collect()
}
pub fn enable(source_name: &String) -> Result<()> {
let sources = available_sources();
for source in sources {
if &source.name() == source_name {
return source.enable()
}
}
Err(Error::new(Other, "No such source was available."))
}
pub fn disable(source_name: &String) -> Result<()> {
let sources = available_sources();
for source in sources {
if &source.name() == source_name {
return source.disable()
}
}
Err(Error::new(Other, "No such source was available."))
}
pub fn reset(source_name: &String) -> Result<()> {
let sources = available_sources();
for source in sources {
if &source.name() == source_name {
return source.reset()
}
}
Err(Error::new(Other, "No such source was available."))
}
pub fn get_track() -> Option<Track> {
let sources = available_sources();
for source in sources {
if let Some(track) = source.current_track() {
return Some(track);
}
}
return None
}
pub fn available_sources() -> Vec<Box<dyn LyricsSource>> {
let mut sources: Vec<Box<dyn LyricsSource>> = Vec::new();
#[cfg(target_os = "macos")]
{
sources.push(Box::new(AppleMusic::new()));
sources.push(Box::new(Spotify::new()));
}
#[cfg(not(target_os = "macos"))]
{
sources.push(Box::new(Dbus::new()));
}
sources
}
|