Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
CreateDelegationTokenRequest.borrowed.rs

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use bytes::BufMut;
3
4use crate::{
5    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6    primitives::{
7        fixed::{get_i64, put_i64},
8        string_bytes::{
9            compact_nullable_string_len, compact_string_len, nullable_string_len,
10            put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
11            string_len,
12        },
13        string_bytes_borrowed::{
14            get_compact_nullable_string_borrowed, get_compact_string_borrowed,
15            get_nullable_string_borrowed, get_string_borrowed,
16        },
17    },
18    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
19};
20pub const API_KEY: i16 = 38;
21pub const MIN_VERSION: i16 = 1;
22pub const MAX_VERSION: i16 = 3;
23pub const LATEST_STABLE_VERSION: i16 = 3;
24pub const FLEXIBLE_MIN: i16 = 2;
25#[inline]
26#[must_use]
27pub fn is_flexible(version: i16) -> bool {
28    version >= FLEXIBLE_MIN
29}
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct CreateDelegationTokenRequest<'a> {
32    pub owner_principal_type: Option<&'a str>,
33    pub owner_principal_name: Option<&'a str>,
34    pub renewers: Vec<CreatableRenewers<'a>>,
35    pub max_lifetime_ms: i64,
36    pub unknown_tagged_fields: UnknownTaggedFields,
37}
38impl Default for CreateDelegationTokenRequest<'_> {
39    fn default() -> Self {
40        Self {
41            owner_principal_type: Some(""),
42            owner_principal_name: Some(""),
43            renewers: Vec::new(),
44            max_lifetime_ms: 0i64,
45            unknown_tagged_fields: UnknownTaggedFields::default(),
46        }
47    }
48}
49impl CreateDelegationTokenRequest<'_> {
50    /// # Panics
51    ///
52    /// Panics if a records field contains an invalid encoded record batch.
53    #[must_use]
54    pub fn to_owned(
55        &self,
56    ) -> crate::owned::create_delegation_token_request::CreateDelegationTokenRequest {
57        crate::owned::create_delegation_token_request::CreateDelegationTokenRequest {
58            owner_principal_type: (self.owner_principal_type).map(std::string::ToString::to_string),
59            owner_principal_name: (self.owner_principal_name).map(std::string::ToString::to_string),
60            renewers: (self.renewers)
61                .iter()
62                .map(CreatableRenewers::to_owned)
63                .collect(),
64            max_lifetime_ms: (self.max_lifetime_ms),
65            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
66        }
67    }
68}
69impl Encode for CreateDelegationTokenRequest<'_> {
70    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
71        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
72            return Err(ProtocolError::UnsupportedVersion {
73                api_key: API_KEY,
74                version,
75            });
76        }
77        let flex = is_flexible(version);
78        if version >= 3 {
79            if flex {
80                let () = put_compact_nullable_string(buf, self.owner_principal_type);
81            } else {
82                let () = put_nullable_string(buf, self.owner_principal_type);
83            }
84        }
85        if version >= 3 {
86            if flex {
87                let () = put_compact_nullable_string(buf, self.owner_principal_name);
88            } else {
89                let () = put_nullable_string(buf, self.owner_principal_name);
90            }
91        }
92        if version >= 0 {
93            {
94                crate::primitives::array::put_array_len(buf, (self.renewers).len(), flex);
95                for it in &self.renewers {
96                    it.encode(buf, version)?;
97                }
98            };
99        }
100        if version >= 0 {
101            put_i64(buf, self.max_lifetime_ms);
102        }
103        if flex {
104            let tagged = WriteTaggedFields::new();
105            tagged.write(buf, &self.unknown_tagged_fields);
106        }
107        Ok(())
108    }
109    fn encoded_len(&self, version: i16) -> usize {
110        let flex = is_flexible(version);
111        let mut n: usize = 0;
112        if version >= 3 {
113            n += if flex {
114                compact_nullable_string_len(self.owner_principal_type)
115            } else {
116                nullable_string_len(self.owner_principal_type)
117            };
118        }
119        if version >= 3 {
120            n += if flex {
121                compact_nullable_string_len(self.owner_principal_name)
122            } else {
123                nullable_string_len(self.owner_principal_name)
124            };
125        }
126        if version >= 0 {
127            n += {
128                let prefix =
129                    crate::primitives::array::array_len_prefix_len((self.renewers).len(), flex);
130                let body: usize = (self.renewers)
131                    .iter()
132                    .map(|it| it.encoded_len(version))
133                    .sum();
134                prefix + body
135            };
136        }
137        if version >= 0 {
138            n += 8;
139        }
140        if flex {
141            let known_pairs: Vec<(u32, usize)> = Vec::new();
142            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
143        }
144        n
145    }
146}
147impl<'de> DecodeBorrow<'de> for CreateDelegationTokenRequest<'de> {
148    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
149        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
150            return Err(ProtocolError::UnsupportedVersion {
151                api_key: API_KEY,
152                version,
153            });
154        }
155        let flex = is_flexible(version);
156        let mut out = Self::default();
157        if version >= 3 {
158            out.owner_principal_type = if flex {
159                get_compact_nullable_string_borrowed(buf)?
160            } else {
161                get_nullable_string_borrowed(buf)?
162            };
163        }
164        if version >= 3 {
165            out.owner_principal_name = if flex {
166                get_compact_nullable_string_borrowed(buf)?
167            } else {
168                get_nullable_string_borrowed(buf)?
169            };
170        }
171        if version >= 0 {
172            out.renewers = {
173                let n = crate::primitives::array::get_array_len(buf, flex)?;
174                let mut v = Vec::with_capacity(n);
175                for _ in 0..n {
176                    v.push(CreatableRenewers::decode_borrow(buf, version)?);
177                }
178                v
179            };
180        }
181        if version >= 0 {
182            out.max_lifetime_ms = get_i64(buf)?;
183        }
184        if flex {
185            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
186        }
187        Ok(out)
188    }
189}
190#[cfg(test)]
191impl CreateDelegationTokenRequest<'_> {
192    #[must_use]
193    pub fn populated(version: i16) -> Self {
194        let mut m = Self::default();
195        if version >= 3 {
196            m.owner_principal_type = Some("x");
197        }
198        if version >= 3 {
199            m.owner_principal_name = Some("x");
200        }
201        if version >= 0 {
202            m.renewers = vec![CreatableRenewers::populated(version)];
203        }
204        if version >= 0 {
205            m.max_lifetime_ms = 1i64;
206        }
207        m
208    }
209}
210#[derive(Debug, Clone, PartialEq, Eq, Default)]
211pub struct CreatableRenewers<'a> {
212    pub principal_type: &'a str,
213    pub principal_name: &'a str,
214    pub unknown_tagged_fields: UnknownTaggedFields,
215}
216impl CreatableRenewers<'_> {
217    /// # Panics
218    ///
219    /// Panics if a records field contains an invalid encoded record batch.
220    #[must_use]
221    pub fn to_owned(&self) -> crate::owned::create_delegation_token_request::CreatableRenewers {
222        crate::owned::create_delegation_token_request::CreatableRenewers {
223            principal_type: (self.principal_type).to_string(),
224            principal_name: (self.principal_name).to_string(),
225            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
226        }
227    }
228}
229impl Encode for CreatableRenewers<'_> {
230    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
231        let flex = version >= 2;
232        if version >= 0 {
233            if flex {
234                let () = put_compact_string(buf, self.principal_type);
235            } else {
236                let () = put_string(buf, self.principal_type);
237            }
238        }
239        if version >= 0 {
240            if flex {
241                let () = put_compact_string(buf, self.principal_name);
242            } else {
243                let () = put_string(buf, self.principal_name);
244            }
245        }
246        if flex {
247            let tagged = WriteTaggedFields::new();
248            tagged.write(buf, &self.unknown_tagged_fields);
249        }
250        Ok(())
251    }
252    fn encoded_len(&self, version: i16) -> usize {
253        let flex = version >= 2;
254        let mut n: usize = 0;
255        if version >= 0 {
256            n += if flex {
257                compact_string_len(self.principal_type)
258            } else {
259                string_len(self.principal_type)
260            };
261        }
262        if version >= 0 {
263            n += if flex {
264                compact_string_len(self.principal_name)
265            } else {
266                string_len(self.principal_name)
267            };
268        }
269        if flex {
270            let known_pairs: Vec<(u32, usize)> = Vec::new();
271            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
272        }
273        n
274    }
275}
276impl<'de> DecodeBorrow<'de> for CreatableRenewers<'de> {
277    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
278        let flex = version >= 2;
279        let mut out = Self::default();
280        if version >= 0 {
281            out.principal_type = if flex {
282                get_compact_string_borrowed(buf)?
283            } else {
284                get_string_borrowed(buf)?
285            };
286        }
287        if version >= 0 {
288            out.principal_name = if flex {
289                get_compact_string_borrowed(buf)?
290            } else {
291                get_string_borrowed(buf)?
292            };
293        }
294        if flex {
295            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
296        }
297        Ok(out)
298    }
299}
300#[cfg(test)]
301impl CreatableRenewers<'_> {
302    #[must_use]
303    pub fn populated(version: i16) -> Self {
304        let mut m = Self::default();
305        if version >= 0 {
306            m.principal_type = "x";
307        }
308        if version >= 0 {
309            m.principal_name = "x";
310        }
311        m
312    }
313}