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