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