1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::io::Write;

use crate::{
    codec::{Decoder, Encoder},
    error::{DecodeError, EncodeError},
    protocol::commands::COMMAND_TUNE,
    FromResponse,
};

use super::Command;

#[cfg(test)]
use fake::Fake;

#[cfg_attr(test, derive(fake::Dummy))]
#[derive(PartialEq, Eq, Debug)]
pub struct TunesCommand {
    pub max_frame_size: u32,
    pub heartbeat: u32,
}

impl FromResponse for TunesCommand {
    fn from_response(response: crate::Response) -> Option<Self> {
        match response.kind {
            crate::ResponseKind::Tunes(tunes) => Some(tunes),
            _ => None,
        }
    }
}

impl TunesCommand {
    pub fn new(max_frame_size: u32, heartbeat: u32) -> Self {
        Self {
            max_frame_size,
            heartbeat,
        }
    }

    /// Get a reference to the tunes command's heartbeat.
    pub fn heartbeat(&self) -> &u32 {
        &self.heartbeat
    }

    /// Get a reference to the tunes command's max frame size.
    pub fn max_frame_size(&self) -> &u32 {
        &self.max_frame_size
    }
}

impl Encoder for TunesCommand {
    fn encoded_size(&self) -> u32 {
        self.heartbeat.encoded_size() + self.max_frame_size.encoded_size()
    }

    fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
        self.max_frame_size.encode(writer)?;
        self.heartbeat.encode(writer)?;
        Ok(())
    }
}

impl Decoder for TunesCommand {
    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
        let (input, max_frame_size) = u32::decode(input)?;
        let (input, heartbeat) = u32::decode(input)?;

        Ok((
            input,
            TunesCommand {
                max_frame_size,
                heartbeat,
            },
        ))
    }
}

impl Command for TunesCommand {
    fn key(&self) -> u16 {
        COMMAND_TUNE
    }
}

#[cfg(test)]
mod tests {

    use super::TunesCommand;
    use crate::commands::tests::command_encode_decode_test;

    #[test]
    fn tune_request_test() {
        command_encode_decode_test::<TunesCommand>()
    }
}