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