Skip to main content

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