Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::BufMut;
4
5use crate::primitives::fixed::{get_f64, get_i16, get_i32, put_f64, 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 = 48;
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, Default)]
28pub struct DescribeClientQuotasResponse<'a> {
29    pub throttle_time_ms: i32,
30    pub error_code: i16,
31    pub error_message: Option<&'a str>,
32    pub entries: Option<Vec<EntryData<'a>>>,
33    pub unknown_tagged_fields: UnknownTaggedFields,
34}
35impl DescribeClientQuotasResponse<'_> {
36    pub fn to_owned(
37        &self,
38    ) -> crate::owned::describe_client_quotas_response::DescribeClientQuotasResponse {
39        crate::owned::describe_client_quotas_response::DescribeClientQuotasResponse {
40            throttle_time_ms: (self.throttle_time_ms),
41            error_code: (self.error_code),
42            error_message: (self.error_message).map(std::string::ToString::to_string),
43            entries: (self.entries)
44                .as_ref()
45                .map(|v| v.iter().map(EntryData::to_owned).collect()),
46            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
47        }
48    }
49}
50impl Encode for DescribeClientQuotasResponse<'_> {
51    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
52        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
53            return Err(ProtocolError::UnsupportedVersion {
54                api_key: API_KEY,
55                version,
56            });
57        }
58        let flex = is_flexible(version);
59        if version >= 0 {
60            put_i32(buf, self.throttle_time_ms);
61        }
62        if version >= 0 {
63            put_i16(buf, self.error_code);
64        }
65        if version >= 0 {
66            if flex {
67                put_compact_nullable_string(buf, self.error_message);
68            } else {
69                put_nullable_string(buf, self.error_message);
70            }
71        }
72        if version >= 0 {
73            {
74                let len = (self.entries).as_ref().map(Vec::len);
75                crate::primitives::array::put_nullable_array_len(buf, len, flex);
76                if let Some(v) = &self.entries {
77                    for it in v {
78                        it.encode(buf, version)?;
79                    }
80                }
81            }
82        }
83        if flex {
84            let tagged = WriteTaggedFields::new();
85            tagged.write(buf, &self.unknown_tagged_fields);
86        }
87        Ok(())
88    }
89    fn encoded_len(&self, version: i16) -> usize {
90        let flex = is_flexible(version);
91        let mut n: usize = 0;
92        if version >= 0 {
93            n += 4;
94        }
95        if version >= 0 {
96            n += 2;
97        }
98        if version >= 0 {
99            n += if flex {
100                compact_nullable_string_len(self.error_message)
101            } else {
102                nullable_string_len(self.error_message)
103            };
104        }
105        if version >= 0 {
106            n += {
107                let opt: Option<&Vec<_>> = (self.entries).as_ref();
108                let prefix = crate::primitives::array::nullable_array_len_prefix_len(
109                    opt.map(std::vec::Vec::len),
110                    flex,
111                );
112                let body: usize =
113                    opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
114                prefix + body
115            };
116        }
117        if flex {
118            let known_pairs: Vec<(u32, usize)> = Vec::new();
119            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
120        }
121        n
122    }
123}
124impl<'de> DecodeBorrow<'de> for DescribeClientQuotasResponse<'de> {
125    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
126        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
127            return Err(ProtocolError::UnsupportedVersion {
128                api_key: API_KEY,
129                version,
130            });
131        }
132        let flex = is_flexible(version);
133        let mut out = Self::default();
134        if version >= 0 {
135            out.throttle_time_ms = get_i32(buf)?;
136        }
137        if version >= 0 {
138            out.error_code = get_i16(buf)?;
139        }
140        if version >= 0 {
141            out.error_message = if flex {
142                get_compact_nullable_string_borrowed(buf)?
143            } else {
144                get_nullable_string_borrowed(buf)?
145            };
146        }
147        if version >= 0 {
148            out.entries = {
149                let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
150                match opt {
151                    None => None,
152                    Some(n) => {
153                        let mut v = Vec::with_capacity(n);
154                        for _ in 0..n {
155                            v.push(EntryData::decode_borrow(buf, version)?);
156                        }
157                        Some(v)
158                    }
159                }
160            };
161        }
162        if flex {
163            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
164        }
165        Ok(out)
166    }
167}
168#[cfg(test)]
169impl DescribeClientQuotasResponse<'_> {
170    #[must_use]
171    pub fn populated(version: i16) -> Self {
172        let mut m = Self::default();
173        if version >= 0 {
174            m.throttle_time_ms = 1i32;
175        }
176        if version >= 0 {
177            m.error_code = 1i16;
178        }
179        if version >= 0 {
180            m.error_message = Some("x");
181        }
182        if version >= 0 {
183            m.entries = Some(vec![EntryData::populated(version)]);
184        }
185        m
186    }
187}
188#[derive(Debug, Clone, PartialEq, Default)]
189pub struct EntryData<'a> {
190    pub entity: Vec<EntityData<'a>>,
191    pub values: Vec<ValueData<'a>>,
192    pub unknown_tagged_fields: UnknownTaggedFields,
193}
194impl EntryData<'_> {
195    pub fn to_owned(&self) -> crate::owned::describe_client_quotas_response::EntryData {
196        crate::owned::describe_client_quotas_response::EntryData {
197            entity: (self.entity).iter().map(EntityData::to_owned).collect(),
198            values: (self.values).iter().map(ValueData::to_owned).collect(),
199            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
200        }
201    }
202}
203impl Encode for EntryData<'_> {
204    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
205        let flex = version >= 1;
206        if version >= 0 {
207            {
208                crate::primitives::array::put_array_len(buf, (self.entity).len(), flex);
209                for it in &self.entity {
210                    it.encode(buf, version)?;
211                }
212            }
213        }
214        if version >= 0 {
215            {
216                crate::primitives::array::put_array_len(buf, (self.values).len(), flex);
217                for it in &self.values {
218                    it.encode(buf, version)?;
219                }
220            }
221        }
222        if flex {
223            let tagged = WriteTaggedFields::new();
224            tagged.write(buf, &self.unknown_tagged_fields);
225        }
226        Ok(())
227    }
228    fn encoded_len(&self, version: i16) -> usize {
229        let flex = version >= 1;
230        let mut n: usize = 0;
231        if version >= 0 {
232            n += {
233                let prefix =
234                    crate::primitives::array::array_len_prefix_len((self.entity).len(), flex);
235                let body: usize = (self.entity).iter().map(|it| it.encoded_len(version)).sum();
236                prefix + body
237            };
238        }
239        if version >= 0 {
240            n += {
241                let prefix =
242                    crate::primitives::array::array_len_prefix_len((self.values).len(), flex);
243                let body: usize = (self.values).iter().map(|it| it.encoded_len(version)).sum();
244                prefix + body
245            };
246        }
247        if flex {
248            let known_pairs: Vec<(u32, usize)> = Vec::new();
249            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
250        }
251        n
252    }
253}
254impl<'de> DecodeBorrow<'de> for EntryData<'de> {
255    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
256        let flex = version >= 1;
257        let mut out = Self::default();
258        if version >= 0 {
259            out.entity = {
260                let n = crate::primitives::array::get_array_len(buf, flex)?;
261                let mut v = Vec::with_capacity(n);
262                for _ in 0..n {
263                    v.push(EntityData::decode_borrow(buf, version)?);
264                }
265                v
266            };
267        }
268        if version >= 0 {
269            out.values = {
270                let n = crate::primitives::array::get_array_len(buf, flex)?;
271                let mut v = Vec::with_capacity(n);
272                for _ in 0..n {
273                    v.push(ValueData::decode_borrow(buf, version)?);
274                }
275                v
276            };
277        }
278        if flex {
279            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
280        }
281        Ok(out)
282    }
283}
284#[cfg(test)]
285impl EntryData<'_> {
286    #[must_use]
287    pub fn populated(version: i16) -> Self {
288        let mut m = Self::default();
289        if version >= 0 {
290            m.entity = vec![EntityData::populated(version)];
291        }
292        if version >= 0 {
293            m.values = vec![ValueData::populated(version)];
294        }
295        m
296    }
297}
298#[derive(Debug, Clone, PartialEq, Eq, Default)]
299pub struct EntityData<'a> {
300    pub entity_type: &'a str,
301    pub entity_name: Option<&'a str>,
302    pub unknown_tagged_fields: UnknownTaggedFields,
303}
304impl EntityData<'_> {
305    pub fn to_owned(&self) -> crate::owned::describe_client_quotas_response::EntityData {
306        crate::owned::describe_client_quotas_response::EntityData {
307            entity_type: (self.entity_type).to_string(),
308            entity_name: (self.entity_name).map(std::string::ToString::to_string),
309            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
310        }
311    }
312}
313impl Encode for EntityData<'_> {
314    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
315        let flex = version >= 1;
316        if version >= 0 {
317            if flex {
318                put_compact_string(buf, self.entity_type);
319            } else {
320                put_string(buf, self.entity_type);
321            }
322        }
323        if version >= 0 {
324            if flex {
325                put_compact_nullable_string(buf, self.entity_name);
326            } else {
327                put_nullable_string(buf, self.entity_name);
328            }
329        }
330        if flex {
331            let tagged = WriteTaggedFields::new();
332            tagged.write(buf, &self.unknown_tagged_fields);
333        }
334        Ok(())
335    }
336    fn encoded_len(&self, version: i16) -> usize {
337        let flex = version >= 1;
338        let mut n: usize = 0;
339        if version >= 0 {
340            n += if flex {
341                compact_string_len(self.entity_type)
342            } else {
343                string_len(self.entity_type)
344            };
345        }
346        if version >= 0 {
347            n += if flex {
348                compact_nullable_string_len(self.entity_name)
349            } else {
350                nullable_string_len(self.entity_name)
351            };
352        }
353        if flex {
354            let known_pairs: Vec<(u32, usize)> = Vec::new();
355            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
356        }
357        n
358    }
359}
360impl<'de> DecodeBorrow<'de> for EntityData<'de> {
361    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
362        let flex = version >= 1;
363        let mut out = Self::default();
364        if version >= 0 {
365            out.entity_type = if flex {
366                get_compact_string_borrowed(buf)?
367            } else {
368                get_string_borrowed(buf)?
369            };
370        }
371        if version >= 0 {
372            out.entity_name = if flex {
373                get_compact_nullable_string_borrowed(buf)?
374            } else {
375                get_nullable_string_borrowed(buf)?
376            };
377        }
378        if flex {
379            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
380        }
381        Ok(out)
382    }
383}
384#[cfg(test)]
385impl EntityData<'_> {
386    #[must_use]
387    pub fn populated(version: i16) -> Self {
388        let mut m = Self::default();
389        if version >= 0 {
390            m.entity_type = "x";
391        }
392        if version >= 0 {
393            m.entity_name = Some("x");
394        }
395        m
396    }
397}
398#[derive(Debug, Clone, PartialEq)]
399pub struct ValueData<'a> {
400    pub key: &'a str,
401    pub value: f64,
402    pub unknown_tagged_fields: UnknownTaggedFields,
403}
404impl Default for ValueData<'_> {
405    fn default() -> Self {
406        Self {
407            key: "",
408            value: 0.0f64,
409            unknown_tagged_fields: Default::default(),
410        }
411    }
412}
413impl ValueData<'_> {
414    pub fn to_owned(&self) -> crate::owned::describe_client_quotas_response::ValueData {
415        crate::owned::describe_client_quotas_response::ValueData {
416            key: (self.key).to_string(),
417            value: (self.value),
418            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
419        }
420    }
421}
422impl Encode for ValueData<'_> {
423    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
424        let flex = version >= 1;
425        if version >= 0 {
426            if flex {
427                put_compact_string(buf, self.key);
428            } else {
429                put_string(buf, self.key);
430            }
431        }
432        if version >= 0 {
433            put_f64(buf, self.value);
434        }
435        if flex {
436            let tagged = WriteTaggedFields::new();
437            tagged.write(buf, &self.unknown_tagged_fields);
438        }
439        Ok(())
440    }
441    fn encoded_len(&self, version: i16) -> usize {
442        let flex = version >= 1;
443        let mut n: usize = 0;
444        if version >= 0 {
445            n += if flex {
446                compact_string_len(self.key)
447            } else {
448                string_len(self.key)
449            };
450        }
451        if version >= 0 {
452            n += 8;
453        }
454        if flex {
455            let known_pairs: Vec<(u32, usize)> = Vec::new();
456            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
457        }
458        n
459    }
460}
461impl<'de> DecodeBorrow<'de> for ValueData<'de> {
462    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
463        let flex = version >= 1;
464        let mut out = Self::default();
465        if version >= 0 {
466            out.key = if flex {
467                get_compact_string_borrowed(buf)?
468            } else {
469                get_string_borrowed(buf)?
470            };
471        }
472        if version >= 0 {
473            out.value = get_f64(buf)?;
474        }
475        if flex {
476            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
477        }
478        Ok(out)
479    }
480}
481#[cfg(test)]
482impl ValueData<'_> {
483    #[must_use]
484    pub fn populated(version: i16) -> Self {
485        let mut m = Self::default();
486        if version >= 0 {
487            m.key = "x";
488        }
489        if version >= 0 {
490            m.value = 1.0f64;
491        }
492        m
493    }
494}