kafka_protocol/messages/
default_principal_data.rs

1//! DefaultPrincipalData
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/DefaultPrincipalData.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
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct DefaultPrincipalData {
24    /// The principal type
25    ///
26    /// Supported API versions: 0
27    pub _type: StrBytes,
28
29    /// The principal name
30    ///
31    /// Supported API versions: 0
32    pub name: StrBytes,
33
34    /// Whether the principal was authenticated by a delegation token on the forwarding broker.
35    ///
36    /// Supported API versions: 0
37    pub token_authenticated: bool,
38
39    /// Other tagged fields
40    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl DefaultPrincipalData {
44    /// Sets `_type` to the passed value.
45    ///
46    /// The principal type
47    ///
48    /// Supported API versions: 0
49    pub fn with_type(mut self, value: StrBytes) -> Self {
50        self._type = value;
51        self
52    }
53    /// Sets `name` to the passed value.
54    ///
55    /// The principal name
56    ///
57    /// Supported API versions: 0
58    pub fn with_name(mut self, value: StrBytes) -> Self {
59        self.name = value;
60        self
61    }
62    /// Sets `token_authenticated` to the passed value.
63    ///
64    /// Whether the principal was authenticated by a delegation token on the forwarding broker.
65    ///
66    /// Supported API versions: 0
67    pub fn with_token_authenticated(mut self, value: bool) -> Self {
68        self.token_authenticated = value;
69        self
70    }
71    /// Sets unknown_tagged_fields to the passed value.
72    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
73        self.unknown_tagged_fields = value;
74        self
75    }
76    /// Inserts an entry into unknown_tagged_fields.
77    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
78        self.unknown_tagged_fields.insert(key, value);
79        self
80    }
81}
82
83impl Encodable for DefaultPrincipalData {
84    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
85        types::CompactString.encode(buf, &self._type)?;
86        types::CompactString.encode(buf, &self.name)?;
87        types::Boolean.encode(buf, &self.token_authenticated)?;
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        types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
96
97        write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
98        Ok(())
99    }
100    fn compute_size(&self, version: i16) -> Result<usize> {
101        let mut total_size = 0;
102        total_size += types::CompactString.compute_size(&self._type)?;
103        total_size += types::CompactString.compute_size(&self.name)?;
104        total_size += types::Boolean.compute_size(&self.token_authenticated)?;
105        let num_tagged_fields = self.unknown_tagged_fields.len();
106        if num_tagged_fields > std::u32::MAX as usize {
107            bail!(
108                "Too many tagged fields to encode ({} fields)",
109                num_tagged_fields
110            );
111        }
112        total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
113
114        total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
115        Ok(total_size)
116    }
117}
118
119impl Decodable for DefaultPrincipalData {
120    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
121        let _type = types::CompactString.decode(buf)?;
122        let name = types::CompactString.decode(buf)?;
123        let token_authenticated = types::Boolean.decode(buf)?;
124        let mut unknown_tagged_fields = BTreeMap::new();
125        let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
126        for _ in 0..num_tagged_fields {
127            let tag: u32 = types::UnsignedVarInt.decode(buf)?;
128            let size: u32 = types::UnsignedVarInt.decode(buf)?;
129            let unknown_value = buf.try_get_bytes(size as usize)?;
130            unknown_tagged_fields.insert(tag as i32, unknown_value);
131        }
132        Ok(Self {
133            _type,
134            name,
135            token_authenticated,
136            unknown_tagged_fields,
137        })
138    }
139}
140
141impl Default for DefaultPrincipalData {
142    fn default() -> Self {
143        Self {
144            _type: Default::default(),
145            name: Default::default(),
146            token_authenticated: false,
147            unknown_tagged_fields: BTreeMap::new(),
148        }
149    }
150}
151
152impl Message for DefaultPrincipalData {
153    const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
154    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
155}