1use bytes::{Buf, BufMut};
4
5use crate::{
6 Decode, Encode, ProtocolError, UnknownTaggedFields,
7 primitives::{
8 fixed::{get_i16, get_i32, get_i64, put_i16, put_i32, put_i64},
9 string_bytes::{
10 bytes_len, compact_bytes_len, compact_string_len, get_bytes_owned,
11 get_compact_bytes_owned, get_compact_string_owned, get_string_owned, put_bytes,
12 put_compact_bytes, put_compact_string, put_string, string_len,
13 },
14 },
15 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
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#[inline]
22#[must_use]
23pub fn is_flexible(version: i16) -> bool {
24 version >= FLEXIBLE_MIN
25}
26#[derive(Debug, Clone, PartialEq, Eq, Default)]
27pub struct CreateDelegationTokenResponse {
28 pub error_code: i16,
29 pub principal_type: String,
30 pub principal_name: String,
31 pub token_requester_principal_type: String,
32 pub token_requester_principal_name: String,
33 pub issue_timestamp_ms: i64,
34 pub expiry_timestamp_ms: i64,
35 pub max_timestamp_ms: i64,
36 pub token_id: String,
37 pub hmac: ::bytes::Bytes,
38 pub throttle_time_ms: i32,
39 pub unknown_tagged_fields: UnknownTaggedFields,
40}
41impl CreateDelegationTokenResponse {
42 fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
43 if version >= 0 {
44 put_i16(buf, self.error_code);
45 }
46 }
47 fn encode_field_1<B: BufMut>(
48 &self,
49 buf: &mut B,
50 version: i16,
51 flex: bool,
52 ) -> Result<(), ProtocolError> {
53 if version >= 0 {
54 if flex {
55 put_compact_string(buf, &self.principal_type)?;
56 } else {
57 put_string(buf, &self.principal_type)?;
58 }
59 }
60 Ok(())
61 }
62 fn encode_field_2<B: BufMut>(
63 &self,
64 buf: &mut B,
65 version: i16,
66 flex: bool,
67 ) -> Result<(), ProtocolError> {
68 if version >= 0 {
69 if flex {
70 put_compact_string(buf, &self.principal_name)?;
71 } else {
72 put_string(buf, &self.principal_name)?;
73 }
74 }
75 Ok(())
76 }
77 fn encode_field_3<B: BufMut>(
78 &self,
79 buf: &mut B,
80 version: i16,
81 flex: bool,
82 ) -> Result<(), ProtocolError> {
83 if version >= 3 {
84 if flex {
85 put_compact_string(buf, &self.token_requester_principal_type)?;
86 } else {
87 put_string(buf, &self.token_requester_principal_type)?;
88 }
89 }
90 Ok(())
91 }
92 fn encode_field_4<B: BufMut>(
93 &self,
94 buf: &mut B,
95 version: i16,
96 flex: bool,
97 ) -> Result<(), ProtocolError> {
98 if version >= 3 {
99 if flex {
100 put_compact_string(buf, &self.token_requester_principal_name)?;
101 } else {
102 put_string(buf, &self.token_requester_principal_name)?;
103 }
104 }
105 Ok(())
106 }
107 fn encode_field_5<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
108 if version >= 0 {
109 put_i64(buf, self.issue_timestamp_ms);
110 }
111 }
112 fn encode_field_6<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
113 if version >= 0 {
114 put_i64(buf, self.expiry_timestamp_ms);
115 }
116 }
117 fn encode_field_7<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
118 if version >= 0 {
119 put_i64(buf, self.max_timestamp_ms);
120 }
121 }
122 fn encode_field_8<B: BufMut>(
123 &self,
124 buf: &mut B,
125 version: i16,
126 flex: bool,
127 ) -> Result<(), ProtocolError> {
128 if version >= 0 {
129 if flex {
130 put_compact_string(buf, &self.token_id)?;
131 } else {
132 put_string(buf, &self.token_id)?;
133 }
134 }
135 Ok(())
136 }
137 fn encode_field_9<B: BufMut>(
138 &self,
139 buf: &mut B,
140 version: i16,
141 flex: bool,
142 ) -> Result<(), ProtocolError> {
143 if version >= 0 {
144 if flex {
145 put_compact_bytes(buf, &self.hmac)?;
146 } else {
147 put_bytes(buf, &self.hmac)?;
148 }
149 }
150 Ok(())
151 }
152 fn encode_field_10<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
153 if version >= 0 {
154 put_i32(buf, self.throttle_time_ms);
155 }
156 }
157 fn encode_tagged_fields<B: BufMut>(
158 &self,
159 buf: &mut B,
160 _version: i16,
161 flex: bool,
162 ) -> Result<(), ProtocolError> {
163 if flex {
164 let tagged = WriteTaggedFields::new();
165 tagged.write(buf, &self.unknown_tagged_fields)?;
166 }
167 Ok(())
168 }
169 fn decode_field_0<B: Buf>(
170 out: &mut Self,
171 buf: &mut B,
172 version: i16,
173 _flex: bool,
174 ) -> Result<(), ProtocolError> {
175 if version >= 0 {
176 out.error_code = get_i16(buf)?;
177 }
178 Ok(())
179 }
180 fn decode_field_1<B: Buf>(
181 out: &mut Self,
182 buf: &mut B,
183 version: i16,
184 flex: bool,
185 ) -> Result<(), ProtocolError> {
186 if version >= 0 {
187 out.principal_type = if flex {
188 get_compact_string_owned(buf)?
189 } else {
190 get_string_owned(buf)?
191 };
192 }
193 Ok(())
194 }
195 fn decode_field_2<B: Buf>(
196 out: &mut Self,
197 buf: &mut B,
198 version: i16,
199 flex: bool,
200 ) -> Result<(), ProtocolError> {
201 if version >= 0 {
202 out.principal_name = if flex {
203 get_compact_string_owned(buf)?
204 } else {
205 get_string_owned(buf)?
206 };
207 }
208 Ok(())
209 }
210 fn decode_field_3<B: Buf>(
211 out: &mut Self,
212 buf: &mut B,
213 version: i16,
214 flex: bool,
215 ) -> Result<(), ProtocolError> {
216 if version >= 3 {
217 out.token_requester_principal_type = if flex {
218 get_compact_string_owned(buf)?
219 } else {
220 get_string_owned(buf)?
221 };
222 }
223 Ok(())
224 }
225 fn decode_field_4<B: Buf>(
226 out: &mut Self,
227 buf: &mut B,
228 version: i16,
229 flex: bool,
230 ) -> Result<(), ProtocolError> {
231 if version >= 3 {
232 out.token_requester_principal_name = if flex {
233 get_compact_string_owned(buf)?
234 } else {
235 get_string_owned(buf)?
236 };
237 }
238 Ok(())
239 }
240 fn decode_field_5<B: Buf>(
241 out: &mut Self,
242 buf: &mut B,
243 version: i16,
244 _flex: bool,
245 ) -> Result<(), ProtocolError> {
246 if version >= 0 {
247 out.issue_timestamp_ms = get_i64(buf)?;
248 }
249 Ok(())
250 }
251 fn decode_field_6<B: Buf>(
252 out: &mut Self,
253 buf: &mut B,
254 version: i16,
255 _flex: bool,
256 ) -> Result<(), ProtocolError> {
257 if version >= 0 {
258 out.expiry_timestamp_ms = get_i64(buf)?;
259 }
260 Ok(())
261 }
262 fn decode_field_7<B: Buf>(
263 out: &mut Self,
264 buf: &mut B,
265 version: i16,
266 _flex: bool,
267 ) -> Result<(), ProtocolError> {
268 if version >= 0 {
269 out.max_timestamp_ms = get_i64(buf)?;
270 }
271 Ok(())
272 }
273 fn decode_field_8<B: Buf>(
274 out: &mut Self,
275 buf: &mut B,
276 version: i16,
277 flex: bool,
278 ) -> Result<(), ProtocolError> {
279 if version >= 0 {
280 out.token_id = if flex {
281 get_compact_string_owned(buf)?
282 } else {
283 get_string_owned(buf)?
284 };
285 }
286 Ok(())
287 }
288 fn decode_field_9<B: Buf>(
289 out: &mut Self,
290 buf: &mut B,
291 version: i16,
292 flex: bool,
293 ) -> Result<(), ProtocolError> {
294 if version >= 0 {
295 out.hmac = if flex {
296 get_compact_bytes_owned(buf)?
297 } else {
298 get_bytes_owned(buf)?
299 };
300 }
301 Ok(())
302 }
303 fn decode_field_10<B: Buf>(
304 out: &mut Self,
305 buf: &mut B,
306 version: i16,
307 _flex: bool,
308 ) -> Result<(), ProtocolError> {
309 if version >= 0 {
310 out.throttle_time_ms = get_i32(buf)?;
311 }
312 Ok(())
313 }
314 fn decode_tagged_fields<B: Buf>(
315 out: &mut Self,
316 buf: &mut B,
317 _version: i16,
318 flex: bool,
319 ) -> Result<(), ProtocolError> {
320 if flex {
321 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
322 }
323 Ok(())
324 }
325}
326impl Encode for CreateDelegationTokenResponse {
327 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
328 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
329 return Err(ProtocolError::UnsupportedVersion {
330 api_key: API_KEY,
331 version,
332 });
333 }
334 let flex = is_flexible(version);
335 self.encode_field_0(buf, version, flex);
336 self.encode_field_1(buf, version, flex)?;
337 self.encode_field_2(buf, version, flex)?;
338 self.encode_field_3(buf, version, flex)?;
339 self.encode_field_4(buf, version, flex)?;
340 self.encode_field_5(buf, version, flex);
341 self.encode_field_6(buf, version, flex);
342 self.encode_field_7(buf, version, flex);
343 self.encode_field_8(buf, version, flex)?;
344 self.encode_field_9(buf, version, flex)?;
345 self.encode_field_10(buf, version, flex);
346 self.encode_tagged_fields(buf, version, flex)?;
347 Ok(())
348 }
349 fn encoded_len(&self, version: i16) -> usize {
350 let flex = is_flexible(version);
351 let mut n: usize = 0;
352 if version >= 0 {
353 n += 2;
354 }
355 if version >= 0 {
356 n += if flex {
357 compact_string_len(&self.principal_type)
358 } else {
359 string_len(&self.principal_type)
360 };
361 }
362 if version >= 0 {
363 n += if flex {
364 compact_string_len(&self.principal_name)
365 } else {
366 string_len(&self.principal_name)
367 };
368 }
369 if version >= 3 {
370 n += if flex {
371 compact_string_len(&self.token_requester_principal_type)
372 } else {
373 string_len(&self.token_requester_principal_type)
374 };
375 }
376 if version >= 3 {
377 n += if flex {
378 compact_string_len(&self.token_requester_principal_name)
379 } else {
380 string_len(&self.token_requester_principal_name)
381 };
382 }
383 if version >= 0 {
384 n += 8;
385 }
386 if version >= 0 {
387 n += 8;
388 }
389 if version >= 0 {
390 n += 8;
391 }
392 if version >= 0 {
393 n += if flex {
394 compact_string_len(&self.token_id)
395 } else {
396 string_len(&self.token_id)
397 };
398 }
399 if version >= 0 {
400 n += if flex {
401 compact_bytes_len(&self.hmac)
402 } else {
403 bytes_len(&self.hmac)
404 };
405 }
406 if version >= 0 {
407 n += 4;
408 }
409 if flex {
410 let known_pairs: Vec<(u32, usize)> = Vec::new();
411 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
412 }
413 n
414 }
415}
416impl Decode<'_> for CreateDelegationTokenResponse {
417 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
418 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
419 return Err(ProtocolError::UnsupportedVersion {
420 api_key: API_KEY,
421 version,
422 });
423 }
424 let flex = is_flexible(version);
425 let mut out = Self::default();
426 Self::decode_field_0(&mut out, buf, version, flex)?;
427 Self::decode_field_1(&mut out, buf, version, flex)?;
428 Self::decode_field_2(&mut out, buf, version, flex)?;
429 Self::decode_field_3(&mut out, buf, version, flex)?;
430 Self::decode_field_4(&mut out, buf, version, flex)?;
431 Self::decode_field_5(&mut out, buf, version, flex)?;
432 Self::decode_field_6(&mut out, buf, version, flex)?;
433 Self::decode_field_7(&mut out, buf, version, flex)?;
434 Self::decode_field_8(&mut out, buf, version, flex)?;
435 Self::decode_field_9(&mut out, buf, version, flex)?;
436 Self::decode_field_10(&mut out, buf, version, flex)?;
437 Self::decode_tagged_fields(&mut out, buf, version, flex)?;
438 Ok(out)
439 }
440}
441#[cfg(test)]
442impl CreateDelegationTokenResponse {
443 #[must_use]
444 pub fn populated(version: i16) -> Self {
445 let mut m = Self::default();
446 if version >= 0 {
447 m.error_code = 1i16;
448 }
449 if version >= 0 {
450 m.principal_type = "x".to_owned();
451 }
452 if version >= 0 {
453 m.principal_name = "x".to_owned();
454 }
455 if version >= 3 {
456 m.token_requester_principal_type = "x".to_owned();
457 }
458 if version >= 3 {
459 m.token_requester_principal_name = "x".to_owned();
460 }
461 if version >= 0 {
462 m.issue_timestamp_ms = 1i64;
463 }
464 if version >= 0 {
465 m.expiry_timestamp_ms = 1i64;
466 }
467 if version >= 0 {
468 m.max_timestamp_ms = 1i64;
469 }
470 if version >= 0 {
471 m.token_id = "x".to_owned();
472 }
473 if version >= 0 {
474 m.hmac = ::bytes::Bytes::from_static(b"x");
475 }
476 if version >= 0 {
477 m.throttle_time_ms = 1i32;
478 }
479 m
480 }
481}
482#[must_use]
485#[allow(unused_comparisons)]
486pub fn default_json(version: i16) -> ::serde_json::Value {
487 let mut obj = ::serde_json::Map::new();
488 obj.insert("errorCode".to_owned(), ::serde_json::json!(0));
489 obj.insert(
490 "principalType".to_owned(),
491 ::serde_json::Value::String(String::new()),
492 );
493 obj.insert(
494 "principalName".to_owned(),
495 ::serde_json::Value::String(String::new()),
496 );
497 if version >= 3 {
498 obj.insert(
499 "tokenRequesterPrincipalType".to_owned(),
500 ::serde_json::Value::String(String::new()),
501 );
502 }
503 if version >= 3 {
504 obj.insert(
505 "tokenRequesterPrincipalName".to_owned(),
506 ::serde_json::Value::String(String::new()),
507 );
508 }
509 obj.insert("issueTimestampMs".to_owned(), ::serde_json::json!(0));
510 obj.insert("expiryTimestampMs".to_owned(), ::serde_json::json!(0));
511 obj.insert("maxTimestampMs".to_owned(), ::serde_json::json!(0));
512 obj.insert(
513 "tokenId".to_owned(),
514 ::serde_json::Value::String(String::new()),
515 );
516 obj.insert(
517 "hmac".to_owned(),
518 ::serde_json::Value::String(String::new()),
519 );
520 obj.insert("throttleTimeMs".to_owned(), ::serde_json::json!(0));
521 ::serde_json::Value::Object(obj)
522}