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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use serde::de::{Deserialize, Deserializer};
use serde_json;
use std::result;
use query::Query;
use {Client, Result, Song};
/// A wrapper on a `Client` to control just the jukebox.
///
/// Any method invoked on a `Jukebox` will fail if the user that created the
/// `Client` is not authorized to control the jukebox.
#[derive(Debug)]
pub struct Jukebox<'a> {
client: &'a Client,
}
/// A representation of the jukebox's current status.
#[derive(Debug, Deserialize)]
pub struct JukeboxStatus {
/// Current index in the playlist (zero-indexed). `-1` means that the
/// jukebox has had its playlist cleared and has not since been played.
#[serde(rename = "currentIndex")]
pub index: isize,
/// Whether or not the jukebox is currently active.
pub playing: bool,
/// Volume level of the jukebox, from `0` to `1.0`.
#[serde(rename = "gain")]
pub volume: f32,
pub position: usize,
}
/// A more detailed representation of the jukebox's status. Includes its
/// current playlist.
#[derive(Debug)]
pub struct JukeboxPlaylist {
/// The jukebox's status.
pub status: JukeboxStatus,
/// The jukebox's current playlist.
pub songs: Vec<Song>,
}
impl<'de> Deserialize<'de> for JukeboxPlaylist {
fn deserialize<D>(de: D) -> result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct _Playlist {
#[serde(rename = "currentIndex")]
index: isize,
playing: bool,
gain: f32,
position: usize,
entry: Vec<Song>,
}
let raw = _Playlist::deserialize(de)?;
Ok(JukeboxPlaylist {
status: JukeboxStatus {
index: raw.index,
playing: raw.playing,
volume: raw.gain,
position: raw.position,
},
songs: raw.entry,
})
}
}
impl<'a> Jukebox<'a> {
/// Creates a new handler to the jukebox of the client.
pub fn start(client: &'a Client) -> Jukebox {
Jukebox { client }
}
fn send_action_with<U>(&self, action: &str, index: U, ids: &[usize]) -> Result<JukeboxStatus>
where
U: Into<Option<usize>>,
{
let args = Query::with("action", action)
.arg("index", index.into())
.arg_list("id", ids)
.build();
let res = self.client.get("jukeboxControl", args)?;
Ok(serde_json::from_value(res)?)
}
fn send_action(&self, action: &str) -> Result<JukeboxStatus> {
self.send_action_with(action, None, &[])
}
/// Returns the current playlist of the jukebox, as well as its status. The
/// status is also returned as it contains the position of the jukebox
/// in its playlist.
pub fn playlist(&self) -> Result<JukeboxPlaylist> {
let res = self
.client
.get("jukeboxControl", Query::with("action", "get"))?;
Ok(serde_json::from_value::<JukeboxPlaylist>(res)?)
}
/// Returns the status of the jukebox.
pub fn status(&self) -> Result<JukeboxStatus> {
self.send_action("status")
}
/// Tells the jukebox to start playing.
pub fn play(&self) -> Result<JukeboxStatus> {
self.send_action("start")
}
/// Tells the jukebox to pause playback.
pub fn stop(&self) -> Result<JukeboxStatus> {
self.send_action("stop")
}
/// Moves the jukebox's currently playing song to the provided index
/// (zero-indexed).
///
/// Using an index outside the range of the jukebox playlist will play the
/// last song in the playlist.
pub fn skip_to(&self, n: usize) -> Result<JukeboxStatus> {
self.send_action_with("skip", n, &[])
}
/// Adds the song to the jukebox's playlist.
pub fn add(&self, song: &Song) -> Result<JukeboxStatus> {
self.send_action_with("add", None, &[song.id as usize])
}
/// Adds a song matching the provided ID to the playlist.
///
/// # Errors
///
/// The method will return an error if a song matching the provided ID
/// cannot be found.
pub fn add_id(&self, id: usize) -> Result<JukeboxStatus> {
self.send_action_with("add", None, &[id])
}
/// Adds all the songs to the jukebox's playlist.
pub fn add_all(&self, songs: &[Song]) -> Result<JukeboxStatus> {
self.send_action_with(
"add",
None,
&songs.iter().map(|s| s.id as usize).collect::<Vec<_>>(),
)
}
/// Adds multiple songs matching the provided IDs to the playlist.
///
/// # Errors
///
/// The method will return an error if at least one ID cannot be matched to
/// a song.
pub fn add_all_ids(&self, ids: &[usize]) -> Result<JukeboxStatus> {
self.send_action_with("add", None, ids)
}
/// Clears the jukebox's playlist.
pub fn clear(&self) -> Result<JukeboxStatus> {
self.send_action("clear")
}
/// Removes the song at the provided index from the playlist.
pub fn remove_id(&self, idx: usize) -> Result<JukeboxStatus> {
self.send_action_with("remove", idx, &[])
}
/// Shuffles the jukebox's playlist.
pub fn shuffle(&self) -> Result<JukeboxStatus> {
self.send_action("shuffle")
}
/// Sets the jukebox's playback volume.
///
/// Seting the volume above `1.0` will have no effect.
pub fn set_volume(&self, volume: f32) -> Result<JukeboxStatus> {
let args = Query::with("action", "setGain").arg("gain", volume).build();
let res = self.client.get("jukeboxControl", args)?;
Ok(serde_json::from_value(res)?)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_playlist() {
let parsed = serde_json::from_str::<JukeboxPlaylist>(
r#"{
"currentIndex" : 0,
"playing" : false,
"gain" : 0.75,
"position" : 0,
"entry" : [ {
"id" : "1887",
"parent" : "1880",
"isDir" : false,
"title" : "トリコリコPLEASE!!",
"album" : "トリコリコPLEASE!!",
"artist" : "AZALEA",
"track" : 1,
"year" : 2016,
"coverArt" : "1880",
"size" : 33457239,
"contentType" : "audio/flac",
"suffix" : "flac",
"transcodedContentType" : "audio/ogg",
"transcodedSuffix" : "ogg",
"duration" : 227,
"bitRate" : 1090,
"path" : "A/AZALEA/トリコリコPLEASE!!/01 トリコリコPLEASE!!.flac",
"isVideo" : false,
"playCount" : 34,
"discNumber" : 1,
"created" : "2018-01-01T10:30:10.000Z",
"albumId" : "260",
"artistId" : "147",
"type" : "music"
}, {
"id" : "1888",
"parent" : "1880",
"isDir" : false,
"title" : "ときめき分類学",
"album" : "トリコリコPLEASE!!",
"artist" : "AZALEA",
"track" : 2,
"year" : 2016,
"coverArt" : "1880",
"size" : 40146569,
"contentType" : "audio/flac",
"suffix" : "flac",
"transcodedContentType" : "audio/ogg",
"transcodedSuffix" : "ogg",
"duration" : 291,
"bitRate" : 1033,
"path" : "A/AZALEA/トリコリコPLEASE!!/02 ときめき分類学.flac",
"isVideo" : false,
"playCount" : 14,
"discNumber" : 1,
"created" : "2018-01-01T10:30:10.000Z",
"albumId" : "260",
"artistId" : "147",
"type" : "music"
} ]
}"#,
)
.unwrap();
assert_eq!(parsed.songs.len(), 2);
assert!(!parsed.status.playing);
assert_eq!(parsed.status.volume, 0.75);
}
}