rabbitmq_stream_protocol/commands/
peer_properties.rs

1use std::collections::HashMap;
2use std::io::Write;
3
4use crate::{
5    codec::{Decoder, Encoder},
6    error::{DecodeError, EncodeError},
7    protocol::commands::COMMAND_PEER_PROPERTIES,
8    FromResponse, ResponseCode,
9};
10
11use super::Command;
12
13#[cfg_attr(test, derive(fake::Dummy))]
14#[derive(PartialEq, Eq, Debug)]
15pub struct PeerPropertiesCommand {
16    correlation_id: u32,
17    client_properties: HashMap<String, String>,
18}
19
20impl PeerPropertiesCommand {
21    pub fn new(correlation_id: u32, client_properties: HashMap<String, String>) -> Self {
22        Self {
23            correlation_id,
24            client_properties,
25        }
26    }
27
28    /// Get a reference to the peer properties command's correlation id.
29    pub fn correlation_id(&self) -> u32 {
30        self.correlation_id
31    }
32}
33
34impl Encoder for PeerPropertiesCommand {
35    fn encoded_size(&self) -> u32 {
36        self.correlation_id.encoded_size() + self.client_properties.encoded_size()
37    }
38
39    fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
40        self.correlation_id.encode(writer)?;
41        self.client_properties.encode(writer)?;
42        Ok(())
43    }
44}
45
46impl Decoder for PeerPropertiesCommand {
47    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
48        let (input, correlation_id) = u32::decode(input)?;
49        let (input, client_properties) = HashMap::decode(input)?;
50
51        Ok((
52            input,
53            PeerPropertiesCommand {
54                correlation_id,
55                client_properties,
56            },
57        ))
58    }
59}
60
61impl Command for PeerPropertiesCommand {
62    fn key(&self) -> u16 {
63        COMMAND_PEER_PROPERTIES
64    }
65}
66
67#[cfg_attr(test, derive(fake::Dummy))]
68#[derive(Debug, PartialEq, Eq)]
69pub struct PeerPropertiesResponse {
70    pub(crate) correlation_id: u32,
71    pub(crate) code: ResponseCode,
72    pub server_properties: HashMap<String, String>,
73}
74
75impl PeerPropertiesResponse {
76    pub fn new(
77        correlation_id: u32,
78        code: ResponseCode,
79        server_properties: HashMap<String, String>,
80    ) -> Self {
81        Self {
82            correlation_id,
83            code,
84            server_properties,
85        }
86    }
87
88    pub fn server_properties(&self) -> &HashMap<String, String> {
89        &self.server_properties
90    }
91}
92
93impl FromResponse for PeerPropertiesResponse {
94    fn from_response(response: crate::Response) -> Option<Self> {
95        match response.kind {
96            crate::ResponseKind::PeerProperties(peer_properties) => Some(peer_properties),
97            _ => None,
98        }
99    }
100}
101
102impl Decoder for PeerPropertiesResponse {
103    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
104        let (input, correlation_id) = u32::decode(input)?;
105        let (input, code) = ResponseCode::decode(input)?;
106        let (input, server_properties) = HashMap::decode(input)?;
107
108        Ok((
109            input,
110            PeerPropertiesResponse {
111                correlation_id,
112                code,
113                server_properties,
114            },
115        ))
116    }
117}
118
119#[cfg(test)]
120mod tests {
121
122    use crate::codec::Encoder;
123    use crate::commands::peer_properties::PeerPropertiesResponse;
124    use crate::commands::tests::command_encode_decode_test;
125
126    use super::PeerPropertiesCommand;
127
128    #[test]
129    fn peer_properties_request_test() {
130        command_encode_decode_test::<PeerPropertiesCommand>()
131    }
132
133    impl Encoder for PeerPropertiesResponse {
134        fn encoded_size(&self) -> u32 {
135            0
136        }
137
138        fn encode(
139            &self,
140            writer: &mut impl std::io::Write,
141        ) -> Result<(), crate::error::EncodeError> {
142            self.correlation_id.encode(writer)?;
143            self.code.encode(writer)?;
144            self.server_properties.encode(writer)?;
145            Ok(())
146        }
147    }
148
149    #[test]
150    fn peer_properties_response_test() {
151        command_encode_decode_test::<PeerPropertiesResponse>();
152    }
153}