blob: cac58332120579a9c432e7867513cc01dd2b89d6 (
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
90
91
92
93
94
95
96
97
98
99
100
|
use crate::configuration::Configuration;
use std::io::{Error, Result};
#[cfg(target_os = "macos")]
mod apple_music;
#[cfg(target_os = "macos")]
mod spotify;
#[cfg(target_os = "macos")]
mod swinsian;
#[cfg(target_os = "linux")]
mod mpris;
#[cfg(target_os = "macos")]
use apple_music::AppleMusic;
#[cfg(target_os = "macos")]
use spotify::Spotify;
#[cfg(target_os = "macos")]
use swinsian::Swinsian;
#[cfg(target_os = "linux")]
use mpris::Mpris;
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::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::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::other("No such source was available."))
}
pub fn get_track(configuration: &Configuration) -> Option<Track> {
let sources = available_sources();
for source in sources {
if configuration.is_enabled(&source.name())
&& let Some(track) = source.current_track()
{
return Some(track);
}
}
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()));
sources.push(Box::new(Swinsian::new()));
}
#[cfg(not(target_os = "macos"))]
{
sources.push(Box::new(Mpris::new()));
}
sources
}
|