1
use crate::{
2
    commands::{Command, RequestParserError, ResponseParserError},
3
    request_tokenizer::RequestTokenizer,
4
    response_tokenizer::{ResponseAttributes, expect_property_type},
5
};
6

            
7
pub struct Commands;
8

            
9
pub type CommandsResponse = Vec<String>;
10

            
11
impl Command for Commands {
12
    type Request = ();
13
    type Response = CommandsResponse;
14
    const COMMAND: &'static str = "commands";
15

            
16
    fn serialize_request(&self, _request: Self::Request) -> String {
17
        Self::COMMAND.to_string()
18
    }
19

            
20
    fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
21
        debug_assert!(parts.next().is_none());
22
        Ok(())
23
    }
24

            
25
    fn parse_response(
26
        parts: ResponseAttributes<'_>,
27
    ) -> Result<Self::Response, ResponseParserError<'_>> {
28
        let parts: Vec<_> = parts.into_vec()?;
29
        let mut result = Vec::with_capacity(parts.len());
30
        for (key, value) in parts.into_iter() {
31
            if key != "command" {
32
                return Err(ResponseParserError::UnexpectedProperty(key));
33
            }
34
            result.push(expect_property_type!(Some(value), "key", Text).to_string());
35
        }
36
        Ok(result)
37
    }
38
}