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

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

            
9
pub struct SwapId;
10

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

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

            
23
impl CommandRequest for SwapIdRequest {
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);
58

            
59
impl Command for SwapId {
60
    type Request = SwapIdRequest;
61
    type Response = SwapIdResponse;
62
}