1
use std::collections::HashMap;
2

            
3
use crate::{
4
    commands::{
5
        get_and_parse_property, get_property, Command, Request, RequestParserError,
6
        RequestParserResult, ResponseAttributes, ResponseParserError,
7
    },
8
    common::Offset,
9
};
10

            
11
pub struct AlbumArt;
12

            
13
pub struct AlbumArtResponse {
14
    pub size: usize,
15
    pub binary: Vec<u8>,
16
}
17

            
18
impl Command for AlbumArt {
19
    type Response = AlbumArtResponse;
20
    const COMMAND: &'static str = "albumart";
21

            
22
    fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
23
        let uri = match parts.next() {
24
            Some(s) => s,
25
            None => return Err(RequestParserError::UnexpectedEOF),
26
        };
27

            
28
        let offset = match parts.next() {
29
            Some(s) => s
30
                .parse::<Offset>()
31
                .map_err(|_| RequestParserError::SyntaxError(1, s.to_owned()))?,
32
            None => return Err(RequestParserError::UnexpectedEOF),
33
        };
34

            
35
        debug_assert!(parts.next().is_none());
36

            
37
        Ok((Request::AlbumArt(uri.to_string(), offset), ""))
38
    }
39

            
40
    fn parse_response(
41
        parts: ResponseAttributes<'_>,
42
    ) -> Result<Self::Response, ResponseParserError> {
43
        let parts: HashMap<_, _> = parts.into();
44

            
45
        let size = get_and_parse_property!(parts, "size", Text);
46

            
47
        let binary = get_property!(parts, "binary", Binary).into();
48

            
49
        Ok(AlbumArtResponse { size, binary })
50
    }
51
}