rabbitmq_stream_protocol/commands/
publish_confirm.rs

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