Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
JoinGroupResponse.borrowed.rs

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