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