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

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

            
9
pub struct Move;
10

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

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

            
23
impl CommandRequest for MoveRequest {
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);
59

            
60
impl Command for Move {
61
    type Request = MoveRequest;
62
    type Response = MoveResponse;
63
}