1
use std::collections::HashMap;
2

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

            
8
pub struct ReadComments;
9

            
10
pub type ReadCommentsResponse = HashMap<String, String>;
11

            
12
impl Command for ReadComments {
13
    type Response = ReadCommentsResponse;
14
    const COMMAND: &'static str = "readcomments";
15

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

            
22
        debug_assert!(parts.next().is_none());
23

            
24
        Ok((Request::ReadComments(uri), ""))
25
    }
26

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

            
32
        let comments = parts
33
            .iter()
34
            .map(|(k, v)| match v {
35
                GenericResponseValue::Text(s) => Ok((k.to_string(), s.to_string())),
36
                GenericResponseValue::Binary(_) => Err(ResponseParserError::SyntaxError(1, k)),
37
            })
38
            .collect::<Result<HashMap<_, _>, ResponseParserError>>()?;
39

            
40
        Ok(comments)
41
    }
42
}