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