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

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

            
9
pub struct MoveId;
10

            
11
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12
pub struct MoveIdRequest {
13
    pub id: SongId,
14
    pub to: AbsouluteRelativeSongPosition,
15
}
16

            
17
impl MoveIdRequest {
18
    pub fn new(id: SongId, to: AbsouluteRelativeSongPosition) -> Self {
19
        Self { id, to }
20
    }
21
}
22

            
23
impl CommandRequest for MoveIdRequest {
24
    const COMMAND: &'static str = "moveid";
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::MoveId(self.id, self.to)
30
    }
31

            
32
    fn from_request_enum(request: crate::Request) -> Option<Self> {
33
        match request {
34
            crate::Request::MoveId(id, to) => Some(MoveIdRequest { id, to }),
35
            _ => None,
36
        }
37
    }
38

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

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

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

            
62
        Self::throw_if_too_many_arguments(parts)?;
63

            
64
        Ok(MoveIdRequest { id, to })
65
    }
66
}
67

            
68
empty_command_response!(MoveId);
69

            
70
impl Command for MoveId {
71
    type Request = MoveIdRequest;
72
    type Response = MoveIdResponse;
73
}