1
use crate::{
2
    commands::{
3
        Command, GenericResponseValue, Request, RequestParserError, RequestParserResult,
4
        ResponseAttributes, ResponseParserError,
5
    },
6
    common::PlaylistName,
7
};
8

            
9
pub struct ListPlaylist;
10

            
11
pub type ListPlaylistResponse = Vec<PlaylistName>;
12

            
13
impl Command for ListPlaylist {
14
    type Response = ListPlaylistResponse;
15
    const COMMAND: &'static str = "listplaylist";
16

            
17
    fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
18
        let name = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
19
        let name = name
20
            .parse::<PlaylistName>()
21
            .map_err(|_| RequestParserError::SyntaxError(0, name.to_owned()))?;
22

            
23
        let range = parts
24
            .next()
25
            .map(|s| {
26
                s.parse()
27
                    .map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))
28
            })
29
            .transpose()?;
30

            
31
        debug_assert!(parts.next().is_none());
32

            
33
        Ok((Request::ListPlaylist(name.to_string(), range), ""))
34
    }
35

            
36
    fn parse_response(
37
        parts: ResponseAttributes<'_>,
38
    ) -> Result<Self::Response, ResponseParserError> {
39
        let parts: Vec<_> = parts.into();
40
        parts
41
            .into_iter()
42
            .map(|(name, value)| {
43
                debug_assert_eq!(name, "file");
44
                match value {
45
                    GenericResponseValue::Text(value) => Ok(value.to_string()),
46
                    GenericResponseValue::Binary(_) => Err(
47
                        ResponseParserError::UnexpectedPropertyType("file", "Binary"),
48
                    ),
49
                }
50
            })
51
            .collect::<Result<Vec<_>, _>>()
52
    }
53
}