Skip to main content

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

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