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
use std::collections::BTreeMap;
use std::str::FromStr;
use std::convert::From;
use std::fmt;
use time::Duration;
use error::{Error, ProtoError, ParseError};
use song::{Id, QueuePlace};
#[derive(Debug, PartialEq, Clone)]
pub struct Status {
pub volume: i8,
pub repeat: bool,
pub random: bool,
pub single: bool,
pub consume: bool,
pub queue_version: u32,
pub queue_len: u32,
pub state: State,
pub song: Option<QueuePlace>,
pub nextsong: Option<QueuePlace>,
pub time: Option<(Duration, Duration)>,
pub elapsed: Option<Duration>,
pub duration: Option<Duration>,
pub bitrate: Option<u32>,
pub crossfade: Option<Duration>,
pub mixrampdb: f32,
pub mixrampdelay: Option<Duration>,
pub audio: Option<AudioFormat>,
pub updating_db: Option<u32>,
pub error: Option<String>,
pub replaygain: Option<ReplayGain>
}
impl Status {
pub fn from_map(map: BTreeMap<String, String>) -> Result<Status, Error> {
Ok(Status {
volume: get_field!(map, "volume"),
repeat: get_field!(map, bool "repeat"),
random: get_field!(map, bool "random"),
single: get_field!(map, bool "single"),
consume: get_field!(map, bool "consume"),
queue_version: get_field!(map, "playlist"),
queue_len: get_field!(map, "playlistlength"),
state: get_field!(map, "state"),
song: try!(map.get("song").map(|v| v.parse().map_err(ParseError::BadInteger)).and_then(|posres|
map.get("songid").map(|v| v.parse().map_err(ParseError::BadInteger)).map(|idres|
posres.and_then(|pos| idres.map(|id| Some(QueuePlace {
id: Id(id),
pos: pos,
prio: 0
}))))).unwrap_or(Ok(None))),
nextsong: try!(map.get("nextsong").map(|v| v.parse().map_err(ParseError::BadInteger)).and_then(|posres|
map.get("nextsongid").map(|v| v.parse().map_err(ParseError::BadInteger)).map(|idres|
posres.and_then(|pos| idres.map(|id| Some(QueuePlace {
id: Id(id),
pos: pos,
prio: 0
}))))).unwrap_or(Ok(None))),
time: try!(map.get("time").map(|time| {
let mut splits = time.splitn(2, ':').map(|v| v.parse().map_err(ParseError::BadInteger).map(Duration::seconds));
match (splits.next(), splits.next()) {
(Some(Ok(a)), Some(Ok(b))) => Ok(Some((a, b))),
(Some(Err(e)), _) | (_, Some(Err(e))) => Err(e),
_ => Ok(None)
}
}).unwrap_or(Ok(None))),
elapsed: map.get("elapsed").and_then(|f| f.parse::<f32>().ok()).map(|v| Duration::milliseconds((v * 1000.0) as i64)),
duration: get_field!(map, opt "duration").map(Duration::seconds),
bitrate: get_field!(map, opt "bitrate"),
crossfade: get_field!(map, opt "xfade").map(Duration::seconds),
mixrampdb: 0.0,
mixrampdelay: None,
audio: get_field!(map, opt "audio"),
updating_db: get_field!(map, opt "updating_db"),
error: map.get("error").map(|v| v.to_owned()),
replaygain: get_field!(map, opt "replay_gain_mode"),
})
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct AudioFormat {
pub rate: u32,
pub bits: u8,
pub chans: u8
}
impl FromStr for AudioFormat {
type Err = ParseError;
fn from_str(s: &str) -> Result<AudioFormat, ParseError> {
let mut it = s.split(':');
Ok(AudioFormat {
rate: try!(it.next().ok_or(ParseError::NoRate).and_then(|v| v.parse().map_err(ParseError::BadRate))),
bits: try!(it.next().ok_or(ParseError::NoBits).and_then(|v| if &*v == "f" { Ok(0) } else { v.parse().map_err(ParseError::BadBits) })),
chans: try!(it.next().ok_or(ParseError::NoChans).and_then(|v| v.parse().map_err(ParseError::BadChans))),
})
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum State {
Stop,
Play,
Pause
}
impl FromStr for State {
type Err = ParseError;
fn from_str(s: &str) -> Result<State, ParseError> {
match s {
"stop" => Ok(State::Stop),
"play" => Ok(State::Play),
"pause" => Ok(State::Pause),
_ => Err(ParseError::BadState(s.to_owned())),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ReplayGain {
Off,
Track,
Album,
Auto
}
impl FromStr for ReplayGain {
type Err = ParseError;
fn from_str(s: &str) -> Result<ReplayGain, ParseError> {
use self::ReplayGain::*;
match s {
"off" => Ok(Off),
"track" => Ok(Track),
"album" => Ok(Album),
"auto" => Ok(Auto),
_ => Err(ParseError::BadValue(s.to_owned()))
}
}
}
impl fmt::Display for ReplayGain {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::ReplayGain::*;
f.write_str(match *self {
Off => "off",
Track => "track",
Album => "album",
Auto => "auto",
})
}
}