Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
3use crate::primitives::string_bytes::{
4    compact_string_len, put_compact_string, put_string, string_len,
5};
6use crate::primitives::string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed};
7use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
8use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
9use bytes::BufMut;
10pub const API_KEY: i16 = 16;
11pub const MIN_VERSION: i16 = 0;
12pub const MAX_VERSION: i16 = 5;
13pub const FLEXIBLE_MIN: i16 = 3;
14#[inline]
15fn is_flexible(version: i16) -> bool {
16    version >= FLEXIBLE_MIN
17}
18#[derive(Debug, Clone, PartialEq, Eq, Default)]
19pub struct ListGroupsResponse<'a> {
20    pub throttle_time_ms: i32,
21    pub error_code: i16,
22    pub groups: Vec<ListedGroup<'a>>,
23    pub unknown_tagged_fields: UnknownTaggedFields,
24}
25impl ListGroupsResponse<'_> {
26    pub fn to_owned(&self) -> crate::owned::list_groups_response::ListGroupsResponse {
27        crate::owned::list_groups_response::ListGroupsResponse {
28            throttle_time_ms: (self.throttle_time_ms),
29            error_code: (self.error_code),
30            groups: (self.groups).iter().map(ListedGroup::to_owned).collect(),
31            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
32        }
33    }
34}
35impl Encode for ListGroupsResponse<'_> {
36    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
37        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
38            return Err(ProtocolError::UnsupportedVersion {
39                api_key: API_KEY,
40                version,
41            });
42        }
43        let flex = is_flexible(version);
44        if version >= 1 {
45            put_i32(buf, self.throttle_time_ms);
46        }
47        if version >= 0 {
48            put_i16(buf, self.error_code);
49        }
50        if version >= 0 {
51            {
52                crate::primitives::array::put_array_len(buf, (self.groups).len(), flex);
53                for it in &self.groups {
54                    it.encode(buf, version)?;
55                }
56            }
57        }
58        if flex {
59            let tagged = WriteTaggedFields::new();
60            tagged.write(buf, &self.unknown_tagged_fields);
61        }
62        Ok(())
63    }
64    fn encoded_len(&self, version: i16) -> usize {
65        let flex = is_flexible(version);
66        let mut n: usize = 0;
67        if version >= 1 {
68            n += 4;
69        }
70        if version >= 0 {
71            n += 2;
72        }
73        if version >= 0 {
74            n += {
75                let prefix =
76                    crate::primitives::array::array_len_prefix_len((self.groups).len(), flex);
77                let body: usize = (self.groups).iter().map(|it| it.encoded_len(version)).sum();
78                prefix + body
79            };
80        }
81        if flex {
82            let known_pairs: Vec<(u32, usize)> = Vec::new();
83            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
84        }
85        n
86    }
87}
88impl<'de> DecodeBorrow<'de> for ListGroupsResponse<'de> {
89    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
90        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
91            return Err(ProtocolError::UnsupportedVersion {
92                api_key: API_KEY,
93                version,
94            });
95        }
96        let flex = is_flexible(version);
97        let mut out = Self::default();
98        if version >= 1 {
99            out.throttle_time_ms = get_i32(buf)?;
100        }
101        if version >= 0 {
102            out.error_code = get_i16(buf)?;
103        }
104        if version >= 0 {
105            out.groups = {
106                let n = crate::primitives::array::get_array_len(buf, flex)?;
107                let mut v = Vec::with_capacity(n);
108                for _ in 0..n {
109                    v.push(ListedGroup::decode_borrow(buf, version)?);
110                }
111                v
112            };
113        }
114        if flex {
115            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
116        }
117        Ok(out)
118    }
119}
120#[cfg(test)]
121impl ListGroupsResponse<'_> {
122    #[must_use]
123    pub fn populated(version: i16) -> Self {
124        let mut m = Self::default();
125        if version >= 1 {
126            m.throttle_time_ms = 1i32;
127        }
128        if version >= 0 {
129            m.error_code = 1i16;
130        }
131        if version >= 0 {
132            m.groups = vec![ListedGroup::populated(version)];
133        }
134        m
135    }
136}
137#[derive(Debug, Clone, PartialEq, Eq, Default)]
138pub struct ListedGroup<'a> {
139    pub group_id: &'a str,
140    pub protocol_type: &'a str,
141    pub group_state: &'a str,
142    pub group_type: &'a str,
143    pub unknown_tagged_fields: UnknownTaggedFields,
144}
145impl ListedGroup<'_> {
146    pub fn to_owned(&self) -> crate::owned::list_groups_response::ListedGroup {
147        crate::owned::list_groups_response::ListedGroup {
148            group_id: (self.group_id).to_string(),
149            protocol_type: (self.protocol_type).to_string(),
150            group_state: (self.group_state).to_string(),
151            group_type: (self.group_type).to_string(),
152            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
153        }
154    }
155}
156impl Encode for ListedGroup<'_> {
157    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
158        let flex = version >= 3;
159        if version >= 0 {
160            if flex {
161                put_compact_string(buf, self.group_id);
162            } else {
163                put_string(buf, self.group_id);
164            }
165        }
166        if version >= 0 {
167            if flex {
168                put_compact_string(buf, self.protocol_type);
169            } else {
170                put_string(buf, self.protocol_type);
171            }
172        }
173        if version >= 4 {
174            if flex {
175                put_compact_string(buf, self.group_state);
176            } else {
177                put_string(buf, self.group_state);
178            }
179        }
180        if version >= 5 {
181            if flex {
182                put_compact_string(buf, self.group_type);
183            } else {
184                put_string(buf, self.group_type);
185            }
186        }
187        if flex {
188            let tagged = WriteTaggedFields::new();
189            tagged.write(buf, &self.unknown_tagged_fields);
190        }
191        Ok(())
192    }
193    fn encoded_len(&self, version: i16) -> usize {
194        let flex = version >= 3;
195        let mut n: usize = 0;
196        if version >= 0 {
197            n += if flex {
198                compact_string_len(self.group_id)
199            } else {
200                string_len(self.group_id)
201            };
202        }
203        if version >= 0 {
204            n += if flex {
205                compact_string_len(self.protocol_type)
206            } else {
207                string_len(self.protocol_type)
208            };
209        }
210        if version >= 4 {
211            n += if flex {
212                compact_string_len(self.group_state)
213            } else {
214                string_len(self.group_state)
215            };
216        }
217        if version >= 5 {
218            n += if flex {
219                compact_string_len(self.group_type)
220            } else {
221                string_len(self.group_type)
222            };
223        }
224        if flex {
225            let known_pairs: Vec<(u32, usize)> = Vec::new();
226            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
227        }
228        n
229    }
230}
231impl<'de> DecodeBorrow<'de> for ListedGroup<'de> {
232    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
233        let flex = version >= 3;
234        let mut out = Self::default();
235        if version >= 0 {
236            out.group_id = if flex {
237                get_compact_string_borrowed(buf)?
238            } else {
239                get_string_borrowed(buf)?
240            };
241        }
242        if version >= 0 {
243            out.protocol_type = if flex {
244                get_compact_string_borrowed(buf)?
245            } else {
246                get_string_borrowed(buf)?
247            };
248        }
249        if version >= 4 {
250            out.group_state = if flex {
251                get_compact_string_borrowed(buf)?
252            } else {
253                get_string_borrowed(buf)?
254            };
255        }
256        if version >= 5 {
257            out.group_type = if flex {
258                get_compact_string_borrowed(buf)?
259            } else {
260                get_string_borrowed(buf)?
261            };
262        }
263        if flex {
264            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
265        }
266        Ok(out)
267    }
268}
269#[cfg(test)]
270impl ListedGroup<'_> {
271    #[must_use]
272    pub fn populated(version: i16) -> Self {
273        let mut m = Self::default();
274        if version >= 0 {
275            m.group_id = "x";
276        }
277        if version >= 0 {
278            m.protocol_type = "x";
279        }
280        if version >= 4 {
281            m.group_state = "x";
282        }
283        if version >= 5 {
284            m.group_type = "x";
285        }
286        m
287    }
288}