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

            
6
pub struct PauseRequest(Option<bool>);
7

            
8
impl PauseRequest {
9
    pub fn new(state: Option<bool>) -> Self {
10
        PauseRequest(state)
11
    }
12
}
13

            
14
impl CommandRequest for PauseRequest {
15
    type Response = PauseResponse;
16

            
17
    const COMMAND: &'static str = "pause";
18
    const MIN_ARGS: u32 = 0;
19
    const MAX_ARGS: Option<u32> = Some(1);
20

            
21
    fn serialize(&self) -> String {
22
        match self.0 {
23
            Some(true) => format!("{} 1\n", Self::COMMAND),
24
            Some(false) => format!("{} 0\n", Self::COMMAND),
25
            None => Self::COMMAND.to_string() + "\n",
26
        }
27
    }
28

            
29
    fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
30
        let result = match parts.next() {
31
            Some("0") => Ok(Some(false)),
32
            Some("1") => Ok(Some(true)),
33
            Some(s) => Err(RequestParserError::SubtypeParserError {
34
                argument_index: 0,
35
                expected_type: "Option<bool>",
36
                raw_input: s.to_owned(),
37
            }),
38
            None => Ok(None),
39
        };
40

            
41
        Self::throw_if_too_many_arguments(parts)?;
42

            
43
        result.map(PauseRequest)
44
    }
45
}
46

            
47
empty_command_response!(Pause);