rabbitmq_stream_protocol/commands/
declare_publisher.rs1use std::io::Write;
2
3use crate::{
4 codec::{Decoder, Encoder},
5 error::{DecodeError, EncodeError},
6 protocol::commands::COMMAND_DECLARE_PUBLISHER,
7};
8
9use super::Command;
10
11#[cfg_attr(test, derive(fake::Dummy))]
12#[derive(PartialEq, Eq, Debug)]
13pub struct DeclarePublisherCommand {
14 correlation_id: u32,
15 publisher_id: u8,
16 publisher_reference: Option<String>,
17 stream_name: String,
18}
19
20impl DeclarePublisherCommand {
21 pub fn new(
22 correlation_id: u32,
23 publisher_id: u8,
24 publisher_reference: Option<String>,
25 stream_name: String,
26 ) -> Self {
27 Self {
28 correlation_id,
29 publisher_id,
30 publisher_reference,
31 stream_name,
32 }
33 }
34}
35
36impl Encoder for DeclarePublisherCommand {
37 fn encoded_size(&self) -> u32 {
38 self.correlation_id.encoded_size()
39 + self.publisher_id.encoded_size()
40 + self.publisher_reference.encoded_size()
41 + self.stream_name.as_str().encoded_size()
42 }
43
44 fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
45 self.correlation_id.encode(writer)?;
46 self.publisher_id.encode(writer)?;
47 self.publisher_reference.encode(writer)?;
48 self.stream_name.as_str().encode(writer)?;
49 Ok(())
50 }
51}
52
53impl Command for DeclarePublisherCommand {
54 fn key(&self) -> u16 {
55 COMMAND_DECLARE_PUBLISHER
56 }
57}
58
59impl Decoder for DeclarePublisherCommand {
60 fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
61 let (input, correlation_id) = u32::decode(input)?;
62 let (input, publisher_id) = u8::decode(input)?;
63 let (input, publisher_reference) = Option::decode(input)?;
64 let (input, stream_name) = Option::decode(input)?;
65
66 Ok((
67 input,
68 DeclarePublisherCommand {
69 correlation_id,
70 publisher_id,
71 publisher_reference,
72 stream_name: stream_name.unwrap(),
73 },
74 ))
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use crate::commands::tests::command_encode_decode_test;
81
82 use super::DeclarePublisherCommand;
83
84 #[test]
85 fn create_stream_request_test() {
86 command_encode_decode_test::<DeclarePublisherCommand>();
87 }
88}