Skip to main content

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