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 serialize(&self) -> String {
29
        format!("{} {} {}\n", Self::COMMAND, self.id, self.to)
30
    }
31

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

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

            
51
        Self::throw_if_too_many_arguments(parts)?;
52

            
53
        Ok(MoveIdRequest { id, to })
54
    }
55
}
56

            
57
empty_command_response!(MoveId);
58

            
59
impl Command for MoveId {
60
    type Request = MoveIdRequest;
61
    type Response = MoveIdResponse;
62
}