1
use crate::{
2
    commands::{CommandRequest, RequestParserError, empty_command_response},
3
    request_tokenizer::RequestTokenizer,
4
    types::Feature,
5
};
6

            
7
pub struct ProtocolEnableRequest(Vec<Feature>);
8

            
9
impl ProtocolEnableRequest {
10
    pub fn new(features: Vec<Feature>) -> Self {
11
        ProtocolEnableRequest(features)
12
    }
13
}
14

            
15
impl CommandRequest for ProtocolEnableRequest {
16
    type Response = ProtocolEnableResponse;
17

            
18
    const COMMAND: &'static str = "protocol enable";
19
    const MIN_ARGS: u32 = 1;
20
    const MAX_ARGS: Option<u32> = None;
21

            
22
    fn serialize(&self) -> String {
23
        let features = self
24
            .0
25
            .iter()
26
            .map(|f| f.to_string())
27
            .collect::<Vec<String>>()
28
            .join(" ");
29
        format!("{} {}", Self::COMMAND, features)
30
    }
31

            
32
    fn parse(parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
33
        let mut parts = parts.peekable();
34
        if parts.peek().is_none() {
35
            return Err(Self::missing_arguments_error(0));
36
        }
37

            
38
        let features = parts
39
            .enumerate()
40
            .map(|(i, f)| {
41
                f.parse()
42
                    .map_err(|_| RequestParserError::SubtypeParserError {
43
                        argument_index: i.try_into().unwrap_or(u32::MAX),
44
                        expected_type: "Feature",
45
                        raw_input: f.to_owned(),
46
                    })
47
            })
48
            .collect::<Result<Vec<Feature>, RequestParserError>>()?;
49

            
50
        Ok(ProtocolEnableRequest(features))
51
    }
52
}
53

            
54
empty_command_response!(ProtocolEnable);