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

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

            
9
pub struct RangeId;
10

            
11
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12
pub struct RangeIdRequest {
13
    songid: SongId,
14
    time_interval: TimeInterval,
15
}
16

            
17
impl RangeIdRequest {
18
    pub fn new(songid: SongId, time_interval: TimeInterval) -> Self {
19
        Self {
20
            songid,
21
            time_interval,
22
        }
23
    }
24
}
25

            
26
impl CommandRequest for RangeIdRequest {
27
    const COMMAND: &'static str = "rangeid";
28
    const MIN_ARGS: u32 = 2;
29
    const MAX_ARGS: Option<u32> = Some(2);
30

            
31
    fn serialize(&self) -> String {
32
        format!("{} {} {}\n", Self::COMMAND, self.songid, self.time_interval)
33
    }
34

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

            
45
        let time_interval = parts.next().ok_or(Self::missing_arguments_error(1))?;
46
        let time_interval =
47
            time_interval
48
                .parse()
49
                .map_err(|_| RequestParserError::SubtypeParserError {
50
                    argument_index: 1,
51
                    expected_type: "TimeInterval",
52
                    raw_input: time_interval.to_string(),
53
                })?;
54

            
55
        Self::throw_if_too_many_arguments(parts)?;
56

            
57
        Ok(RangeIdRequest {
58
            songid,
59
            time_interval,
60
        })
61
    }
62
}
63

            
64
empty_command_response!(RangeId);
65

            
66
impl Command for RangeId {
67
    type Request = RangeIdRequest;
68
    type Response = RangeIdResponse;
69
}