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