kafka_protocol/messages/
create_delegation_token_request.rs

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