rabbitmq_stream_protocol/commands/
unsubscribe.rs

1use std::io::Write;
2
3use crate::{
4    codec::{Decoder, Encoder},
5    error::{DecodeError, EncodeError},
6    protocol::commands::COMMAND_UNSUBSCRIBE,
7};
8
9use super::Command;
10
11#[cfg_attr(test, derive(fake::Dummy))]
12#[derive(PartialEq, Eq, Debug)]
13pub struct UnSubscribeCommand {
14    correlation_id: u32,
15    subscription_id: u8,
16}
17
18impl UnSubscribeCommand {
19    pub fn new(correlation_id: u32, subscription_id: u8) -> Self {
20        Self {
21            correlation_id,
22            subscription_id,
23        }
24    }
25}
26
27impl Encoder for UnSubscribeCommand {
28    fn encoded_size(&self) -> u32 {
29        self.correlation_id.encoded_size() + self.subscription_id.encoded_size()
30    }
31
32    fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
33        self.correlation_id.encode(writer)?;
34        self.subscription_id.encode(writer)?;
35        Ok(())
36    }
37}
38
39impl Command for UnSubscribeCommand {
40    fn key(&self) -> u16 {
41        COMMAND_UNSUBSCRIBE
42    }
43}
44
45impl Decoder for UnSubscribeCommand {
46    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
47        let (input, correlation_id) = u32::decode(input)?;
48        let (input, subscription_id) = u8::decode(input)?;
49
50        Ok((
51            input,
52            UnSubscribeCommand {
53                correlation_id,
54                subscription_id,
55            },
56        ))
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use crate::commands::tests::command_encode_decode_test;
63
64    use super::UnSubscribeCommand;
65
66    #[test]
67    fn unsubscribe_request_test() {
68        command_encode_decode_test::<UnSubscribeCommand>();
69    }
70}