kafka_protocol/messages/
sasl_authenticate_request.rs

1//! SaslAuthenticateRequest
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/SaslAuthenticateRequest.json).
4// WARNING: the items of this module are generated and should not be edited directly
5#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15    buf::{ByteBuf, ByteBufMut},
16    compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17    Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20/// Valid versions: 0-2
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct SaslAuthenticateRequest {
24    /// The SASL authentication bytes from the client, as defined by the SASL mechanism.
25    ///
26    /// Supported API versions: 0-2
27    pub auth_bytes: Bytes,
28
29    /// Other tagged fields
30    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
31}
32
33impl SaslAuthenticateRequest {
34    /// Sets `auth_bytes` to the passed value.
35    ///
36    /// The SASL authentication bytes from the client, as defined by the SASL mechanism.
37    ///
38    /// Supported API versions: 0-2
39    pub fn with_auth_bytes(mut self, value: Bytes) -> Self {
40        self.auth_bytes = value;
41        self
42    }
43    /// Sets unknown_tagged_fields to the passed value.
44    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
45        self.unknown_tagged_fields = value;
46        self
47    }
48    /// Inserts an entry into unknown_tagged_fields.
49    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
50        self.unknown_tagged_fields.insert(key, value);
51        self
52    }
53}
54
55#[cfg(feature = "client")]
56impl Encodable for SaslAuthenticateRequest {
57    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
58        if version < 0 || version > 2 {
59            bail!("specified version not supported by this message type");
60        }
61        if version >= 2 {
62            types::CompactBytes.encode(buf, &self.auth_bytes)?;
63        } else {
64            types::Bytes.encode(buf, &self.auth_bytes)?;
65        }
66        if version >= 2 {
67            let num_tagged_fields = self.unknown_tagged_fields.len();
68            if num_tagged_fields > std::u32::MAX as usize {
69                bail!(
70                    "Too many tagged fields to encode ({} fields)",
71                    num_tagged_fields
72                );
73            }
74            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
75
76            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
77        }
78        Ok(())
79    }
80    fn compute_size(&self, version: i16) -> Result<usize> {
81        let mut total_size = 0;
82        if version >= 2 {
83            total_size += types::CompactBytes.compute_size(&self.auth_bytes)?;
84        } else {
85            total_size += types::Bytes.compute_size(&self.auth_bytes)?;
86        }
87        if version >= 2 {
88            let num_tagged_fields = self.unknown_tagged_fields.len();
89            if num_tagged_fields > std::u32::MAX as usize {
90                bail!(
91                    "Too many tagged fields to encode ({} fields)",
92                    num_tagged_fields
93                );
94            }
95            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
96
97            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
98        }
99        Ok(total_size)
100    }
101}
102
103#[cfg(feature = "broker")]
104impl Decodable for SaslAuthenticateRequest {
105    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
106        if version < 0 || version > 2 {
107            bail!("specified version not supported by this message type");
108        }
109        let auth_bytes = if version >= 2 {
110            types::CompactBytes.decode(buf)?
111        } else {
112            types::Bytes.decode(buf)?
113        };
114        let mut unknown_tagged_fields = BTreeMap::new();
115        if version >= 2 {
116            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
117            for _ in 0..num_tagged_fields {
118                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
119                let size: u32 = types::UnsignedVarInt.decode(buf)?;
120                let unknown_value = buf.try_get_bytes(size as usize)?;
121                unknown_tagged_fields.insert(tag as i32, unknown_value);
122            }
123        }
124        Ok(Self {
125            auth_bytes,
126            unknown_tagged_fields,
127        })
128    }
129}
130
131impl Default for SaslAuthenticateRequest {
132    fn default() -> Self {
133        Self {
134            auth_bytes: Default::default(),
135            unknown_tagged_fields: BTreeMap::new(),
136        }
137    }
138}
139
140impl Message for SaslAuthenticateRequest {
141    const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
142    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
143}
144
145impl HeaderVersion for SaslAuthenticateRequest {
146    fn header_version(version: i16) -> i16 {
147        if version >= 2 {
148            2
149        } else {
150            1
151        }
152    }
153}