Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
BeginQuorumEpochResponse.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_i16, get_i32, get_u16, 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 = 53;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 1;
16pub const FLEXIBLE_MIN: i16 = 1;
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 BeginQuorumEpochResponse<'a> {
24    pub error_code: i16,
25    pub topics: Vec<TopicData<'a>>,
26    pub node_endpoints: Vec<crate::owned::begin_quorum_epoch_response::NodeEndpoint>,
27    pub unknown_tagged_fields: UnknownTaggedFields,
28}
29impl BeginQuorumEpochResponse<'_> {
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::begin_quorum_epoch_response::BeginQuorumEpochResponse {
35        crate::owned::begin_quorum_epoch_response::BeginQuorumEpochResponse {
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 BeginQuorumEpochResponse<'_> {
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 !(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 !(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 BeginQuorumEpochResponse<'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 => {
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::begin_quorum_epoch_response::NodeEndpoint::decode(b, version)?);
166                            }
167                            v
168                        }
169                    });
170                    Ok(true)
171                }
172                _ => Ok(false),
173            })?;
174            if let Some(v) = tag_node_endpoints {
175                out.node_endpoints = v;
176            }
177        }
178        Ok(out)
179    }
180}
181#[cfg(test)]
182impl BeginQuorumEpochResponse<'_> {
183    #[must_use]
184    pub fn populated(version: i16) -> Self {
185        let mut m = Self::default();
186        if version >= 0 {
187            m.error_code = 1i16;
188        }
189        if version >= 0 {
190            m.topics = vec![TopicData::populated(version)];
191        }
192        if version >= 1 {
193            m.node_endpoints =
194                vec![crate::owned::begin_quorum_epoch_response::NodeEndpoint::populated(version)];
195        }
196        m
197    }
198}
199#[derive(Debug, Clone, PartialEq, Eq, Default)]
200pub struct TopicData<'a> {
201    pub topic_name: &'a str,
202    pub partitions: Vec<PartitionData>,
203    pub unknown_tagged_fields: UnknownTaggedFields,
204}
205impl TopicData<'_> {
206    /// # Panics
207    ///
208    /// Panics if a records field contains an invalid encoded record batch.
209    #[must_use]
210    pub fn to_owned(&self) -> crate::owned::begin_quorum_epoch_response::TopicData {
211        crate::owned::begin_quorum_epoch_response::TopicData {
212            topic_name: (self.topic_name).to_string(),
213            partitions: (self.partitions)
214                .iter()
215                .map(PartitionData::to_owned)
216                .collect(),
217            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
218        }
219    }
220}
221impl Encode for TopicData<'_> {
222    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
223        let flex = version >= 1;
224        if version >= 0 {
225            if flex {
226                let () = put_compact_string(buf, self.topic_name);
227            } else {
228                let () = put_string(buf, self.topic_name);
229            }
230        }
231        if version >= 0 {
232            {
233                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
234                for it in &self.partitions {
235                    it.encode(buf, version)?;
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 >= 1;
247        let mut n: usize = 0;
248        if version >= 0 {
249            n += if flex {
250                compact_string_len(self.topic_name)
251            } else {
252                string_len(self.topic_name)
253            };
254        }
255        if version >= 0 {
256            n += {
257                let prefix =
258                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
259                let body: usize = (self.partitions)
260                    .iter()
261                    .map(|it| it.encoded_len(version))
262                    .sum();
263                prefix + body
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<'de> DecodeBorrow<'de> for TopicData<'de> {
274    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
275        let flex = version >= 1;
276        let mut out = Self::default();
277        if version >= 0 {
278            out.topic_name = if flex {
279                get_compact_string_borrowed(buf)?
280            } else {
281                get_string_borrowed(buf)?
282            };
283        }
284        if version >= 0 {
285            out.partitions = {
286                let n = crate::primitives::array::get_array_len(buf, flex)?;
287                let mut v = Vec::with_capacity(n);
288                for _ in 0..n {
289                    v.push(PartitionData::decode_borrow(buf, version)?);
290                }
291                v
292            };
293        }
294        if flex {
295            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
296        }
297        Ok(out)
298    }
299}
300#[cfg(test)]
301impl TopicData<'_> {
302    #[must_use]
303    pub fn populated(version: i16) -> Self {
304        let mut m = Self::default();
305        if version >= 0 {
306            m.topic_name = "x";
307        }
308        if version >= 0 {
309            m.partitions = vec![PartitionData::populated(version)];
310        }
311        m
312    }
313}
314#[derive(Debug, Clone, PartialEq, Eq, Default)]
315pub struct PartitionData {
316    pub partition_index: i32,
317    pub error_code: i16,
318    pub leader_id: i32,
319    pub leader_epoch: i32,
320    pub unknown_tagged_fields: UnknownTaggedFields,
321}
322impl PartitionData {
323    /// # Panics
324    ///
325    /// Panics if a records field contains an invalid encoded record batch.
326    #[must_use]
327    pub fn to_owned(&self) -> crate::owned::begin_quorum_epoch_response::PartitionData {
328        crate::owned::begin_quorum_epoch_response::PartitionData {
329            partition_index: (self.partition_index),
330            error_code: (self.error_code),
331            leader_id: (self.leader_id),
332            leader_epoch: (self.leader_epoch),
333            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
334        }
335    }
336}
337impl Encode for PartitionData {
338    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
339        let flex = version >= 1;
340        if version >= 0 {
341            put_i32(buf, self.partition_index);
342        }
343        if version >= 0 {
344            put_i16(buf, self.error_code);
345        }
346        if version >= 0 {
347            put_i32(buf, self.leader_id);
348        }
349        if version >= 0 {
350            put_i32(buf, self.leader_epoch);
351        }
352        if flex {
353            let tagged = WriteTaggedFields::new();
354            tagged.write(buf, &self.unknown_tagged_fields);
355        }
356        Ok(())
357    }
358    fn encoded_len(&self, version: i16) -> usize {
359        let flex = version >= 1;
360        let mut n: usize = 0;
361        if version >= 0 {
362            n += 4;
363        }
364        if version >= 0 {
365            n += 2;
366        }
367        if version >= 0 {
368            n += 4;
369        }
370        if version >= 0 {
371            n += 4;
372        }
373        if flex {
374            let known_pairs: Vec<(u32, usize)> = Vec::new();
375            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
376        }
377        n
378    }
379}
380impl<'de> DecodeBorrow<'de> for PartitionData {
381    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
382        let flex = version >= 1;
383        let mut out = Self::default();
384        if version >= 0 {
385            out.partition_index = get_i32(buf)?;
386        }
387        if version >= 0 {
388            out.error_code = get_i16(buf)?;
389        }
390        if version >= 0 {
391            out.leader_id = get_i32(buf)?;
392        }
393        if version >= 0 {
394            out.leader_epoch = get_i32(buf)?;
395        }
396        if flex {
397            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
398        }
399        Ok(out)
400    }
401}
402#[cfg(test)]
403impl PartitionData {
404    #[must_use]
405    pub fn populated(version: i16) -> Self {
406        let mut m = Self::default();
407        if version >= 0 {
408            m.partition_index = 1i32;
409        }
410        if version >= 0 {
411            m.error_code = 1i16;
412        }
413        if version >= 0 {
414            m.leader_id = 1i32;
415        }
416        if version >= 0 {
417            m.leader_epoch = 1i32;
418        }
419        m
420    }
421}
422#[derive(Debug, Clone, PartialEq, Eq, Default)]
423pub struct NodeEndpoint<'a> {
424    pub node_id: i32,
425    pub host: &'a str,
426    pub port: u16,
427    pub unknown_tagged_fields: UnknownTaggedFields,
428}
429impl NodeEndpoint<'_> {
430    /// # Panics
431    ///
432    /// Panics if a records field contains an invalid encoded record batch.
433    #[must_use]
434    pub fn to_owned(&self) -> crate::owned::begin_quorum_epoch_response::NodeEndpoint {
435        crate::owned::begin_quorum_epoch_response::NodeEndpoint {
436            node_id: (self.node_id),
437            host: (self.host).to_string(),
438            port: (self.port),
439            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
440        }
441    }
442}
443impl Encode for NodeEndpoint<'_> {
444    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
445        let flex = version >= 1;
446        if version >= 1 {
447            put_i32(buf, self.node_id);
448        }
449        if version >= 1 {
450            if flex {
451                let () = put_compact_string(buf, self.host);
452            } else {
453                let () = put_string(buf, self.host);
454            }
455        }
456        if version >= 1 {
457            put_u16(buf, self.port);
458        }
459        if flex {
460            let tagged = WriteTaggedFields::new();
461            tagged.write(buf, &self.unknown_tagged_fields);
462        }
463        Ok(())
464    }
465    fn encoded_len(&self, version: i16) -> usize {
466        let flex = version >= 1;
467        let mut n: usize = 0;
468        if version >= 1 {
469            n += 4;
470        }
471        if version >= 1 {
472            n += if flex {
473                compact_string_len(self.host)
474            } else {
475                string_len(self.host)
476            };
477        }
478        if version >= 1 {
479            n += 2;
480        }
481        if flex {
482            let known_pairs: Vec<(u32, usize)> = Vec::new();
483            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
484        }
485        n
486    }
487}
488impl<'de> DecodeBorrow<'de> for NodeEndpoint<'de> {
489    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
490        let flex = version >= 1;
491        let mut out = Self::default();
492        if version >= 1 {
493            out.node_id = get_i32(buf)?;
494        }
495        if version >= 1 {
496            out.host = if flex {
497                get_compact_string_borrowed(buf)?
498            } else {
499                get_string_borrowed(buf)?
500            };
501        }
502        if version >= 1 {
503            out.port = get_u16(buf)?;
504        }
505        if flex {
506            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
507        }
508        Ok(out)
509    }
510}
511#[cfg(test)]
512impl NodeEndpoint<'_> {
513    #[must_use]
514    pub fn populated(version: i16) -> Self {
515        let mut m = Self::default();
516        if version >= 1 {
517            m.node_id = 1i32;
518        }
519        if version >= 1 {
520            m.host = "x";
521        }
522        if version >= 1 {
523            m.port = 1u16;
524        }
525        m
526    }
527}