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
101
102
103
104
105
#![warn(missing_docs)]
extern crate winapi;
extern crate kernel32;
extern crate reqwest;
extern crate time;
extern crate json;
#[cfg(windows)]
mod windows_process;
mod connector;
mod status;
#[cfg(windows)]
use windows_process::WindowsProcess;
use connector::{SpotifyConnector, InternalSpotifyError};
use status::SpotifyStatus;
use json::JsonValue;
type Result<T> = std::result::Result<T, SpotifyError>;
#[derive(Debug)]
pub enum SpotifyError {
InternalError(InternalSpotifyError),
ClientNotRunning,
WebHelperNotRunning,
}
#[allow(dead_code)]
pub struct Spotify {
connector: SpotifyConnector,
}
impl Spotify {
#[cfg(windows)]
pub fn new() -> Result<Spotify> {
if !Spotify::spotify_alive() {
return Err(SpotifyError::ClientNotRunning);
}
if !Spotify::spotify_webhelper_alive() {
return Err(SpotifyError::WebHelperNotRunning);
}
Spotify::new_unchecked()
}
#[cfg(not(windows))]
pub fn new() -> Result<Spotify> {
Spotify::new_unchecked()
}
fn new_unchecked() -> Result<Spotify> {
match SpotifyConnector::connect_new() {
Ok(result) => Ok(Spotify { connector: result }),
Err(error) => Err(SpotifyError::InternalError(error)),
}
}
pub fn get_status(&self) -> Result<SpotifyStatus> {
let json = match self.get_status_object() {
Ok(result) => result,
Err(error) => return Err(error),
};
Ok(SpotifyStatus::from(json))
}
fn get_status_object(&self) -> Result<JsonValue> {
match self.connector.fetch_status_json() {
Ok(result) => Ok(result),
Err(error) => Err(SpotifyError::InternalError(error)),
}
}
#[cfg(windows)]
fn spotify_alive() -> bool {
let process = "Spotify.exe";
WindowsProcess::find_by_name(process).is_some()
}
#[cfg(windows)]
fn spotify_webhelper_alive() -> bool {
let process = "SpotifyWebHelper.exe";
WindowsProcess::find_by_name(process).is_some()
}
}