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
use async_net::TcpStream;
use futures_lite::{io::AsyncBufReadExt, io::BufReader, StreamExt};
use itertools::Itertools;
use serde::Serialize;

use crate::client::respmap::RespMap;
use crate::{Directory, Playlist, Subsystem, Track, State};
use crate::{Stats, Status};
use std::str::FromStr;

impl FromStr for Subsystem {
    type Err = crate::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let r = match s {
            "partitions" => Subsystem::Partitions,
            "player" => Subsystem::Player,
            "mixer" => Subsystem::Mixer,
            "options" => Subsystem::Options,
            "update" => Subsystem::Update,
            "storedplaylist" => Subsystem::StoredPlaylist,
            "output" => Subsystem::Output,
            _ => return Err(crate::Error::ValueError { msg: s.into() }),
        };
        Ok(r)
    }
}

impl FromStr for State {
    type Err = crate::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let status = match s {
            "play" => State::Play,
            "pause" => State::Pause,
            "stop" => State::Stop,
            _ => return Err(crate::Error::ValueError { msg: s.into()}),
        };
        Ok(status)
    }
}


#[derive(Serialize, Debug)]
/// Response from commands that returns entries with metadata and tags
pub enum MixedResponse {
    File(Track),
    Directory(Directory),
    Playlist(Playlist),
}

impl MixedResponse {
    /// Try to convert to Track
    pub fn track(&self) -> Option<&Track> {
        match self {
            MixedResponse::File(t) => Some(t),
            _ => None,
        }
    }
    /// Try to convert to Directory
    pub fn directory(&self) -> Option<&Directory> {
        match self {
            MixedResponse::Directory(d) => Some(d),
            _ => None,
        }
    }

    pub fn playlist(&self) -> Option<&Playlist> {
        if let MixedResponse::Playlist(playlist) = self {
            Some(playlist)
        } else {
            None
        }
    }
}

pub(crate) async fn tracks(stream: &mut BufReader<TcpStream>) -> std::io::Result<Vec<Track>> {
    Ok(mixed_stream(stream)
        .await?
        .iter()
        .filter_map(MixedResponse::track)
        .cloned()
        .collect())
}

impl From<RespMap> for Directory {
    fn from(mut map: RespMap) -> Self {
        let dir = Directory {
            path: map.get_def("directory"),
            last_modified: map.get("Last-Modified"),
        };

        if !map.is_empty() {
            log::warn!("Status map not empty: {:?}", map.inner);
        }

        dir
    }
}

impl From<RespMap> for Playlist {
    fn from(mut map: RespMap) -> Self {
        let playlist = Playlist {
            path: map.get_def("playlist"),
            last_modified: map.get("Last-Modified"),
        };

        if !map.is_empty() {
            log::warn!("Status map not empty: {:?}", map.inner);
        }

        playlist
    }
}

impl From<RespMap> for MixedResponse {
    fn from(map: RespMap) -> Self {
        if map.contains_key("directory") {
            MixedResponse::Directory(Directory::from(map))
        } else if map.contains_key("playlist") {
            MixedResponse::Playlist(Playlist::from(map))
        } else {
            MixedResponse::File(Track::from(map))
        }
    }
}

pub async fn mixed_stream(
    stream: &mut BufReader<TcpStream>,
) -> std::io::Result<Vec<MixedResponse>> {
    let mut resvec = Vec::new();
    let mut map = RespMap::new();
    let mut lines = stream.lines();

    while let Some(line) = lines.next().await {
        let line = line?;
        let line = line.trim();

        if line == "OK" {
            // We're done
            resvec.push(MixedResponse::from(map));
            break;
        }

        if !map.is_empty()
            && (line.starts_with("directory:")
                || line.starts_with("file:")
                || line.starts_with("playlist:"))
        {
            // Add the previous record to the result vec
            resvec.push(MixedResponse::from(map));
            // Open a new record
            map = RespMap::new();
        }

        if let Some((k, v)) = line.splitn(2, ": ").next_tuple() {
            map.insert(k, v);
        }
    }

    Ok(resvec)
}

