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

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

            
9
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10
pub struct MoveRequest {
11
    pub from_or_range: OneOrRange,
12
    pub to: AbsouluteRelativeSongPosition,
13
}
14

            
15
impl MoveRequest {
16
    pub fn new(from_or_range: OneOrRange, to: AbsouluteRelativeSongPosition) -> Self {
17
        Self { from_or_range, to }
18
    }
19
}
20

            
21
impl CommandRequest for MoveRequest {
22
    type Response = MoveResponse;
23

            
24
    const COMMAND: &'static str = "move";
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.from_or_range, self.to)
30
    }
31

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

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

            
52
        Self::throw_if_too_many_arguments(parts)?;
53

            
54
        Ok(MoveRequest { from_or_range, to })
55
    }
56
}
57

            
58
empty_command_response!(Move);