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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//! The module defines MPD status data structures

use std::str::FromStr;
use std::fmt;
use time::Duration;
use rustc_serialize::{Encodable, Encoder};

use error::{Error, ParseError};
use song::{Id, QueuePlace};
use convert::FromIter;

/// MPD status
#[derive(Debug, PartialEq, Clone, Default)]
pub struct Status {
    /// volume (0-100, or -1 if volume is unavailable (e.g. for HTTPD output type)
    pub volume: i8,
    /// repeat mode
    pub repeat: bool,
    /// random mode
    pub random: bool,
    /// single mode
    pub single: bool,
    /// consume mode
    pub consume: bool,
    /// queue version number
    pub queue_version: u32,
    /// queue length
    pub queue_len: u32,
    /// playback state
    pub state: State,
    /// currently playing song place in the queue
    pub song: Option<QueuePlace>,
    /// next song to play place in the queue
    pub nextsong: Option<QueuePlace>,
    /// time current song played, and total song duration (in seconds resolution)
    pub time: Option<(Duration, Duration)>,
    /// elapsed play time current song played (in milliseconds resolution)
    pub elapsed: Option<Duration>,
    /// current song duration
    pub duration: Option<Duration>,
    /// current song bitrate, kbps
    pub bitrate: Option<u32>,
    /// crossfade timeout, seconds
    pub crossfade: Option<Duration>,
    /// mixramp threshold, dB
    pub mixrampdb: f32,
    /// mixramp duration, seconds
    pub mixrampdelay: Option<Duration>,
    /// current audio playback format
    pub audio: Option<AudioFormat>,
    /// current DB updating job number (if DB updating is in progress)
    pub updating_db: Option<u32>,
    /// last player error (if happened, can be reset with `clearerror()` method)
    pub error: Option<String>,
    /// replay gain mode
    pub replaygain: Option<ReplayGain>,
}

impl Encodable for Status {
    fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
        e.emit_struct("Status", 21, |e| {
            e.emit_struct_field("volume", 0, |e| self.volume.encode(e))
             .and_then(|_| e.emit_struct_field("repeat", 1, |e| self.repeat.encode(e)))
             .and_then(|_| e.emit_struct_field("random", 2, |e| self.random.encode(e)))
             .and_then(|_| e.emit_struct_field("single", 3, |e| self.single.encode(e)))
             .and_then(|_| e.emit_struct_field("consume", 4, |e| self.consume.encode(e)))
             .and_then(|_| e.emit_struct_field("queue_version", 5, |e| self.queue_version.encode(e)))
             .and_then(|_| e.emit_struct_field("queue_len", 6, |e| self.queue_len.encode(e)))
             .and_then(|_| e.emit_struct_field("state", 7, |e| self.state.encode(e)))
             .and_then(|_| e.emit_struct_field("song", 8, |e| self.song.encode(e)))
             .and_then(|_| e.emit_struct_field("nextsong", 9, |e| self.nextsong.encode(e)))
             .and_then(|_| {
                 e.emit_struct_field("time", 10, |e| {
                     e.emit_option(|e| {
                         self.time
                             .map(|p| {
                                 e.emit_option_some(|e| {
                                     e.emit_tuple(2, |e| {
                                         e.emit_tuple_arg(0, |e| p.0.num_seconds().encode(e))
                                          .and_then(|_| e.emit_tuple_arg(1, |e| p.1.num_seconds().encode(e)))
                                     })
                                 })
                             })
                             .unwrap_or_else(|| e.emit_option_none())
                     })
                 })
             })
             .and_then(|_| {
                 e.emit_struct_field("elapsed", 11, |e| {
                     e.emit_option(|e| {
                         self.elapsed
                             .map(|d| e.emit_option_some(|e| d.num_seconds().encode(e)))
                             .unwrap_or_else(|| e.emit_option_none())
                     })
                 })
             })
             .and_then(|_| {
                 e.emit_struct_field("duration", 12, |e| {
                     e.emit_option(|e| {
                         self.duration
                             .map(|d| e.emit_option_some(|e| d.num_seconds().encode(e)))
                             .unwrap_or_else(|| e.emit_option_none())
                     })
                 })
             })
             .and_then(|_| e.emit_struct_field("bitrate", 13, |e| self.bitrate.encode(e)))
             .and_then(|_| {
                 e.emit_struct_field("crossfade", 14, |e| {
                     e.emit_option(|e| {
                         self.crossfade
                             .map(|d| e.emit_option_some(|e| d.num_seconds().encode(e)))
                             .unwrap_or_else(|| e.emit_option_none())
                     })
                 })
             })
             .and_then(|_| e.emit_struct_field("mixrampdb", 15, |e| self.mixrampdb.encode(e)))
             .and_then(|_| {
                 e.emit_struct_field("mixrampdelay", 16, |e| {
                     e.emit_option(|e| {
                         self.mixrampdelay
                             .map(|d| e.emit_option_some(|e| d.num_seconds().encode(e)))
                             .unwrap_or_else(|| e.emit_option_none())
                     })
                 })
             })
             .and_then(|_| e.emit_struct_field("audio", 17, |e| self.audio.encode(e)))
             .and_then(|_| e.emit_struct_field("updating_db", 18, |e| self.updating_db.encode(e)))
             .and_then(|_| e.emit_struct_field("error", 19, |e| self.error.encode(e)))
             .and_then(|_| e.emit_struct_field("replaygain", 20, |e| self.replaygain.encode(e)))
        })

    }
}

