Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
SyncGroupRequest.borrowed.rs

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