Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
VoteResponse.borrowed.rs

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