impl From<RespMap> for Track {
    fn from(mut map: RespMap) -> Self {
        let track = Track {
            file: map.get_def("file"),
            artist_sort: map.get("ArtistSort"),
            album_artist: map.get("AlbumArtist"),
            album_sort: map.get("AlbumSort"),
            album_artist_sort: map.get("AlbumArtistSort"),
            performer: map.get_vec("Performer"),
            genre: map.get("Genre"),
            title: map.get("Title"),
            track: map.get("Track"),
            album: map.get("Album"),
            artist: map.get("Artist"),
            pos: map.get("Pos"),
            id: map.get("Id"),
            last_modified: map.get("Last-Modified"),
            original_date: map.get("OriginalDate"),
            time: map.get("Time"),
            format: map.get("Format"),
            duration: map.as_duration_def("duration"),
            label: map.get("Label"),
            date: map.get("Date"),
            disc: map.get("Disc"),
            musicbraiz_trackid: map.get("MUSICBRAINZ_TRACKID"),
            musicbrainz_albumid: map.get("MUSICBRAINZ_ALBUMID"),
            musicbrainz_albumartistid: map.get("MUSICBRAINZ_ALBUMARTISTID"),
            musicbrainz_artistid: map.get("MUSICBRAINZ_ARTISTID"),
            musicbraiz_releasetrackid: map.get("MUSICBRAINZ_RELEASETRACKID"),
            musicbraiz_workid: map.get("MUSICBRAINZ_WORKID"),
            composer: map.get_vec("Composer"),
        };

        if !map.is_empty() {
            log::warn!("Track map not empty: {:?}", map.inner);
        }

        track
    }
}

impl From<RespMap> for Status {
    fn from(mut map: RespMap) -> Self {
        let status = Status {
            partition: map.get("partition"),
            volume: map.get("volume"),
            repeat: map.as_bool("repeat"),
            random: map.as_bool("random"),
            single: map.get_def("single"),
            consume: map.as_bool("consume"),
            playlist: map.get_def("playlist"),
            playlistlength: map.get_def("playlistlength"),
            song: map.get("song"),
            songid: map.get("songid"),
            nextsong: map.get("nextsong"),
            nextsongid: map.get("nextsongid"),
            time: map.get("time"),
            elapsed: map.as_duration("elapsed"),
            duration: map.as_duration("duration"),
            mixrampdb: map.get_def("mixrampdb"),
            mixrampdelay: map.get("mixrampdelay"),
            state: map.get_def("state"),
            bitrate: map.get("bitrate"),
            xfade: map.get("xfade"),
            audio: map.get("audio"),
            updating_db: map.get("updating_db"),
            error: map.get("error"),
        };

        if !map.is_empty() {
            log::warn!("Status map not empty: {:?}", map.inner);
        }

        status
    }
}

impl From<RespMap> for Stats {
    fn from(mut map: RespMap) -> Self {
        let stats = Stats {
            uptime: map.as_duration_def("uptime"),
            playtime: map.as_duration_def("playtime"),
            artists: map.get_def("artists"),
            albums: map.get_def("albums"),
            songs: map.get_def("songs"),
            db_playtime: map.as_duration_def("db_playtime"),
            db_update: map.get_def("db_update"),
        };

        if !map.is_empty() {
            log::warn!("Status map not empty: {:?}", map.inner);
        }
        stats
    }
}

#[cfg(test)]
mod test {
    use crate::client::respmap::RespMap;
    use crate::{State, Status};
    use std::time::Duration;

    #[test]
    fn parse_status() {
        let input = r#"\
volume: 50
repeat: 1
random: 1
single: 0
consume: 0
playlist: 2
playlistlength: 141
mixrampdb: 0.000000
state: play
song: 1
songid: 2
time: 149:308
elapsed: 149.029
bitrate: 878
duration: 307.760
audio: 44100:16:2
nextsong: 124
nextsongid: 125
"#;

        let reference = Status {
            partition: None,
            volume: Some(50),
            repeat: true,
            random: true,
            single: "0".into(),
            consume: false,
            playlist: 2,
            playlistlength: 141,
            song: Some(1),
            songid: Some(2),
            nextsong: Some(124),
            nextsongid: Some(125),
            time: Some("149:308".into()),
            elapsed: Some(Duration::from_secs_f64(149.029)),
            duration: Some(Duration::from_secs_f64(307.76)),
            mixrampdb: 0.0,
            mixrampdelay: None,
            state: State::Play,
            bitrate: Some(878),
            xfade: None,
            audio: Some("44100:16:2".into()),
            updating_db: None,
            error: None,
        };

        let parsed = Status::from(RespMap::from_string(input.into()));
        assert_eq!(parsed, reference);
    }
}