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