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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#![warn(missing_docs)]
#![deny(missing_debug_implementations, missing_copy_implementations, trivial_casts,
trivial_numeric_casts, unsafe_code, unstable_features, unused_import_braces,
unused_qualifications)]
#[allow(unused_imports)]
#[macro_use]
extern crate failure_derive;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate enum_kinds;
#[macro_use]
extern crate derive_is_enum_variant;
#[macro_use]
extern crate from_variants;
extern crate dbus;
mod generated;
mod extensions;
mod event;
mod find;
mod metadata;
mod player;
mod pooled_connection;
mod progress;
pub use find::{FindingError, PlayerFinder};
pub use metadata::{Metadata, Value as MetadataValue, ValueKind as MetadataValueKind};
pub use player::Player;
pub use progress::{Progress, ProgressTracker};
pub use event::Event;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[allow(missing_docs)]
pub enum PlaybackStatus {
Playing,
Paused,
Stopped,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum LoopStatus {
None,
Track,
Playlist,
}
#[derive(Fail, Debug)]
#[fail(display = "PlaybackStatus must be one of Playing, Paused, Stopped, but was {}", _0)]
pub struct InvalidPlaybackStatus(String);
impl ::std::str::FromStr for PlaybackStatus {
type Err = InvalidPlaybackStatus;
fn from_str(string: &str) -> Result<Self, Self::Err> {
use PlaybackStatus::*;
match string {
"Playing" => Ok(Playing),
"Paused" => Ok(Paused),
"Stopped" => Ok(Stopped),
other => Err(InvalidPlaybackStatus(other.to_string())),
}
}
}
#[derive(Fail, Debug)]
#[fail(display = "LoopStatus must be one of None, Track, Playlist, but was {}", _0)]
pub struct InvalidLoopStatus(String);
impl ::std::str::FromStr for LoopStatus {
type Err = InvalidLoopStatus;
fn from_str(string: &str) -> Result<Self, Self::Err> {
match string {
"None" => Ok(LoopStatus::None),
"Track" => Ok(LoopStatus::Track),
"Playlist" => Ok(LoopStatus::Playlist),
other => Err(InvalidLoopStatus(other.to_string())),
}
}
}
impl LoopStatus {
fn dbus_value(&self) -> String {
String::from(match *self {
LoopStatus::None => "None",
LoopStatus::Track => "Track",
LoopStatus::Playlist => "Playlist",
})
}
}
#[derive(Debug, Clone)]
pub struct TrackID<'a>(pub(crate) dbus::Path<'a>);
impl<'a, T> From<T> for TrackID<'a>
where
T: Into<dbus::Path<'a>>,
{
fn from(value: T) -> TrackID<'a> {
TrackID(value.into())
}
}
#[derive(Fail, Debug)]
#[fail(display = "D-Bus call failed: {}", message)]
pub struct DBusError {
message: String,
}
impl DBusError {
fn new<S: Into<String>>(message: S) -> Self {
DBusError {
message: message.into(),
}
}
}
impl From<dbus::Error> for DBusError {
fn from(error: dbus::Error) -> Self {
DBusError {
message: error
.message()
.unwrap_or("No error message present")
.to_string(),
}
}
}
impl From<InvalidPlaybackStatus> for DBusError {
fn from(error: InvalidPlaybackStatus) -> Self {
DBusError {
message: error.to_string(),
}
}
}
impl From<InvalidLoopStatus> for DBusError {
fn from(error: InvalidLoopStatus) -> Self {
DBusError {
message: error.to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
mod track_id {
use super::*;
#[test]
fn it_creates_track_ids() {
let _: TrackID = "/org/mpris/MediaPlayer2/TrackList/NoTrack".into();
let _: TrackID = TrackID::from("/org/mpris/MediaPlayer2/TrackList/NoTrack");
let _: TrackID =
TrackID::from(String::from("/org/mpris/MediaPlayer2/TrackList/NoTrack"));
}
}
}