Skip to main content

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

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