rabbitmq_stream_protocol/commands/
tune.rs

1use std::io::Write;
2
3use crate::{
4    codec::{Decoder, Encoder},
5    error::{DecodeError, EncodeError},
6    protocol::commands::COMMAND_TUNE,
7    FromResponse,
8};
9
10use super::Command;
11
12#[cfg_attr(test, derive(fake::Dummy))]
13#[derive(PartialEq, Eq, Debug)]
14pub struct TunesCommand {
15    pub max_frame_size: u32,
16    pub heartbeat: u32,
17}
18
19impl FromResponse for TunesCommand {
20    fn from_response(response: crate::Response) -> Option<Self> {
21        match response.kind {
22            crate::ResponseKind::Tunes(tunes) => Some(tunes),
23            _ => None,
24        }
25    }
26}
27
28impl TunesCommand {
29    pub fn new(max_frame_size: u32, heartbeat: u32) -> Self {
30        Self {
31            max_frame_size,
32            heartbeat,
33        }
34    }
35
36    /// Get a reference to the tunes command's heartbeat.
37    pub fn heartbeat(&self) -> &u32 {
38        &self.heartbeat
39    }
40
41    /// Get a reference to the tunes command's max frame size.
42    pub fn max_frame_size(&self) -> &u32 {
43        &self.max_frame_size
44    }
45}
46
47impl Encoder for TunesCommand {
48    fn encoded_size(&self) -> u32 {
49        self.heartbeat.encoded_size() + self.max_frame_size.encoded_size()
50    }
51
52    fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
53        self.max_frame_size.encode(writer)?;
54        self.heartbeat.encode(writer)?;
55        Ok(())
56    }
57}
58
59impl Decoder for TunesCommand {
60    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
61        let (input, max_frame_size) = u32::decode(input)?;
62        let (input, heartbeat) = u32::decode(input)?;
63
64        Ok((
65            input,
66            TunesCommand {
67                max_frame_size,
68                heartbeat,
69            },
70        ))
71    }
72}
73
74impl Command for TunesCommand {
75    fn key(&self) -> u16 {
76        COMMAND_TUNE
77    }
78}
79
80#[cfg(test)]
81mod tests {
82
83    use super::TunesCommand;
84    use crate::commands::tests::command_encode_decode_test;
85
86    #[test]
87    fn tune_request_test() {
88        command_encode_decode_test::<TunesCommand>()
89    }
90}