kafka_protocol/messages/
create_delegation_token_response.rs

1//! CreateDelegationTokenResponse
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/CreateDelegationTokenResponse.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-3
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct CreateDelegationTokenResponse {
24    /// The top-level error, or zero if there was no error.
25    ///
26    /// Supported API versions: 0-3
27    pub error_code: i16,
28
29    /// The principal type of the token owner.
30    ///
31    /// Supported API versions: 0-3
32    pub principal_type: StrBytes,
33
34    /// The name of the token owner.
35    ///
36    /// Supported API versions: 0-3
37    pub principal_name: StrBytes,
38
39    /// The principal type of the requester of the token.
40    ///
41    /// Supported API versions: 3
42    pub token_requester_principal_type: StrBytes,
43
44    /// The principal type of the requester of the token.
45    ///
46    /// Supported API versions: 3
47    pub token_requester_principal_name: StrBytes,
48
49    /// When this token was generated.
50    ///
51    /// Supported API versions: 0-3
52    pub issue_timestamp_ms: i64,
53
54    /// When this token expires.
55    ///
56    /// Supported API versions: 0-3
57    pub expiry_timestamp_ms: i64,
58
59    /// The maximum lifetime of this token.
60    ///
61    /// Supported API versions: 0-3
62    pub max_timestamp_ms: i64,
63
64    /// The token UUID.
65    ///
66    /// Supported API versions: 0-3
67    pub token_id: StrBytes,
68
69    /// HMAC of the delegation token.
70    ///
71    /// Supported API versions: 0-3
72    pub hmac: Bytes,
73
74    /// The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
75    ///
76    /// Supported API versions: 0-3
77    pub throttle_time_ms: i32,
78
79    /// Other tagged fields
80    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
81}
82
83impl CreateDelegationTokenResponse {
84    /// Sets `error_code` to the passed value.
85    ///
86    /// The top-level error, or zero if there was no error.
87    ///
88    /// Supported API versions: 0-3
89    pub fn with_error_code(mut self, value: i16) -> Self {
90        self.error_code = value;
91        self
92    }
93    /// Sets `principal_type` to the passed value.
94    ///
95    /// The principal type of the token owner.
96    ///
97    /// Supported API versions: 0-3
98    pub fn with_principal_type(mut self, value: StrBytes) -> Self {
99        self.principal_type = value;
100        self
101    }
102    /// Sets `principal_name` to the passed value.
103    ///
104    /// The name of the token owner.
105    ///
106    /// Supported API versions: 0-3
107    pub fn with_principal_name(mut self, value: StrBytes) -> Self {
108        self.principal_name = value;
109        self
110    }
111    /// Sets `token_requester_principal_type` to the passed value.
112    ///
113    /// The principal type of the requester of the token.
114    ///
115    /// Supported API versions: 3
116    pub fn with_token_requester_principal_type(mut self, value: StrBytes) -> Self {
117        self.token_requester_principal_type = value;
118        self
119    }
120    /// Sets `token_requester_principal_name` to the passed value.
121    ///
122    /// The principal type of the requester of the token.
123    ///
124    /// Supported API versions: 3
125    pub fn with_token_requester_principal_name(mut self, value: StrBytes) -> Self {
126        self.token_requester_principal_name = value;
127        self
128    }
129    /// Sets `issue_timestamp_ms` to the passed value.
130    ///
131    /// When this token was generated.
132    ///
133    /// Supported API versions: 0-3
134    pub fn with_issue_timestamp_ms(mut self, value: i64) -> Self {
135        self.issue_timestamp_ms = value;
136        self
137    }
138    /// Sets `expiry_timestamp_ms` to the passed value.
139    ///
140    /// When this token expires.
141    ///
142    /// Supported API versions: 0-3
143    pub fn with_expiry_timestamp_ms(mut self, value: i64) -> Self {
144        self.expiry_timestamp_ms = value;
145        self
146    }
147    /// Sets `max_timestamp_ms` to the passed value.
148    ///
149    /// The maximum lifetime of this token.
150    ///
151    /// Supported API versions: 0-3
152    pub fn with_max_timestamp_ms(mut self, value: i64) -> Self {
153        self.max_timestamp_ms = value;
154        self
155    }
156    /// Sets `token_id` to the passed value.
157    ///
158    /// The token UUID.
159    ///
160    /// Supported API versions: 0-3
161    pub fn with_token_id(mut self, value: StrBytes) -> Self {
162        self.token_id = value;
163        self
164    }
165    /// Sets `hmac` to the passed value.
166    ///
167    /// HMAC of the delegation token.
168    ///
169    /// Supported API versions: 0-3
170    pub fn with_hmac(mut self, value: Bytes) -> Self {
171        self.hmac = value;
172        self
173    }
174    /// Sets `throttle_time_ms` to the passed value.
175    ///
176    /// The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
177    ///
178    /// Supported API versions: 0-3
179    pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
180        self.throttle_time_ms = value;
181        self
182    }
183    /// Sets unknown_tagged_fields to the passed value.
184    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
185        self.unknown_tagged_fields = value;
186        self
187    }
188    /// Inserts an entry into unknown_tagged_fields.
189    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
190        self.unknown_tagged_fields.insert(key, value);
191        self
192    }
193}
194
195#[cfg(feature = "broker")]
196impl Encodable for CreateDelegationTokenResponse {
197    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
198        if version < 0 || version > 3 {
199            bail!("specified version not supported by this message type");
200        }
201        types::Int16.encode(buf, &self.error_code)?;
202        if version >= 2 {
203            types::CompactString.encode(buf, &self.principal_type)?;
204        } else {
205            types::String.encode(buf, &self.principal_type)?;
206        }
207        if version >= 2 {
208            types::CompactString.encode(buf, &self.principal_name)?;
209        } else {
210            types::String.encode(buf, &self.principal_name)?;
211        }
212        if version >= 3 {
213            types::CompactString.encode(buf, &self.token_requester_principal_type)?;
214        } else {
215            if !self.token_requester_principal_type.is_empty() {
216                bail!("A field is set that is not available on the selected protocol version");
217            }
218        }
219        if version >= 3 {
220            types::CompactString.encode(buf, &self.token_requester_principal_name)?;
221        } else {
222            if !self.token_requester_principal_name.is_empty() {
223                bail!("A field is set that is not available on the selected protocol version");
224            }
225        }
226        types::Int64.encode(buf, &self.issue_timestamp_ms)?;
227        types::Int64.encode(buf, &self.expiry_timestamp_ms)?;
228        types::Int64.encode(buf, &self.max_timestamp_ms)?;
229        if version >= 2 {
230            types::CompactString.encode(buf, &self.token_id)?;
231        } else {
232            types::String.encode(buf, &self.token_id)?;
233        }
234        if version >= 2 {
235            types::CompactBytes.encode(buf, &self.hmac)?;
236        } else {
237            types::Bytes.encode(buf, &self.hmac)?;
238        }
239        types::Int32.encode(buf, &self.throttle_time_ms)?;
240        if version >= 2 {
241            let num_tagged_fields = self.unknown_tagged_fields.len();
242            if num_tagged_fields > std::u32::MAX as usize {
243                bail!(
244                    "Too many tagged fields to encode ({} fields)",
245                    num_tagged_fields
246                );
247            }
248            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
249
250            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
251        }
252        Ok(())
253    }
254    fn compute_size(&self, version: i16) -> Result<usize> {
255        let mut total_size = 0;
256        total_size += types::Int16.compute_size(&self.error_code)?;
257        if version >= 2 {
258            total_size += types::CompactString.compute_size(&self.principal_type)?;
259        } else {
260            total_size += types::String.compute_size(&self.principal_type)?;
261        }
262        if version >= 2 {
263            total_size += types::CompactString.compute_size(&self.principal_name)?;
264        } else {
265            total_size += types::String.compute_size(&self.principal_name)?;
266        }
267        if version >= 3 {
268            total_size +=
269                types::CompactString.compute_size(&self.token_requester_principal_type)?;
270        } else {
271            if !self.token_requester_principal_type.is_empty() {
272                bail!("A field is set that is not available on the selected protocol version");
273            }
274        }
275        if version >= 3 {
276            total_size +=
277                types::CompactString.compute_size(&self.token_requester_principal_name)?;
278        } else {
279            if !self.token_requester_principal_name.is_empty() {
280                bail!("A field is set that is not available on the selected protocol version");
281            }
282        }
283        total_size += types::Int64.compute_size(&self.issue_timestamp_ms)?;
284        total_size += types::Int64.compute_size(&self.expiry_timestamp_ms)?;
285        total_size += types::Int64.compute_size(&self.max_timestamp_ms)?;
286        if version >= 2 {
287            total_size += types::CompactString.compute_size(&self.token_id)?;
288        } else {
289            total_size += types::String.compute_size(&self.token_id)?;
290        }
291        if version >= 2 {
292            total_size += types::CompactBytes.compute_size(&self.hmac)?;
293        } else {
294            total_size += types::Bytes.compute_size(&self.hmac)?;
295        }
296        total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
297        if version >= 2 {
298            let num_tagged_fields = self.unknown_tagged_fields.len();
299            if num_tagged_fields > std::u32::MAX as usize {
300                bail!(
301                    "Too many tagged fields to encode ({} fields)",
302                    num_tagged_fields
303                );
304            }
305            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
306
307            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
308        }
309        Ok(total_size)
310    }
311}
312
313#[cfg(feature = "client")]
314impl Decodable for CreateDelegationTokenResponse {
315    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
316        if version < 0 || version > 3 {
317            bail!("specified version not supported by this message type");
318        }
319        let error_code = types::Int16.decode(buf)?;
320        let principal_type = if version >= 2 {
321            types::CompactString.decode(buf)?
322        } else {
323            types::String.decode(buf)?
324        };
325        let principal_name = if version >= 2 {
326            types::CompactString.decode(buf)?
327        } else {
328            types::String.decode(buf)?
329        };
330        let token_requester_principal_type = if version >= 3 {
331            types::CompactString.decode(buf)?
332        } else {
333            Default::default()
334        };
335        let token_requester_principal_name = if version >= 3 {
336            types::CompactString.decode(buf)?
337        } else {
338            Default::default()
339        };
340        let issue_timestamp_ms = types::Int64.decode(buf)?;
341        let expiry_timestamp_ms = types::Int64.decode(buf)?;
342        let max_timestamp_ms = types::Int64.decode(buf)?;
343        let token_id = if version >= 2 {
344            types::CompactString.decode(buf)?
345        } else {
346            types::String.decode(buf)?
347        };
348        let hmac = if version >= 2 {
349            types::CompactBytes.decode(buf)?
350        } else {
351            types::Bytes.decode(buf)?
352        };
353        let throttle_time_ms = types::Int32.decode(buf)?;
354        let mut unknown_tagged_fields = BTreeMap::new();
355        if version >= 2 {
356            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
357            for _ in 0..num_tagged_fields {
358                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
359                let size: u32 = types::UnsignedVarInt.decode(buf)?;
360                let unknown_value = buf.try_get_bytes(size as usize)?;
361                unknown_tagged_fields.insert(tag as i32, unknown_value);
362            }
363        }
364        Ok(Self {
365            error_code,
366            principal_type,
367            principal_name,
368            token_requester_principal_type,
369            token_requester_principal_name,
370            issue_timestamp_ms,
371            expiry_timestamp_ms,
372            max_timestamp_ms,
373            token_id,
374            hmac,
375            throttle_time_ms,
376            unknown_tagged_fields,
377        })
378    }
379}
380
381impl Default for CreateDelegationTokenResponse {
382    fn default() -> Self {
383        Self {
384            error_code: 0,
385            principal_type: Default::default(),
386            principal_name: Default::default(),
387            token_requester_principal_type: Default::default(),
388            token_requester_principal_name: Default::default(),
389            issue_timestamp_ms: 0,
390            expiry_timestamp_ms: 0,
391            max_timestamp_ms: 0,
392            token_id: Default::default(),
393            hmac: Default::default(),
394            throttle_time_ms: 0,
395            unknown_tagged_fields: BTreeMap::new(),
396        }
397    }
398}
399
400impl Message for CreateDelegationTokenResponse {
401    const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
402    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
403}
404
405impl HeaderVersion for CreateDelegationTokenResponse {
406    fn header_version(version: i16) -> i16 {
407        if version >= 2 {
408            1
409        } else {
410            0
411        }
412    }
413}