odradek_protocol/messages/
join_group_request.rs1#![allow(
6 clippy::field_reassign_with_default,
7 clippy::nonminimal_bool,
8 clippy::derivable_impls,
9 clippy::borrow_deref_ref,
10 clippy::double_parens,
11 unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21pub fn is_flexible(version: i16) -> bool {
23 let _ = version;
24 version >= 6
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct JoinGroupRequest {
31 pub group_id: String,
33 pub session_timeout_ms: i32,
35 pub rebalance_timeout_ms: i32,
37 pub member_id: String,
39 pub group_instance_id: Option<String>,
41 pub protocol_type: String,
43 pub protocols: Vec<JoinGroupRequestProtocol>,
45 pub reason: Option<String>,
47 pub unknown_tagged_fields: Vec<RawTaggedField>,
49}
50
51impl Default for JoinGroupRequest {
52 fn default() -> Self {
53 Self {
54 group_id: String::new(),
55 session_timeout_ms: 0,
56 rebalance_timeout_ms: -1,
57 member_id: String::new(),
58 group_instance_id: None,
59 protocol_type: String::new(),
60 protocols: Vec::new(),
61 reason: None,
62 unknown_tagged_fields: Vec::new(),
63 }
64 }
65}
66
67impl JoinGroupRequest {
68 pub const API_KEY: i16 = 11;
70 pub const MIN_VERSION: i16 = 0;
72 pub const MAX_VERSION: i16 = 9;
74
75 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
77 if is_flexible(version) {
78 wire::put_compact_string(buf, &self.group_id);
79 } else {
80 wire::put_string(buf, &self.group_id)?;
81 }
82 buf.put_i32(self.session_timeout_ms);
83 if version >= 1 {
84 buf.put_i32(self.rebalance_timeout_ms);
85 }
86 if is_flexible(version) {
87 wire::put_compact_string(buf, &self.member_id);
88 } else {
89 wire::put_string(buf, &self.member_id)?;
90 }
91 if version >= 5 {
92 if is_flexible(version) {
93 wire::put_compact_nullable_string(buf, self.group_instance_id.as_deref());
94 } else {
95 wire::put_nullable_string(buf, self.group_instance_id.as_deref())?;
96 }
97 }
98 if is_flexible(version) {
99 wire::put_compact_string(buf, &self.protocol_type);
100 } else {
101 wire::put_string(buf, &self.protocol_type)?;
102 }
103 if is_flexible(version) {
104 wire::put_compact_array_len(buf, Some(self.protocols.len()));
105 } else {
106 wire::put_array_len(buf, Some(self.protocols.len()))?;
107 }
108 for item in &self.protocols {
109 item.encode(buf, version)?;
110 }
111 if version >= 8 {
112 if is_flexible(version) {
113 wire::put_compact_nullable_string(buf, self.reason.as_deref());
114 } else {
115 wire::put_nullable_string(buf, self.reason.as_deref())?;
116 }
117 }
118 if is_flexible(version) {
119 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
120 }
121 Ok(())
122 }
123
124 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
127 Self::decode_with_limits(buf, version, Limits::default())
128 }
129
130 pub fn decode_with_limits(
132 buf: &mut impl Buf,
133 version: i16,
134 limits: Limits,
135 ) -> Result<Self, DecodeError> {
136 let mut budget = limits.budget(buf.remaining());
137 Self::decode_with_budget(buf, version, &mut budget)
138 }
139
140 pub fn decode_with_budget(
142 buf: &mut impl Buf,
143 version: i16,
144 budget: &mut Budget,
145 ) -> Result<Self, DecodeError> {
146 let mut this = Self::default();
147 this.group_id = if is_flexible(version) {
148 wire::get_compact_string(buf)?
149 } else {
150 wire::get_string(buf)?
151 };
152 this.session_timeout_ms = wire::get_i32(buf)?;
153 if version >= 1 {
154 this.rebalance_timeout_ms = wire::get_i32(buf)?;
155 }
156 this.member_id = if is_flexible(version) {
157 wire::get_compact_string(buf)?
158 } else {
159 wire::get_string(buf)?
160 };
161 if version >= 5 {
162 this.group_instance_id = if is_flexible(version) {
163 wire::get_compact_nullable_string(buf)?
164 } else {
165 wire::get_nullable_string(buf)?
166 };
167 }
168 this.protocol_type = if is_flexible(version) {
169 wire::get_compact_string(buf)?
170 } else {
171 wire::get_string(buf)?
172 };
173 this.protocols = {
174 let len = if is_flexible(version) {
175 wire::get_compact_array_len(buf)?
176 } else {
177 wire::get_array_len(buf)?
178 };
179 match len {
180 None => return Err(DecodeError::InvalidLength(-1)),
181 Some(n) => {
182 let mut items = Vec::new();
183 for _ in 0..n {
184 let mark = buf.remaining();
185 let item =
186 JoinGroupRequestProtocol::decode_with_budget(buf, version, budget)?;
187 budget.progress(mark, buf.remaining())?;
188 budget.push(&mut items, item)?;
189 }
190 items
191 }
192 }
193 };
194 if version >= 8 {
195 this.reason = if is_flexible(version) {
196 wire::get_compact_nullable_string(buf)?
197 } else {
198 wire::get_nullable_string(buf)?
199 };
200 }
201 if is_flexible(version) {
202 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
203 }
204 Ok(this)
205 }
206}
207
208impl crate::Message for JoinGroupRequest {
209 const API_KEY: i16 = 11;
210 const MIN_VERSION: i16 = 0;
211 const MAX_VERSION: i16 = 9;
212
213 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
214 JoinGroupRequest::encode(self, buf, version)
215 }
216 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
217 JoinGroupRequest::decode(buf, version)
218 }
219 fn decode_with_limits(
220 buf: &mut impl Buf,
221 version: i16,
222 limits: Limits,
223 ) -> Result<Self, DecodeError> {
224 JoinGroupRequest::decode_with_limits(buf, version, limits)
225 }
226}
227
228#[derive(Debug, Clone, PartialEq)]
230#[non_exhaustive]
231pub struct JoinGroupRequestProtocol {
232 pub name: String,
234 pub metadata: bytes::Bytes,
236 pub unknown_tagged_fields: Vec<RawTaggedField>,
238}
239
240impl Default for JoinGroupRequestProtocol {
241 fn default() -> Self {
242 Self {
243 name: String::new(),
244 metadata: bytes::Bytes::new(),
245 unknown_tagged_fields: Vec::new(),
246 }
247 }
248}
249
250impl JoinGroupRequestProtocol {
251 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
253 if is_flexible(version) {
254 wire::put_compact_string(buf, &self.name);
255 } else {
256 wire::put_string(buf, &self.name)?;
257 }
258 if is_flexible(version) {
259 wire::put_compact_bytes(buf, &self.metadata);
260 } else {
261 wire::put_bytes(buf, &self.metadata)?;
262 }
263 if is_flexible(version) {
264 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
265 }
266 Ok(())
267 }
268
269 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
272 Self::decode_with_limits(buf, version, Limits::default())
273 }
274
275 pub fn decode_with_limits(
277 buf: &mut impl Buf,
278 version: i16,
279 limits: Limits,
280 ) -> Result<Self, DecodeError> {
281 let mut budget = limits.budget(buf.remaining());
282 Self::decode_with_budget(buf, version, &mut budget)
283 }
284
285 pub fn decode_with_budget(
287 buf: &mut impl Buf,
288 version: i16,
289 budget: &mut Budget,
290 ) -> Result<Self, DecodeError> {
291 let mut this = Self::default();
292 this.name = if is_flexible(version) {
293 wire::get_compact_string(buf)?
294 } else {
295 wire::get_string(buf)?
296 };
297 this.metadata = {
298 let raw = if is_flexible(version) {
299 wire::get_compact_nullable_bytes(buf)?
300 } else {
301 wire::get_nullable_bytes(buf)?
302 };
303 match raw {
304 Some(v) => v,
305 None => return Err(DecodeError::InvalidLength(-1)),
306 }
307 };
308 if is_flexible(version) {
309 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
310 }
311 Ok(this)
312 }
313}