Skip to main content

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