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        if version < 0 || version > 2 {
101            bail!("specified version not supported by this message type");
102        }
103        types::Int16.encode(buf, &self.error_code)?;
104        if version >= 2 {
105            types::CompactString.encode(buf, &self.error_message)?;
106        } else {
107            types::String.encode(buf, &self.error_message)?;
108        }
109        if version >= 2 {
110            types::CompactBytes.encode(buf, &self.auth_bytes)?;
111        } else {
112            types::Bytes.encode(buf, &self.auth_bytes)?;
113        }
114        if version >= 1 {
115            types::Int64.encode(buf, &self.session_lifetime_ms)?;
116        }
117        if version >= 2 {
118            let num_tagged_fields = self.unknown_tagged_fields.len();
119            if num_tagged_fields > std::u32::MAX as usize {
120                bail!(
121                    "Too many tagged fields to encode ({} fields)",
122                    num_tagged_fields
123                );
124            }
125            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
126
127            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
128        }
129        Ok(())
130    }
131    fn compute_size(&self, version: i16) -> Result<usize> {
132        let mut total_size = 0;
133        total_size += types::Int16.compute_size(&self.error_code)?;
134        if version >= 2 {
135            total_size += types::CompactString.compute_size(&self.error_message)?;
136        } else {
137            total_size += types::String.compute_size(&self.error_message)?;
138        }
139        if version >= 2 {
140            total_size += types::CompactBytes.compute_size(&self.auth_bytes)?;
141        } else {
142            total_size += types::Bytes.compute_size(&self.auth_bytes)?;
143        }
144        if version >= 1 {
145            total_size += types::Int64.compute_size(&self.session_lifetime_ms)?;
146        }
147        if version >= 2 {
148            let num_tagged_fields = self.unknown_tagged_fields.len();
149            if num_tagged_fields > std::u32::MAX as usize {
150                bail!(
151                    "Too many tagged fields to encode ({} fields)",
152                    num_tagged_fields
153                );
154            }
155            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
156
157            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
158        }
159        Ok(total_size)
160    }
161}
162
163#[cfg(feature = "client")]
164impl Decodable for SaslAuthenticateResponse {
165    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
166        if version < 0 || version > 2 {
167            bail!("specified version not supported by this message type");
168        }
169        let error_code = types::Int16.decode(buf)?;
170        let error_message = if version >= 2 {
171            types::CompactString.decode(buf)?
172        } else {
173            types::String.decode(buf)?
174        };
175        let auth_bytes = if version >= 2 {
176            types::CompactBytes.decode(buf)?
177        } else {
178            types::Bytes.decode(buf)?
179        };
180        let session_lifetime_ms = if version >= 1 {
181            types::Int64.decode(buf)?
182        } else {
183            0
184        };
185        let mut unknown_tagged_fields = BTreeMap::new();
186        if version >= 2 {
187            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
188            for _ in 0..num_tagged_fields {
189                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
190                let size: u32 = types::UnsignedVarInt.decode(buf)?;
191                let unknown_value = buf.try_get_bytes(size as usize)?;
192                unknown_tagged_fields.insert(tag as i32, unknown_value);
193            }
194        }
195        Ok(Self {
196            error_code,
197            error_message,
198            auth_bytes,
199            session_lifetime_ms,
200            unknown_tagged_fields,
201        })
202    }
203}
204
205impl Default for SaslAuthenticateResponse {
206    fn default() -> Self {
207        Self {
208            error_code: 0,
209            error_message: Some(Default::default()),
210            auth_bytes: Default::default(),
211            session_lifetime_ms: 0,
212            unknown_tagged_fields: BTreeMap::new(),
213        }
214    }
215}
216
217impl Message for SaslAuthenticateResponse {
218    const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
219    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
220}
221
222impl HeaderVersion for SaslAuthenticateResponse {
223    fn header_version(version: i16) -> i16 {
224        if version >= 2 {
225            1
226        } else {
227            0
228        }
229    }
230}