1
use std::collections::HashMap;
2

            
3
use serde::{Deserialize, Serialize};
4

            
5
use crate::{
6
    commands::{CommandResponse, ResponseParserError, single_item_command_request},
7
    response_tokenizer::{GenericResponseValue, ResponseAttributes},
8
    types::Uri,
9
};
10

            
11
single_item_command_request!(ReadComments, "readcomments", Uri);
12

            
13
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14
pub struct ReadCommentsResponse(HashMap<String, String>);
15

            
16
impl ReadCommentsResponse {
17
    pub fn new(comments: HashMap<String, String>) -> Self {
18
        ReadCommentsResponse(comments)
19
    }
20
}
21

            
22
impl CommandResponse for ReadCommentsResponse {
23
    type Request = ReadCommentsRequest;
24

            
25
    fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
26
        let parts: HashMap<_, _> = parts.into_map()?;
27

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

            
38
        Ok(ReadCommentsResponse(comments))
39
    }
40

            
41
    fn serialize(&self) -> Vec<u8> {
42
        unimplemented!("response serialization is not yet implemented for ReadCommentsResponse")
43
    }
44
}