1use bytes::{BufMut, Bytes};
3
4use crate::{
5 DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6 primitives::{
7 fixed::{get_i16, get_i32, put_i16, put_i32},
8 string_bytes::{
9 compact_nullable_string_len, compact_string_len, nullable_string_len, put_bytes,
10 put_compact_bytes, put_compact_nullable_string, put_compact_string,
11 put_nullable_string, put_string, string_len,
12 },
13 string_bytes_borrowed::{
14 get_bytes_borrowed, get_compact_bytes_borrowed, get_compact_nullable_string_borrowed,
15 get_compact_string_borrowed, 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 = 15;
21pub const MIN_VERSION: i16 = 0;
22pub const MAX_VERSION: i16 = 6;
23pub const FLEXIBLE_MIN: i16 = 5;
24#[inline]
25#[must_use]
26pub fn is_flexible(version: i16) -> bool {
27 version >= FLEXIBLE_MIN
28}
29#[derive(Debug, Clone, PartialEq, Eq, Default)]
30pub struct DescribeGroupsResponse<'a> {
31 pub throttle_time_ms: i32,
32 pub groups: Vec<DescribedGroup<'a>>,
33 pub unknown_tagged_fields: UnknownTaggedFields,
34}
35impl DescribeGroupsResponse<'_> {
36 #[must_use]
40 pub fn to_owned(&self) -> crate::owned::describe_groups_response::DescribeGroupsResponse {
41 crate::owned::describe_groups_response::DescribeGroupsResponse {
42 throttle_time_ms: (self.throttle_time_ms),
43 groups: (self.groups).iter().map(DescribedGroup::to_owned).collect(),
44 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
45 }
46 }
47}
48impl Encode for DescribeGroupsResponse<'_> {
49 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
50 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
51 return Err(ProtocolError::UnsupportedVersion {
52 api_key: API_KEY,
53 version,
54 });
55 }
56 let flex = is_flexible(version);
57 if version >= 1 {
58 put_i32(buf, self.throttle_time_ms);
59 }
60 if version >= 0 {
61 {
62 crate::primitives::array::put_array_len(buf, (self.groups).len(), flex);
63 for it in &self.groups {
64 it.encode(buf, version)?;
65 }
66 };
67 }
68 if flex {
69 let tagged = WriteTaggedFields::new();
70 tagged.write(buf, &self.unknown_tagged_fields);
71 }
72 Ok(())
73 }
74 fn encoded_len(&self, version: i16) -> usize {
75 let flex = is_flexible(version);
76 let mut n: usize = 0;
77 if version >= 1 {
78 n += 4;
79 }
80 if version >= 0 {
81 n += {
82 let prefix =
83 crate::primitives::array::array_len_prefix_len((self.groups).len(), flex);
84 let body: usize = (self.groups).iter().map(|it| it.encoded_len(version)).sum();
85 prefix + body
86 };
87 }
88 if flex {
89 let known_pairs: Vec<(u32, usize)> = Vec::new();
90 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
91 }
92 n
93 }
94}
95impl<'de> DecodeBorrow<'de> for DescribeGroupsResponse<'de> {
96 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
97 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
98 return Err(ProtocolError::UnsupportedVersion {
99 api_key: API_KEY,
100 version,
101 });
102 }
103 let flex = is_flexible(version);
104 let mut out = Self::default();
105 if version >= 1 {
106 out.throttle_time_ms = get_i32(buf)?;
107 }
108 if version >= 0 {
109 out.groups = {
110 let n = crate::primitives::array::get_array_len(buf, flex)?;
111 let mut v = Vec::with_capacity(n);
112 for _ in 0..n {
113 v.push(DescribedGroup::decode_borrow(buf, version)?);
114 }
115 v
116 };
117 }
118 if flex {
119 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
120 }
121 Ok(out)
122 }
123}
124#[cfg(test)]
125impl DescribeGroupsResponse<'_> {
126 #[must_use]
127 pub fn populated(version: i16) -> Self {
128 let mut m = Self::default();
129 if version >= 1 {
130 m.throttle_time_ms = 1i32;
131 }
132 if version >= 0 {
133 m.groups = vec![DescribedGroup::populated(version)];
134 }
135 m
136 }
137}
138#[derive(Debug, Clone, PartialEq, Eq)]
139pub struct DescribedGroup<'a> {
140 pub error_code: i16,
141 pub error_message: Option<&'a str>,
142 pub group_id: &'a str,
143 pub group_state: &'a str,
144 pub protocol_type: &'a str,
145 pub protocol_data: &'a str,
146 pub members: Vec<DescribedGroupMember<'a>>,
147 pub authorized_operations: i32,
148 pub unknown_tagged_fields: UnknownTaggedFields,
149}
150impl Default for DescribedGroup<'_> {
151 fn default() -> Self {
152 Self {
153 error_code: 0i16,
154 error_message: None,
155 group_id: "",
156 group_state: "",
157 protocol_type: "",
158 protocol_data: "",
159 members: Vec::new(),
160 authorized_operations: -2_147_483_648i32,
161 unknown_tagged_fields: UnknownTaggedFields::default(),
162 }
163 }
164}
165impl<'a> DescribedGroup<'a> {
166 #[must_use]
170 pub fn to_owned(&self) -> crate::owned::describe_groups_response::DescribedGroup {
171 crate::owned::describe_groups_response::DescribedGroup {
172 error_code: (self.error_code),
173 error_message: (self.error_message).map(std::string::ToString::to_string),
174 group_id: (self.group_id).to_string(),
175 group_state: (self.group_state).to_string(),
176 protocol_type: (self.protocol_type).to_string(),
177 protocol_data: (self.protocol_data).to_string(),
178 members: (self.members)
179 .iter()
180 .map(DescribedGroupMember::to_owned)
181 .collect(),
182 authorized_operations: (self.authorized_operations),
183 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
184 }
185 }
186 fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
187 if version >= 0 {
188 put_i16(buf, self.error_code);
189 }
190 }
191 fn encode_field_1<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
192 if version >= 6 {
193 if flex {
194 let () = put_compact_nullable_string(buf, self.error_message);
195 } else {
196 let () = put_nullable_string(buf, self.error_message);
197 }
198 }
199 }
200 fn encode_field_2<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
201 if version >= 0 {
202 if flex {
203 let () = put_compact_string(buf, self.group_id);
204 } else {
205 let () = put_string(buf, self.group_id);
206 }
207 }
208 }
209 fn encode_field_3<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
210 if version >= 0 {
211 if flex {
212 let () = put_compact_string(buf, self.group_state);
213 } else {
214 let () = put_string(buf, self.group_state);
215 }
216 }
217 }
218 fn encode_field_4<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
219 if version >= 0 {
220 if flex {
221 let () = put_compact_string(buf, self.protocol_type);
222 } else {
223 let () = put_string(buf, self.protocol_type);
224 }
225 }
226 }
227 fn encode_field_5<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
228 if version >= 0 {
229 if flex {
230 let () = put_compact_string(buf, self.protocol_data);
231 } else {
232 let () = put_string(buf, self.protocol_data);
233 }
234 }
235 }
236 fn encode_field_6<B: BufMut>(
237 &self,
238 buf: &mut B,
239 version: i16,
240 flex: bool,
241 ) -> Result<(), ProtocolError> {
242 if version >= 0 {
243 {
244 crate::primitives::array::put_array_len(buf, (self.members).len(), flex);
245 for it in &self.members {
246 it.encode(buf, version)?;
247 }
248 };
249 }
250 Ok(())
251 }
252 fn encode_field_7<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
253 if version >= 3 {
254 put_i32(buf, self.authorized_operations);
255 }
256 }
257 fn encode_tagged_fields<B: BufMut>(&self, buf: &mut B, _version: i16, flex: bool) {
258 if flex {
259 let tagged = WriteTaggedFields::new();
260 tagged.write(buf, &self.unknown_tagged_fields);
261 }
262 }
263 fn decode_field_0(
264 out: &mut Self,
265 buf: &mut &'a [u8],
266 version: i16,
267 _flex: bool,
268 ) -> Result<(), ProtocolError> {
269 if version >= 0 {
270 out.error_code = get_i16(buf)?;
271 }
272 Ok(())
273 }
274 fn decode_field_1(
275 out: &mut Self,
276 buf: &mut &'a [u8],
277 version: i16,
278 flex: bool,
279 ) -> Result<(), ProtocolError> {
280 if version >= 6 {
281 out.error_message = if flex {
282 get_compact_nullable_string_borrowed(buf)?
283 } else {
284 get_nullable_string_borrowed(buf)?
285 };
286 }
287 Ok(())
288 }
289 fn decode_field_2(
290 out: &mut Self,
291 buf: &mut &'a [u8],
292 version: i16,
293 flex: bool,
294 ) -> Result<(), ProtocolError> {
295 if version >= 0 {
296 out.group_id = if flex {
297 get_compact_string_borrowed(buf)?
298 } else {
299 get_string_borrowed(buf)?
300 };
301 }
302 Ok(())
303 }
304 fn decode_field_3(
305 out: &mut Self,
306 buf: &mut &'a [u8],
307 version: i16,
308 flex: bool,
309 ) -> Result<(), ProtocolError> {
310 if version >= 0 {
311 out.group_state = if flex {
312 get_compact_string_borrowed(buf)?
313 } else {
314 get_string_borrowed(buf)?
315 };
316 }
317 Ok(())
318 }
319 fn decode_field_4(
320 out: &mut Self,
321 buf: &mut &'a [u8],
322 version: i16,
323 flex: bool,
324 ) -> Result<(), ProtocolError> {
325 if version >= 0 {
326 out.protocol_type = if flex {
327 get_compact_string_borrowed(buf)?
328 } else {
329 get_string_borrowed(buf)?
330 };
331 }
332 Ok(())
333 }
334 fn decode_field_5(
335 out: &mut Self,
336 buf: &mut &'a [u8],
337 version: i16,
338 flex: bool,
339 ) -> Result<(), ProtocolError> {
340 if version >= 0 {
341 out.protocol_data = if flex {
342 get_compact_string_borrowed(buf)?
343 } else {
344 get_string_borrowed(buf)?
345 };
346 }
347 Ok(())
348 }
349 fn decode_field_6(
350 out: &mut Self,
351 buf: &mut &'a [u8],
352 version: i16,
353 flex: bool,
354 ) -> Result<(), ProtocolError> {
355 if version >= 0 {
356 out.members = {
357 let n = crate::primitives::array::get_array_len(buf, flex)?;
358 let mut v = Vec::with_capacity(n);
359 for _ in 0..n {
360 v.push(DescribedGroupMember::decode_borrow(buf, version)?);
361 }
362 v
363 };
364 }
365 Ok(())
366 }
367 fn decode_field_7(
368 out: &mut Self,
369 buf: &mut &'a [u8],
370 version: i16,
371 _flex: bool,
372 ) -> Result<(), ProtocolError> {
373 if version >= 3 {
374 out.authorized_operations = get_i32(buf)?;
375 }
376 Ok(())
377 }
378 fn decode_tagged_fields(
379 out: &mut Self,
380 buf: &mut &'a [u8],
381 _version: i16,
382 flex: bool,
383 ) -> Result<(), ProtocolError> {
384 if flex {
385 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
386 }
387 Ok(())
388 }
389}
390impl Encode for DescribedGroup<'_> {
391 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
392 let flex = version >= 5;
393 self.encode_field_0(buf, version, flex);
394 self.encode_field_1(buf, version, flex);
395 self.encode_field_2(buf, version, flex);
396 self.encode_field_3(buf, version, flex);
397 self.encode_field_4(buf, version, flex);
398 self.encode_field_5(buf, version, flex);
399 self.encode_field_6(buf, version, flex)?;
400 self.encode_field_7(buf, version, flex);
401 self.encode_tagged_fields(buf, version, flex);
402 Ok(())
403 }
404 fn encoded_len(&self, version: i16) -> usize {
405 let flex = version >= 5;
406 let mut n: usize = 0;
407 if version >= 0 {
408 n += 2;
409 }
410 if version >= 6 {
411 n += if flex {
412 compact_nullable_string_len(self.error_message)
413 } else {
414 nullable_string_len(self.error_message)
415 };
416 }
417 if version >= 0 {
418 n += if flex {
419 compact_string_len(self.group_id)
420 } else {
421 string_len(self.group_id)
422 };
423 }
424 if version >= 0 {
425 n += if flex {
426 compact_string_len(self.group_state)
427 } else {
428 string_len(self.group_state)
429 };
430 }
431 if version >= 0 {
432 n += if flex {
433 compact_string_len(self.protocol_type)
434 } else {
435 string_len(self.protocol_type)
436 };
437 }
438 if version >= 0 {
439 n += if flex {
440 compact_string_len(self.protocol_data)
441 } else {
442 string_len(self.protocol_data)
443 };
444 }
445 if version >= 0 {
446 n += {
447 let prefix =
448 crate::primitives::array::array_len_prefix_len((self.members).len(), flex);
449 let body: usize = (self.members)
450 .iter()
451 .map(|it| it.encoded_len(version))
452 .sum();
453 prefix + body
454 };
455 }
456 if version >= 3 {
457 n += 4;
458 }
459 if flex {
460 let known_pairs: Vec<(u32, usize)> = Vec::new();
461 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
462 }
463 n
464 }
465}
466impl<'de> DecodeBorrow<'de> for DescribedGroup<'de> {
467 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
468 let flex = version >= 5;
469 let mut out = Self::default();
470 Self::decode_field_0(&mut out, buf, version, flex)?;
471 Self::decode_field_1(&mut out, buf, version, flex)?;
472 Self::decode_field_2(&mut out, buf, version, flex)?;
473 Self::decode_field_3(&mut out, buf, version, flex)?;
474 Self::decode_field_4(&mut out, buf, version, flex)?;
475 Self::decode_field_5(&mut out, buf, version, flex)?;
476 Self::decode_field_6(&mut out, buf, version, flex)?;
477 Self::decode_field_7(&mut out, buf, version, flex)?;
478 Self::decode_tagged_fields(&mut out, buf, version, flex)?;
479 Ok(out)
480 }
481}
482#[cfg(test)]
483impl DescribedGroup<'_> {
484 #[must_use]
485 pub fn populated(version: i16) -> Self {
486 let mut m = Self::default();
487 if version >= 0 {
488 m.error_code = 1i16;
489 }
490 if version >= 6 {
491 m.error_message = Some("x");
492 }
493 if version >= 0 {
494 m.group_id = "x";
495 }
496 if version >= 0 {
497 m.group_state = "x";
498 }
499 if version >= 0 {
500 m.protocol_type = "x";
501 }
502 if version >= 0 {
503 m.protocol_data = "x";
504 }
505 if version >= 0 {
506 m.members = vec![DescribedGroupMember::populated(version)];
507 }
508 if version >= 3 {
509 m.authorized_operations = 1i32;
510 }
511 m
512 }
513}
514#[derive(Debug, Clone, PartialEq, Eq, Default)]
515pub struct DescribedGroupMember<'a> {
516 pub member_id: &'a str,
517 pub group_instance_id: Option<&'a str>,
518 pub client_id: &'a str,
519 pub client_host: &'a str,
520 pub member_metadata: &'a [u8],
521 pub member_assignment: &'a [u8],
522 pub unknown_tagged_fields: UnknownTaggedFields,
523}
524impl DescribedGroupMember<'_> {
525 #[must_use]
529 pub fn to_owned(&self) -> crate::owned::describe_groups_response::DescribedGroupMember {
530 crate::owned::describe_groups_response::DescribedGroupMember {
531 member_id: (self.member_id).to_string(),
532 group_instance_id: (self.group_instance_id).map(std::string::ToString::to_string),
533 client_id: (self.client_id).to_string(),
534 client_host: (self.client_host).to_string(),
535 member_metadata: Bytes::copy_from_slice(self.member_metadata),
536 member_assignment: Bytes::copy_from_slice(self.member_assignment),
537 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
538 }
539 }
540}
541impl Encode for DescribedGroupMember<'_> {
542 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
543 let flex = version >= 5;
544 if version >= 0 {
545 if flex {
546 let () = put_compact_string(buf, self.member_id);
547 } else {
548 let () = put_string(buf, self.member_id);
549 }
550 }
551 if version >= 4 {
552 if flex {
553 let () = put_compact_nullable_string(buf, self.group_instance_id);
554 } else {
555 let () = put_nullable_string(buf, self.group_instance_id);
556 }
557 }
558 if version >= 0 {
559 if flex {
560 let () = put_compact_string(buf, self.client_id);
561 } else {
562 let () = put_string(buf, self.client_id);
563 }
564 }
565 if version >= 0 {
566 if flex {
567 let () = put_compact_string(buf, self.client_host);
568 } else {
569 let () = put_string(buf, self.client_host);
570 }
571 }
572 if version >= 0 {
573 if flex {
574 let () = put_compact_bytes(buf, self.member_metadata);
575 } else {
576 let () = put_bytes(buf, self.member_metadata);
577 }
578 }
579 if version >= 0 {
580 if flex {
581 let () = put_compact_bytes(buf, self.member_assignment);
582 } else {
583 let () = put_bytes(buf, self.member_assignment);
584 }
585 }
586 if flex {
587 let tagged = WriteTaggedFields::new();
588 tagged.write(buf, &self.unknown_tagged_fields);
589 }
590 Ok(())
591 }
592 fn encoded_len(&self, version: i16) -> usize {
593 let flex = version >= 5;
594 let mut n: usize = 0;
595 if version >= 0 {
596 n += if flex {
597 compact_string_len(self.member_id)
598 } else {
599 string_len(self.member_id)
600 };
601 }
602 if version >= 4 {
603 n += if flex {
604 compact_nullable_string_len(self.group_instance_id)
605 } else {
606 nullable_string_len(self.group_instance_id)
607 };
608 }
609 if version >= 0 {
610 n += if flex {
611 compact_string_len(self.client_id)
612 } else {
613 string_len(self.client_id)
614 };
615 }
616 if version >= 0 {
617 n += if flex {
618 compact_string_len(self.client_host)
619 } else {
620 string_len(self.client_host)
621 };
622 }
623 if version >= 0 {
624 n += if flex {
625 crate::primitives::varint::uvarint_len(
626 u32::try_from((self.member_metadata).len() + 1).unwrap(),
627 ) + (self.member_metadata).len()
628 } else {
629 4 + (self.member_metadata).len()
630 };
631 }
632 if version >= 0 {
633 n += if flex {
634 crate::primitives::varint::uvarint_len(
635 u32::try_from((self.member_assignment).len() + 1).unwrap(),
636 ) + (self.member_assignment).len()
637 } else {
638 4 + (self.member_assignment).len()
639 };
640 }
641 if flex {
642 let known_pairs: Vec<(u32, usize)> = Vec::new();
643 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
644 }
645 n
646 }
647}
648impl<'de> DecodeBorrow<'de> for DescribedGroupMember<'de> {
649 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
650 let flex = version >= 5;
651 let mut out = Self::default();
652 if version >= 0 {
653 out.member_id = if flex {
654 get_compact_string_borrowed(buf)?
655 } else {
656 get_string_borrowed(buf)?
657 };
658 }
659 if version >= 4 {
660 out.group_instance_id = if flex {
661 get_compact_nullable_string_borrowed(buf)?
662 } else {
663 get_nullable_string_borrowed(buf)?
664 };
665 }
666 if version >= 0 {
667 out.client_id = if flex {
668 get_compact_string_borrowed(buf)?
669 } else {
670 get_string_borrowed(buf)?
671 };
672 }
673 if version >= 0 {
674 out.client_host = if flex {
675 get_compact_string_borrowed(buf)?
676 } else {
677 get_string_borrowed(buf)?
678 };
679 }
680 if version >= 0 {
681 out.member_metadata = if flex {
682 get_compact_bytes_borrowed(buf)?
683 } else {
684 get_bytes_borrowed(buf)?
685 };
686 }
687 if version >= 0 {
688 out.member_assignment = if flex {
689 get_compact_bytes_borrowed(buf)?
690 } else {
691 get_bytes_borrowed(buf)?
692 };
693 }
694 if flex {
695 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
696 }
697 Ok(out)
698 }
699}
700#[cfg(test)]
701impl DescribedGroupMember<'_> {
702 #[must_use]
703 pub fn populated(version: i16) -> Self {
704 let mut m = Self::default();
705 if version >= 0 {
706 m.member_id = "x";
707 }
708 if version >= 4 {
709 m.group_instance_id = Some("x");
710 }
711 if version >= 0 {
712 m.client_id = "x";
713 }
714 if version >= 0 {
715 m.client_host = "x";
716 }
717 if version >= 0 {
718 m.member_metadata = &b"x"[..];
719 }
720 if version >= 0 {
721 m.member_assignment = &b"x"[..];
722 }
723 m
724 }
725}