1
use std::collections::HashMap;
2

            
3
use crate::commands::{
4
    Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
5
    ResponseParserError, get_and_parse_property,
6
};
7

            
8
pub struct PlaylistLength;
9

            
10
pub struct PlaylistLengthResponse {
11
    pub songs: u64,
12
    pub playtime: u64,
13
}
14

            
15
impl Command for PlaylistLength {
16
    type Response = PlaylistLengthResponse;
17
    const COMMAND: &'static str = "playlistlength";
18

            
19
    fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
20
        let playlist_name = parts
21
            .next()
22
            .ok_or(RequestParserError::UnexpectedEOF)?
23
            .to_string();
24

            
25
        debug_assert!(parts.next().is_none());
26

            
27
        Ok((Request::PlaylistLength(playlist_name), ""))
28
    }
29

            
30
    fn parse_response(
31
        parts: ResponseAttributes<'_>,
32
    ) -> Result<Self::Response, ResponseParserError> {
33
        let parts: HashMap<_, _> = parts.into();
34

            
35
        let songs = get_and_parse_property!(parts, "songs", Text);
36
        let playtime = get_and_parse_property!(parts, "playtime", Text);
37

            
38
        Ok(PlaylistLengthResponse { songs, playtime })
39
    }
40
}