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