Skip to main content

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