kafka_protocol/messages/
sasl_authenticate_response.rs

1//! SaslAuthenticateResponse
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/SaslAuthenticateResponse.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 SaslAuthenticateResponse {
24    /// The error code, or 0 if there was no error.
25    ///
26    /// Supported API versions: 0-2
27    pub error_code: i16,
28
29    /// The error message, or null if there was no error.
30    ///
31    /// Supported API versions: 0-2
32    pub error_message: Option<StrBytes>,
33
34    /// The SASL authentication bytes from the server, as defined by the SASL mechanism.
35    ///
36    /// Supported API versions: 0-2
37    pub auth_bytes: Bytes,
38
39    /// Number of milliseconds after which only re-authentication over the existing connection to create a new session can occur.
40    ///
41    /// Supported API versions: 1-2
42    pub session_lifetime_ms: i64,
43
44    /// Other tagged fields
45    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl SaslAuthenticateResponse {
49    /// Sets `error_code` to the passed value.
50    ///
51    /// The error code, or 0 if there was no error.
52    ///
53    /// Supported API versions: 0-2
54    pub fn with_error_code(mut self, value: i16) -> Self {
55        self.error_code = value;
56        self
57    }
58    /// Sets `error_message` to the passed value.
59    ///
60    /// The error message, or null if there was no error.
61    ///
62    /// Supported API versions: 0-2
63    pub fn with_error_message(mut self, value: Option<StrBytes>) -> Self {
64        self.error_message = value;
65        self
66    }
67    /// Sets `auth_bytes` to the passed value.
68    ///
69    /// The SASL authentication bytes from the server, as defined by the SASL mechanism.
70    ///
71    /// Supported API versions: 0-2
72    pub fn with_auth_bytes(mut self, value: Bytes) -> Self {
73        self.auth_bytes = value;
74        self
75    }
76    /// Sets `session_lifetime_ms` to the passed value.
77    ///
78    /// Number of milliseconds after which only re-authentication over the existing connection to create a new session can occur.
79    ///
80    /// Supported API versions: 1-2
81    pub fn with_session_lifetime_ms(mut self, value: i64) -> Self {
82        self.session_lifetime_ms = value;
83        self
84    }
85    /// Sets unknown_tagged_fields to the passed value.
86    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
87        self.unknown_tagged_fields = value;
88        self
89    }
90    /// Inserts an entry into unknown_tagged_fields.
91    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
92        self.unknown_tagged_fields.insert(key, value);
93        self
94    }
95}
96
97#[cfg(feature = "broker")]
98impl Encodable for SaslAuthenticateResponse {
99    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100        types::Int16.encode(buf, &self.error_code)?;
101        if version >= 2 {
102            types::CompactString.encode(buf, &self.error_message)?;
103        } else {
104            types::String.encode(buf, &self.error_message)?;
105        }
106        if version >= 2 {
107            types::CompactBytes.encode(buf, &self.auth_bytes)?;
108        } else {
109            types::Bytes.encode(buf, &self.auth_bytes)?;
110        }
111        if version >= 1 {
112            types::Int64.encode(buf, &self.session_lifetime_ms)?;
113        }
114        if version >= 2 {
115            let num_tagged_fields = self.unknown_tagged_fields.len();
116            if num_tagged_fields > std::u32::MAX as usize {
117                bail!(
118                    "Too many tagged fields to encode ({} fields)",
119                    num_tagged_fields
120                );
121            }
122            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
123
124            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
125        }
126        Ok(())
127    }
128    fn compute_size(&self, version: i16) -> Result<usize> {
129        let mut total_size = 0;
130        total_size += types::Int16.compute_size(&self.error_code)?;
131        if version >= 2 {
132            total_size += types::CompactString.compute_size(&self.error_message)?;
133        } else {
134            total_size += types::String.compute_size(&self.error_message)?;
135        }
136        if version >= 2 {
137            total_size += types::CompactBytes.compute_size(&self.auth_bytes)?;
138        } else {
139            total_size += types::Bytes.compute_size(&self.auth_bytes)?;
140        }
141        if version >= 1 {
142            total_size += types::Int64.compute_size(&self.session_lifetime_ms)?;
143        }
144        if version >= 2 {
145            let num_tagged_fields = self.unknown_tagged_fields.len();
146            if num_tagged_fields > std::u32::MAX as usize {
147                bail!(
148                    "Too many tagged fields to encode ({} fields)",
149                    num_tagged_fields
150                );
151            }
152            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
153
154            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
155        }
156        Ok(total_size)
157    }
158}
159
160#[cfg(feature = "client")]
161impl Decodable for SaslAuthenticateResponse {
162    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
163        let error_code = types::Int16.decode(buf)?;
164        let error_message = if version >= 2 {
165            types::CompactString.decode(buf)?
166        } else {
167            types::String.decode(buf)?
168        };
169        let auth_bytes = if version >= 2 {
170            types::CompactBytes.decode(buf)?
171        } else {
172            types::Bytes.decode(buf)?
173        };
174        let session_lifetime_ms = if version >= 1 {
175            types::Int64.decode(buf)?
176        } else {
177            0
178        };
179        let mut unknown_tagged_fields = BTreeMap::new();
180        if version >= 2 {
181            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
182            for _ in 0..num_tagged_fields {
183                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
184                let size: u32 = types::UnsignedVarInt.decode(buf)?;
185                let unknown_value = buf.try_get_bytes(size as usize)?;
186                unknown_tagged_fields.insert(tag as i32, unknown_value);
187            }
188        }
189        Ok(Self {
190            error_code,
191            error_message,
192            auth_bytes,
193            session_lifetime_ms,
194            unknown_tagged_fields,
195        })
196    }
197}
198
199impl Default for SaslAuthenticateResponse {
200    fn default() -> Self {
201        Self {
202            error_code: 0,
203            error_message: Some(Default::default()),
204            auth_bytes: Default::default(),
205            session_lifetime_ms: 0,
206            unknown_tagged_fields: BTreeMap::new(),
207        }
208    }
209}
210
211impl Message for SaslAuthenticateResponse {
212    const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
213    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
214}
215
216impl HeaderVersion for SaslAuthenticateResponse {
217    fn header_version(version: i16) -> i16 {
218        if version >= 2 {
219            1
220        } else {
221            0
222        }
223    }
224}