1
use serde::{Deserialize, Serialize};
2

            
3
use crate::{
4
    commands::{Command, CommandRequest, RequestParserError, empty_command_response},
5
    request_tokenizer::RequestTokenizer,
6
    types::{Priority, WindowRange},
7
};
8

            
9
pub struct Prio;
10

            
11
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12
pub struct PrioRequest {
13
    pub prio: Priority,
14
    pub window: WindowRange,
15
}
16

            
17
impl PrioRequest {
18
    pub fn new(prio: Priority, window: WindowRange) -> Self {
19
        Self { prio, window }
20
    }
21
}
22

            
23
impl CommandRequest for PrioRequest {
24
    const COMMAND: &'static str = "prio";
25
    const MIN_ARGS: u32 = 2;
26
    const MAX_ARGS: Option<u32> = Some(2);
27

            
28
    fn serialize(&self) -> String {
29
        format!("{} {} {}\n", Self::COMMAND, self.prio, self.window)
30
    }
31

            
32
    fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
33
        let prio = parts.next().ok_or(Self::missing_arguments_error(0))?;
34
        let prio = prio
35
            .parse()
36
            .map_err(|_| RequestParserError::SubtypeParserError {
37
                argument_index: 0,
38
                expected_type: "Priority",
39
                raw_input: prio.to_string(),
40
            })?;
41

            
42
        let window = parts.next().ok_or(Self::missing_arguments_error(1))?;
43
        let window = window
44
            .parse()
45
            .map_err(|_| RequestParserError::SubtypeParserError {
46
                argument_index: 1,
47
                expected_type: "WindowRange",
48
                raw_input: window.to_string(),
49
            })?;
50

            
51
        Self::throw_if_too_many_arguments(parts)?;
52

            
53
        Ok(PrioRequest { prio, window })
54
    }
55
}
56

            
57
empty_command_response!(Prio);
58

            
59
impl Command for Prio {
60
    type Request = PrioRequest;
61
    type Response = PrioResponse;
62
}