impl FromIter for Status {
    fn from_iter<I: Iterator<Item = Result<(String, String), Error>>>(iter: I) -> Result<Status, Error> {
        let mut result = Status::default();

        for res in iter {
            let line = try!(res);
            match &*line.0 {
                "volume" => result.volume = try!(line.1.parse()),

                "repeat" => result.repeat = &*line.1 == "1",
                "random" => result.random = &*line.1 == "1",
                "single" => result.single = &*line.1 == "1",
                "consume" => result.consume = &*line.1 == "1",

                "playlist" => result.queue_version = try!(line.1.parse()),
                "playlistlength" => result.queue_len = try!(line.1.parse()),
                "state" => result.state = try!(line.1.parse()),
                "songid" => {
                    match result.song {
                        None => {
                            result.song = Some(QueuePlace {
                                id: Id(try!(line.1.parse())),
                                pos: 0,
                                prio: 0,
                            })
                        }
                        Some(ref mut place) => place.id = Id(try!(line.1.parse())),
                    }
                }
                "song" => {
                    match result.song {
                        None => {
                            result.song = Some(QueuePlace {
                                pos: try!(line.1.parse()),
                                id: Id(0),
                                prio: 0,
                            })
                        }
                        Some(ref mut place) => place.pos = try!(line.1.parse()),
                    }
                }
                "nextsongid" => {
                    match result.nextsong {
                        None => {
                            result.nextsong = Some(QueuePlace {
                                id: Id(try!(line.1.parse())),
                                pos: 0,
                                prio: 0,
                            })
                        }
                        Some(ref mut place) => place.id = Id(try!(line.1.parse())),
                    }
                }
                "nextsong" => {
                    match result.nextsong {
                        None => {
                            result.nextsong = Some(QueuePlace {
                                pos: try!(line.1.parse()),
                                id: Id(0),
                                prio: 0,
                            })
                        }
                        Some(ref mut place) => place.pos = try!(line.1.parse()),
                    }
                }
                "time" => {
                    result.time = try!({
                        let mut splits = line.1.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),
                        }
                    })
                }
                // TODO" => float errors don't work on stable
                "elapsed" => {
                    result.elapsed = line.1
                                         .parse::<f32>()
                                         .ok()
                                         .map(|v| Duration::milliseconds((v * 1000.0) as i64))
                }
                "duration" => result.duration = Some(Duration::seconds(try!(line.1.parse()))),
                "bitrate" => result.bitrate = Some(try!(line.1.parse())),
                "xfade" => result.crossfade = Some(Duration::seconds(try!(line.1.parse()))),
                // "mixrampdb" => 0.0, //get_field!(map, "mixrampdb"),
                // "mixrampdelay" => None, //get_field!(map, opt "mixrampdelay").map(|v: f64| Duration::milliseconds((v * 1000.0) as i64)),
                "audio" => result.audio = Some(try!(line.1.parse())),
                "updating_db" => result.updating_db = Some(try!(line.1.parse())),
                "error" => result.error = Some(line.1.to_owned()),
                "replay_gain_mode" => result.replaygain = Some(try!(line.1.parse())),
                _ => (),
            }
        }

        Ok(result)
    }
}

/// Audio playback format
#[derive(Debug, Copy, Clone, PartialEq, RustcEncodable)]
pub struct AudioFormat {
    /// sample rate, kbps
    pub rate: u32,
    /// sample resolution in bits, can be 0 for floating point resolution
    pub bits: u8,
    /// number of channels
    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))),
        })
    }
}

/// Playback state
#[derive(Debug, Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
pub enum State {
    /// player stopped
    Stop,
    /// player is playing
    Play,
    /// player paused
    Pause,
}

impl Default for State {
    fn default() -> State {
        State::Stop
    }
}

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())),
        }
    }
}

/// Replay gain mode
#[derive(Debug, Clone, Copy, PartialEq, RustcEncodable, RustcDecodable)]
pub enum ReplayGain {
    /// off
    Off,
    /// track
    Track,
    /// album
    Album,
    /// auto
    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",
        })
    }
}