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