Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{
6    get_bool, get_i16, get_i32, get_u16, put_bool, put_i16, put_i32, put_u16,
7};
8use crate::primitives::string_bytes::{
9    compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
10    string_len,
11};
12use crate::tagged_fields::{
13    WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len,
14};
15use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
16
17pub const API_KEY: i16 = 52;
18pub const MIN_VERSION: i16 = 0;
19pub const MAX_VERSION: i16 = 2;
20pub const FLEXIBLE_MIN: i16 = 0;
21
22#[inline]
23fn is_flexible(version: i16) -> bool {
24    version >= FLEXIBLE_MIN
25}
26
27#[derive(Debug, Clone, PartialEq, Eq, Default)]
28pub struct VoteResponse {
29    pub error_code: i16,
30    pub topics: Vec<TopicData>,
31    pub node_endpoints: Vec<NodeEndpoint>,
32    pub unknown_tagged_fields: UnknownTaggedFields,
33}
34impl Encode for VoteResponse {
35    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
36        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
37            return Err(ProtocolError::UnsupportedVersion {
38                api_key: API_KEY,
39                version,
40            });
41        }
42        let flex = is_flexible(version);
43        if version >= 0 {
44            put_i16(buf, self.error_code);
45        }
46        if version >= 0 {
47            {
48                crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
49                for it in &self.topics {
50                    it.encode(buf, version)?;
51                }
52            }
53        }
54        if flex {
55            let mut tagged = WriteTaggedFields::new();
56            if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
57                let payload = encode_to_bytes(
58                    {
59                        let prefix = crate::primitives::array::array_len_prefix_len(
60                            (self.node_endpoints).len(),
61                            flex,
62                        );
63                        let body: usize = (self.node_endpoints)
64                            .iter()
65                            .map(|it| it.encoded_len(version))
66                            .sum();
67                        prefix + body
68                    },
69                    |b| {
70                        {
71                            crate::primitives::array::put_array_len(
72                                b,
73                                (self.node_endpoints).len(),
74                                flex,
75                            );
76                            for it in &self.node_endpoints {
77                                it.encode(b, version)?;
78                            }
79                        };
80                        Ok(())
81                    },
82                );
83                tagged.add(0, payload);
84            }
85            tagged.write(buf, &self.unknown_tagged_fields);
86        }
87        Ok(())
88    }
89    fn encoded_len(&self, version: i16) -> usize {
90        let flex = is_flexible(version);
91        let mut n: usize = 0;
92        if version >= 0 {
93            n += 2;
94        }
95        if version >= 0 {
96            n += {
97                let prefix =
98                    crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
99                let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
100                prefix + body
101            };
102        }
103        if flex {
104            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
105            if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
106                known_pairs.push((0, {
107                    let prefix = crate::primitives::array::array_len_prefix_len(
108                        (self.node_endpoints).len(),
109                        flex,
110                    );
111                    let body: usize = (self.node_endpoints)
112                        .iter()
113                        .map(|it| it.encoded_len(version))
114                        .sum();
115                    prefix + body
116                }));
117            }
118            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
119        }
120        n
121    }
122}
123impl Decode<'_> for VoteResponse {
124    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
125        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
126            return Err(ProtocolError::UnsupportedVersion {
127                api_key: API_KEY,
128                version,
129            });
130        }
131        let flex = is_flexible(version);
132        let mut out = Self::default();
133        if version >= 0 {
134            out.error_code = get_i16(buf)?;
135        }
136        if version >= 0 {
137            out.topics = {
138                let n = crate::primitives::array::get_array_len(buf, flex)?;
139                let mut v = Vec::with_capacity(n);
140                for _ in 0..n {
141                    v.push(TopicData::decode(buf, version)?);
142                }
143                v
144            };
145        }
146        if flex {
147            let mut tag_node_endpoints = None;
148            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
149                0 => {
150                    tag_node_endpoints = Some({
151                        let b: &mut &[u8] = payload;
152                        {
153                            let n = crate::primitives::array::get_array_len(b, flex)?;
154                            let mut v = Vec::with_capacity(n);
155                            for _ in 0..n {
156                                v.push(NodeEndpoint::decode(b, version)?);
157                            }
158                            v
159                        }
160                    });
161                    Ok(true)
162                }
163                _ => Ok(false),
164            })?;
165            if let Some(v) = tag_node_endpoints {
166                out.node_endpoints = v;
167            }
168        }
169        Ok(out)
170    }
171}
172#[cfg(test)]
173impl VoteResponse {
174    #[must_use]
175    pub fn populated(version: i16) -> Self {
176        let mut m = Self::default();
177        if version >= 0 {
178            m.error_code = 1i16;
179        }
180        if version >= 0 {
181            m.topics = vec![TopicData::populated(version)];
182        }
183        if version >= 1 {
184            m.node_endpoints = vec![NodeEndpoint::populated(version)];
185        }
186        m
187    }
188}
189#[derive(Debug, Clone, PartialEq, Eq, Default)]
190pub struct TopicData {
191    pub topic_name: String,
192    pub partitions: Vec<PartitionData>,
193    pub unknown_tagged_fields: UnknownTaggedFields,
194}
195impl Encode for TopicData {
196    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
197        let flex = version >= 0;
198        if version >= 0 {
199            if flex {
200                put_compact_string(buf, &self.topic_name);
201            } else {
202                put_string(buf, &self.topic_name);
203            }
204        }
205        if version >= 0 {
206            {
207                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
208                for it in &self.partitions {
209                    it.encode(buf, version)?;
210                }
211            }
212        }
213        if flex {
214            let tagged = WriteTaggedFields::new();
215            tagged.write(buf, &self.unknown_tagged_fields);
216        }
217        Ok(())
218    }
219    fn encoded_len(&self, version: i16) -> usize {
220        let flex = version >= 0;
221        let mut n: usize = 0;
222        if version >= 0 {
223            n += if flex {
224                compact_string_len(&self.topic_name)
225            } else {
226                string_len(&self.topic_name)
227            };
228        }
229        if version >= 0 {
230            n += {
231                let prefix =
232                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
233                let body: usize = (self.partitions)
234                    .iter()
235                    .map(|it| it.encoded_len(version))
236                    .sum();
237                prefix + body
238            };
239        }
240        if flex {
241            let known_pairs: Vec<(u32, usize)> = Vec::new();
242            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
243        }
244        n
245    }
246}
247impl Decode<'_> for TopicData {
248    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
249        let flex = version >= 0;
250        let mut out = Self::default();
251        if version >= 0 {
252            out.topic_name = if flex {
253                get_compact_string_owned(buf)?
254            } else {
255                get_string_owned(buf)?
256            };
257        }
258        if version >= 0 {
259            out.partitions = {
260                let n = crate::primitives::array::get_array_len(buf, flex)?;
261                let mut v = Vec::with_capacity(n);
262                for _ in 0..n {
263                    v.push(PartitionData::decode(buf, version)?);
264                }
265                v
266            };
267        }
268        if flex {
269            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
270        }
271        Ok(out)
272    }
273}
274#[cfg(test)]
275impl TopicData {
276    #[must_use]
277    pub fn populated(version: i16) -> Self {
278        let mut m = Self::default();
279        if version >= 0 {
280            m.topic_name = "x".to_string();
281        }
282        if version >= 0 {
283            m.partitions = vec![PartitionData::populated(version)];
284        }
285        m
286    }
287}
288#[derive(Debug, Clone, PartialEq, Eq, Default)]
289pub struct PartitionData {
290    pub partition_index: i32,
291    pub error_code: i16,
292    pub leader_id: i32,
293    pub leader_epoch: i32,
294    pub vote_granted: bool,
295    pub unknown_tagged_fields: UnknownTaggedFields,
296}
297impl Encode for PartitionData {
298    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
299        let flex = version >= 0;
300        if version >= 0 {
301            put_i32(buf, self.partition_index);
302        }
303        if version >= 0 {
304            put_i16(buf, self.error_code);
305        }
306        if version >= 0 {
307            put_i32(buf, self.leader_id);
308        }
309        if version >= 0 {
310            put_i32(buf, self.leader_epoch);
311        }
312        if version >= 0 {
313            put_bool(buf, self.vote_granted);
314        }
315        if flex {
316            let tagged = WriteTaggedFields::new();
317            tagged.write(buf, &self.unknown_tagged_fields);
318        }
319        Ok(())
320    }
321    fn encoded_len(&self, version: i16) -> usize {
322        let flex = version >= 0;
323        let mut n: usize = 0;
324        if version >= 0 {
325            n += 4;
326        }
327        if version >= 0 {
328            n += 2;
329        }
330        if version >= 0 {
331            n += 4;
332        }
333        if version >= 0 {
334            n += 4;
335        }
336        if version >= 0 {
337            n += 1;
338        }
339        if flex {
340            let known_pairs: Vec<(u32, usize)> = Vec::new();
341            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
342        }
343        n
344    }
345}
346impl Decode<'_> for PartitionData {
347    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
348        let flex = version >= 0;
349        let mut out = Self::default();
350        if version >= 0 {
351            out.partition_index = get_i32(buf)?;
352        }
353        if version >= 0 {
354            out.error_code = get_i16(buf)?;
355        }
356        if version >= 0 {
357            out.leader_id = get_i32(buf)?;
358        }
359        if version >= 0 {
360            out.leader_epoch = get_i32(buf)?;
361        }
362        if version >= 0 {
363            out.vote_granted = get_bool(buf)?;
364        }
365        if flex {
366            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
367        }
368        Ok(out)
369    }
370}
371#[cfg(test)]
372impl PartitionData {
373    #[must_use]
374    pub fn populated(version: i16) -> Self {
375        let mut m = Self::default();
376        if version >= 0 {
377            m.partition_index = 1i32;
378        }
379        if version >= 0 {
380            m.error_code = 1i16;
381        }
382        if version >= 0 {
383            m.leader_id = 1i32;
384        }
385        if version >= 0 {
386            m.leader_epoch = 1i32;
387        }
388        if version >= 0 {
389            m.vote_granted = true;
390        }
391        m
392    }
393}
394#[derive(Debug, Clone, PartialEq, Eq, Default)]
395pub struct NodeEndpoint {
396    pub node_id: i32,
397    pub host: String,
398    pub port: u16,
399    pub unknown_tagged_fields: UnknownTaggedFields,
400}
401impl Encode for NodeEndpoint {
402    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
403        let flex = version >= 0;
404        if version >= 1 {
405            put_i32(buf, self.node_id);
406        }
407        if version >= 1 {
408            if flex {
409                put_compact_string(buf, &self.host);
410            } else {
411                put_string(buf, &self.host);
412            }
413        }
414        if version >= 1 {
415            put_u16(buf, self.port);
416        }
417        if flex {
418            let tagged = WriteTaggedFields::new();
419            tagged.write(buf, &self.unknown_tagged_fields);
420        }
421        Ok(())
422    }
423    fn encoded_len(&self, version: i16) -> usize {
424        let flex = version >= 0;
425        let mut n: usize = 0;
426        if version >= 1 {
427            n += 4;
428        }
429        if version >= 1 {
430            n += if flex {
431                compact_string_len(&self.host)
432            } else {
433                string_len(&self.host)
434            };
435        }
436        if version >= 1 {
437            n += 2;
438        }
439        if flex {
440            let known_pairs: Vec<(u32, usize)> = Vec::new();
441            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
442        }
443        n
444    }
445}
446impl Decode<'_> for NodeEndpoint {
447    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
448        let flex = version >= 0;
449        let mut out = Self::default();
450        if version >= 1 {
451            out.node_id = get_i32(buf)?;
452        }
453        if version >= 1 {
454            out.host = if flex {
455                get_compact_string_owned(buf)?
456            } else {
457                get_string_owned(buf)?
458            };
459        }
460        if version >= 1 {
461            out.port = get_u16(buf)?;
462        }
463        if flex {
464            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
465        }
466        Ok(out)
467    }
468}
469#[cfg(test)]
470impl NodeEndpoint {
471    #[must_use]
472    pub fn populated(version: i16) -> Self {
473        let mut m = Self::default();
474        if version >= 1 {
475            m.node_id = 1i32;
476        }
477        if version >= 1 {
478            m.host = "x".to_string();
479        }
480        if version >= 1 {
481            m.port = 1u16;
482        }
483        m
484    }
485}
486
487/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
488/// Only includes fields valid for the given version.
489#[must_use]
490#[allow(unused_comparisons)]
491pub fn default_json(version: i16) -> ::serde_json::Value {
492    let mut obj = ::serde_json::Map::new();
493    obj.insert("errorCode".to_string(), ::serde_json::json!(0));
494    obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
495    if version >= 1 {
496        obj.insert(
497            "nodeEndpoints".to_string(),
498            ::serde_json::Value::Array(vec![]),
499        );
500    }
501    ::serde_json::Value::Object(obj)
502}