1
use crate::commands::{
2
    Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
3
    ResponseParserError,
4
};
5

            
6
pub struct Commands;
7

            
8
pub type CommandsResponse = Vec<String>;
9

            
10
impl Command for Commands {
11
    type Response = CommandsResponse;
12
    const COMMAND: &'static str = "commands";
13

            
14
    fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
15
        debug_assert!(parts.next().is_none());
16
        Ok((Request::Commands, ""))
17
    }
18

            
19
    fn parse_response(
20
        parts: ResponseAttributes<'_>,
21
    ) -> Result<Self::Response, ResponseParserError> {
22
        let parts: Vec<_> = parts.into();
23
        let mut result = Vec::new();
24
        for (key, value) in parts.into_iter() {
25
            if key != "command" {
26
                return Err(ResponseParserError::UnexpectedProperty(key));
27
            }
28
            let value = match value {
29
                GenericResponseValue::Text(value) => value,
30
                GenericResponseValue::Binary(_) => {
31
                    return Err(ResponseParserError::UnexpectedPropertyType(
32
                        "handler", "Binary",
33
                    ));
34
                }
35
            };
36
            result.push(value.to_string());
37
        }
38
        Ok(result)
39
    }
40
}