Skip to main content

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

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