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

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

            
9
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10
pub struct SwapIdRequest {
11
    pub songid1: SongId,
12
    pub songid2: SongId,
13
}
14

            
15
impl SwapIdRequest {
16
    pub fn new(songid1: SongId, songid2: SongId) -> Self {
17
        Self { songid1, songid2 }
18
    }
19
}
20

            
21
impl CommandRequest for SwapIdRequest {
22
    type Response = SwapIdResponse;
23

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

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

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

            
51
        Self::throw_if_too_many_arguments(parts)?;
52

            
53
        Ok(SwapIdRequest { songid1, songid2 })
54
    }
55
}
56

            
57
empty_command_response!(SwapId);