rabbitmq_stream_protocol/commands/
sasl_handshake.rs

1use std::io::Write;
2
3use crate::{
4    codec::{Decoder, Encoder},
5    error::{DecodeError, EncodeError},
6    protocol::commands::COMMAND_SASL_HANDSHAKE,
7    response::ResponseCode,
8    FromResponse,
9};
10
11use super::Command;
12
13#[cfg_attr(test, derive(fake::Dummy))]
14#[derive(PartialEq, Eq, Debug)]
15pub struct SaslHandshakeCommand {
16    correlation_id: u32,
17}
18
19impl SaslHandshakeCommand {
20    pub fn new(correlation_id: u32) -> Self {
21        Self { correlation_id }
22    }
23}
24
25impl Encoder for SaslHandshakeCommand {
26    fn encoded_size(&self) -> u32 {
27        self.correlation_id.encoded_size()
28    }
29
30    fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
31        self.correlation_id.encode(writer)?;
32        Ok(())
33    }
34}
35
36impl Decoder for SaslHandshakeCommand {
37    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
38        let (input, correlation_id) = u32::decode(input)?;
39
40        Ok((input, SaslHandshakeCommand { correlation_id }))
41    }
42}
43
44impl Command for SaslHandshakeCommand {
45    fn key(&self) -> u16 {
46        COMMAND_SASL_HANDSHAKE
47    }
48}
49
50#[cfg_attr(test, derive(fake::Dummy))]
51#[derive(Debug, PartialEq, Eq)]
52pub struct SaslHandshakeResponse {
53    pub(crate) correlation_id: u32,
54    pub(crate) code: ResponseCode,
55    pub mechanisms: Vec<String>,
56}
57
58impl SaslHandshakeResponse {
59    pub fn mechanisms(&self) -> &Vec<String> {
60        &self.mechanisms
61    }
62}
63
64impl FromResponse for SaslHandshakeResponse {
65    fn from_response(response: crate::Response) -> Option<Self> {
66        match response.kind {
67            crate::ResponseKind::SaslHandshake(handshake) => Some(handshake),
68            _ => None,
69        }
70    }
71}
72
73impl Decoder for SaslHandshakeResponse {
74    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
75        let (input, correlation_id) = u32::decode(input)?;
76        let (input, response_code) = ResponseCode::decode(input)?;
77        let (input, mechanisms) = Vec::decode(input)?;
78
79        Ok((
80            input,
81            SaslHandshakeResponse {
82                correlation_id,
83                code: response_code,
84                mechanisms,
85            },
86        ))
87    }
88}
89
90#[cfg(test)]
91mod tests {
92
93    use crate::{codec::Encoder, commands::tests::command_encode_decode_test};
94
95    use super::SaslHandshakeCommand;
96    use super::SaslHandshakeResponse;
97
98    #[test]
99    fn sasl_handshake_request_test() {
100        command_encode_decode_test::<SaslHandshakeCommand>();
101    }
102
103    impl Encoder for SaslHandshakeResponse {
104        fn encoded_size(&self) -> u32 {
105            0
106        }
107
108        fn encode(
109            &self,
110            writer: &mut impl std::io::Write,
111        ) -> Result<(), crate::error::EncodeError> {
112            self.correlation_id.encode(writer)?;
113            self.code.encode(writer)?;
114            self.mechanisms.encode(writer)?;
115            Ok(())
116        }
117    }
118    #[test]
119    fn sasl_handshake_response_test() {
120        command_encode_decode_test::<SaslHandshakeResponse>()
121    }
122}