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