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