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

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

            
9
pub struct Swap;
10

            
11
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12
pub struct SwapRequest {
13
    pub songpos1: SongPosition,
14
    pub songpos2: SongPosition,
15
}
16

            
17
impl SwapRequest {
18
    pub fn new(songpos1: SongPosition, songpos2: SongPosition) -> Self {
19
        Self { songpos1, songpos2 }
20
    }
21
}
22

            
23
impl CommandRequest for SwapRequest {
24
    const COMMAND: &'static str = "swap";
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.songpos1, self.songpos2)
30
    }
31

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

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

            
51
        Self::throw_if_too_many_arguments(parts)?;
52

            
53
        Ok(SwapRequest { songpos1, songpos2 })
54
    }
55
}
56

            
57
empty_command_response!(Swap);
58

            
59
impl Command for Swap {
60
    type Request = SwapRequest;
61
    type Response = SwapResponse;
62
}