Skip to main content

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

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