Skip to main content

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

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