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