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