rabbitmq_stream_protocol/commands/
sasl_authenticate.rs

1use std::io::Write;
2
3use crate::{
4    codec::{Decoder, Encoder},
5    error::{DecodeError, EncodeError},
6    protocol::commands::COMMAND_SASL_AUTHENTICATE,
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 SaslAuthenticateCommand {
17    correlation_id: u32,
18    mechanism: String,
19    sasl_data: Vec<u8>,
20}
21
22impl SaslAuthenticateCommand {
23    pub fn new(correlation_id: u32, mechanism: String, sasl_data: Vec<u8>) -> Self {
24        Self {
25            correlation_id,
26            mechanism,
27            sasl_data,
28        }
29    }
30}
31
32impl Encoder for SaslAuthenticateCommand {
33    fn encoded_size(&self) -> u32 {
34        self.correlation_id.encoded_size()
35            + self.mechanism.as_str().encoded_size()
36            + self.sasl_data.encoded_size()
37    }
38
39    fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
40        self.correlation_id.encode(writer)?;
41        self.mechanism.as_str().encode(writer)?;
42        self.sasl_data.encode(writer)?;
43        Ok(())
44    }
45}
46
47impl Decoder for SaslAuthenticateCommand {
48    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
49        let (input, correlation_id) = u32::decode(input)?;
50        let (input, mechanism) = Option::decode(input)?;
51        let (input, sasl_data) = Vec::decode(input)?;
52
53        Ok((
54            input,
55            SaslAuthenticateCommand {
56                correlation_id,
57                mechanism: mechanism.unwrap(),
58                sasl_data,
59            },
60        ))
61    }
62}
63
64impl Command for SaslAuthenticateCommand {
65    fn key(&self) -> u16 {
66        COMMAND_SASL_AUTHENTICATE
67    }
68}
69
70#[cfg(test)]
71mod tests {
72
73    use crate::commands::tests::command_encode_decode_test;
74
75    use super::SaslAuthenticateCommand;
76
77    #[test]
78    fn sasl_authenticate_request_test() {
79        command_encode_decode_test::<SaslAuthenticateCommand>()
80    }
81}