Skip to main content

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