Skip to main content

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