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