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        if version != 0 {
86            bail!("specified version not supported by this message type");
87        }
88        types::CompactString.encode(buf, &self._type)?;
89        types::CompactString.encode(buf, &self.name)?;
90        types::Boolean.encode(buf, &self.token_authenticated)?;
91        let num_tagged_fields = self.unknown_tagged_fields.len();
92        if num_tagged_fields > std::u32::MAX as usize {
93            bail!(
94                "Too many tagged fields to encode ({} fields)",
95                num_tagged_fields
96            );
97        }
98        types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
99
100        write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
101        Ok(())
102    }
103    fn compute_size(&self, version: i16) -> Result<usize> {
104        let mut total_size = 0;
105        total_size += types::CompactString.compute_size(&self._type)?;
106        total_size += types::CompactString.compute_size(&self.name)?;
107        total_size += types::Boolean.compute_size(&self.token_authenticated)?;
108        let num_tagged_fields = self.unknown_tagged_fields.len();
109        if num_tagged_fields > std::u32::MAX as usize {
110            bail!(
111                "Too many tagged fields to encode ({} fields)",
112                num_tagged_fields
113            );
114        }
115        total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
116
117        total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
118        Ok(total_size)
119    }
120}
121
122impl Decodable for DefaultPrincipalData {
123    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
124        if version != 0 {
125            bail!("specified version not supported by this message type");
126        }
127        let _type = types::CompactString.decode(buf)?;
128        let name = types::CompactString.decode(buf)?;
129        let token_authenticated = types::Boolean.decode(buf)?;
130        let mut unknown_tagged_fields = BTreeMap::new();
131        let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
132        for _ in 0..num_tagged_fields {
133            let tag: u32 = types::UnsignedVarInt.decode(buf)?;
134            let size: u32 = types::UnsignedVarInt.decode(buf)?;
135            let unknown_value = buf.try_get_bytes(size as usize)?;
136            unknown_tagged_fields.insert(tag as i32, unknown_value);
137        }
138        Ok(Self {
139            _type,
140            name,
141            token_authenticated,
142            unknown_tagged_fields,
143        })
144    }
145}
146
147impl Default for DefaultPrincipalData {
148    fn default() -> Self {
149        Self {
150            _type: Default::default(),
151            name: Default::default(),
152            token_authenticated: false,
153            unknown_tagged_fields: BTreeMap::new(),
154        }
155    }
156}
157
158impl Message for DefaultPrincipalData {
159    const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
160    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
161}