Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
MetadataRequest.owned.rs

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