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

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

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

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

            
21
impl CommandRequest for SwapRequest {
22
    type Response = SwapResponse;
23

            
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);