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 into_request_enum(self) -> crate::Request {
29
        crate::Request::SwapId(self.songid1, self.songid2)
30
    }
31

            
32
    fn from_request_enum(request: crate::Request) -> Option<Self> {
33
        match request {
34
            crate::Request::SwapId(songid1, songid2) => Some(SwapIdRequest { songid1, songid2 }),
35
            _ => None,
36
        }
37
    }
38

            
39
    fn serialize(&self) -> String {
40
        format!("{} {} {}\n", Self::COMMAND, self.songid1, self.songid2)
41
    }
42

            
43
    fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
44
        let songid1 = parts.next().ok_or(Self::missing_arguments_error(0))?;
45
        let songid1 = songid1
46
            .parse()
47
            .map_err(|_| RequestParserError::SubtypeParserError {
48
                argument_index: 0,
49
                expected_type: "SongId",
50
                raw_input: songid1.to_string(),
51
            })?;
52

            
53
        let songid2 = parts.next().ok_or(Self::missing_arguments_error(1))?;
54
        let songid2 = songid2
55
            .parse()
56
            .map_err(|_| RequestParserError::SubtypeParserError {
57
                argument_index: 1,
58
                expected_type: "SongId",
59
                raw_input: songid2.to_string(),
60
            })?;
61

            
62
        Self::throw_if_too_many_arguments(parts)?;
63

            
64
        Ok(SwapIdRequest { songid1, songid2 })
65
    }
66
}
67

            
68
empty_command_response!(SwapId);
69

            
70
impl Command for SwapId {
71
    type Request = SwapIdRequest;
72
    type Response = SwapIdResponse;
73
}