Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
BeginQuorumEpochResponse.borrowed.rs

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