Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
FetchResponse.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_i64, put_i16, put_i32, put_i64},
8        string_bytes::{
9            compact_nullable_string_len, compact_string_len, nullable_string_len, put_bytes,
10            put_compact_bytes, put_compact_nullable_bytes, put_compact_nullable_string,
11            put_compact_string, put_nullable_bytes, put_nullable_string, put_string, string_len,
12        },
13        string_bytes_borrowed::{
14            get_compact_nullable_bytes_borrowed, get_compact_nullable_string_borrowed,
15            get_compact_string_borrowed, get_nullable_bytes_borrowed, get_nullable_string_borrowed,
16            get_string_borrowed,
17        },
18    },
19    tagged_fields::{WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len},
20};
21pub const API_KEY: i16 = 1;
22pub const MIN_VERSION: i16 = 4;
23pub const MAX_VERSION: i16 = 18;
24pub const FLEXIBLE_MIN: i16 = 12;
25#[inline]
26#[must_use]
27pub fn is_flexible(version: i16) -> bool {
28    version >= FLEXIBLE_MIN
29}
30#[derive(Debug, Clone, PartialEq, Eq, Default)]
31pub struct FetchResponse<'a> {
32    pub throttle_time_ms: i32,
33    pub error_code: i16,
34    pub session_id: i32,
35    pub responses: Vec<FetchableTopicResponse<'a>>,
36    pub node_endpoints: Vec<crate::owned::fetch_response::NodeEndpoint>,
37    pub unknown_tagged_fields: UnknownTaggedFields,
38}
39impl FetchResponse<'_> {
40    /// # Panics
41    ///
42    /// Panics if a records field contains an invalid encoded record batch.
43    #[must_use]
44    pub fn to_owned(&self) -> crate::owned::fetch_response::FetchResponse {
45        crate::owned::fetch_response::FetchResponse {
46            throttle_time_ms: (self.throttle_time_ms),
47            error_code: (self.error_code),
48            session_id: (self.session_id),
49            responses: (self.responses)
50                .iter()
51                .map(FetchableTopicResponse::to_owned)
52                .collect(),
53            node_endpoints: self.node_endpoints.clone(),
54            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
55        }
56    }
57}
58impl Encode for FetchResponse<'_> {
59    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
60        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
61            return Err(ProtocolError::UnsupportedVersion {
62                api_key: API_KEY,
63                version,
64            });
65        }
66        let flex = is_flexible(version);
67        if version >= 1 {
68            put_i32(buf, self.throttle_time_ms);
69        }
70        if version >= 7 {
71            put_i16(buf, self.error_code);
72        }
73        if version >= 7 {
74            put_i32(buf, self.session_id);
75        }
76        if version >= 0 {
77            {
78                crate::primitives::array::put_array_len(buf, (self.responses).len(), flex);
79                for it in &self.responses {
80                    it.encode(buf, version)?;
81                }
82            };
83        }
84        if flex {
85            let mut tagged = WriteTaggedFields::new();
86            if version >= 16 && !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
87                let payload = encode_to_bytes(
88                    {
89                        let prefix = crate::primitives::array::array_len_prefix_len(
90                            (self.node_endpoints).len(),
91                            flex,
92                        );
93                        let body: usize = (self.node_endpoints)
94                            .iter()
95                            .map(|it| it.encoded_len(version))
96                            .sum();
97                        prefix + body
98                    },
99                    |b| {
100                        {
101                            crate::primitives::array::put_array_len(
102                                b,
103                                (self.node_endpoints).len(),
104                                flex,
105                            );
106                            for it in &self.node_endpoints {
107                                it.encode(b, version)?;
108                            }
109                        };
110                        Ok(())
111                    },
112                );
113                tagged.add(0, payload);
114            }
115            tagged.write(buf, &self.unknown_tagged_fields);
116        }
117        Ok(())
118    }
119    fn encoded_len(&self, version: i16) -> usize {
120        let flex = is_flexible(version);
121        let mut n: usize = 0;
122        if version >= 1 {
123            n += 4;
124        }
125        if version >= 7 {
126            n += 2;
127        }
128        if version >= 7 {
129            n += 4;
130        }
131        if version >= 0 {
132            n += {
133                let prefix =
134                    crate::primitives::array::array_len_prefix_len((self.responses).len(), flex);
135                let body: usize = (self.responses)
136                    .iter()
137                    .map(|it| it.encoded_len(version))
138                    .sum();
139                prefix + body
140            };
141        }
142        if flex {
143            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
144            if version >= 16 && !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
145                known_pairs.push((0, {
146                    let prefix = crate::primitives::array::array_len_prefix_len(
147                        (self.node_endpoints).len(),
148                        flex,
149                    );
150                    let body: usize = (self.node_endpoints)
151                        .iter()
152                        .map(|it| it.encoded_len(version))
153                        .sum();
154                    prefix + body
155                }));
156            }
157            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
158        }
159        n
160    }
161}
162impl<'de> DecodeBorrow<'de> for FetchResponse<'de> {
163    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
164        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
165            return Err(ProtocolError::UnsupportedVersion {
166                api_key: API_KEY,
167                version,
168            });
169        }
170        let flex = is_flexible(version);
171        let mut out = Self::default();
172        if version >= 1 {
173            out.throttle_time_ms = get_i32(buf)?;
174        }
175        if version >= 7 {
176            out.error_code = get_i16(buf)?;
177        }
178        if version >= 7 {
179            out.session_id = get_i32(buf)?;
180        }
181        if version >= 0 {
182            out.responses = {
183                let n = crate::primitives::array::get_array_len(buf, flex)?;
184                let mut v = Vec::with_capacity(n);
185                for _ in 0..n {
186                    v.push(FetchableTopicResponse::decode_borrow(buf, version)?);
187                }
188                v
189            };
190        }
191        if flex {
192            let mut tag_node_endpoints = None;
193            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
194                0 if version >= 16 => {
195                    tag_node_endpoints = Some({
196                        let b: &mut &[u8] = payload;
197                        {
198                            let n = crate::primitives::array::get_array_len(b, flex)?;
199                            let mut v = Vec::with_capacity(n);
200                            for _ in 0..n {
201                                v.push(crate::owned::fetch_response::NodeEndpoint::decode(
202                                    b, version,
203                                )?);
204                            }
205                            v
206                        }
207                    });
208                    Ok(true)
209                }
210                0 => Err(ProtocolError::TagNotValidForVersion { tag: 0, version }),
211                _ => Ok(false),
212            })?;
213            if let Some(v) = tag_node_endpoints {
214                out.node_endpoints = v;
215            }
216        }
217        Ok(out)
218    }
219}
220#[cfg(test)]
221impl FetchResponse<'_> {
222    #[must_use]
223    pub fn populated(version: i16) -> Self {
224        let mut m = Self::default();
225        if version >= 1 {
226            m.throttle_time_ms = 1i32;
227        }
228        if version >= 7 {
229            m.error_code = 1i16;
230        }
231        if version >= 7 {
232            m.session_id = 1i32;
233        }
234        if version >= 0 {
235            m.responses = vec![FetchableTopicResponse::populated(version)];
236        }
237        if version >= 16 {
238            m.node_endpoints = vec![crate::owned::fetch_response::NodeEndpoint::populated(
239                version,
240            )];
241        }
242        m
243    }
244}
245#[derive(Debug, Clone, PartialEq, Eq, Default)]
246pub struct FetchableTopicResponse<'a> {
247    pub topic: &'a str,
248    pub topic_id: crate::primitives::uuid::Uuid,
249    pub partitions: Vec<PartitionData<'a>>,
250    pub unknown_tagged_fields: UnknownTaggedFields,
251}
252impl FetchableTopicResponse<'_> {
253    /// # Panics
254    ///
255    /// Panics if a records field contains an invalid encoded record batch.
256    #[must_use]
257    pub fn to_owned(&self) -> crate::owned::fetch_response::FetchableTopicResponse {
258        crate::owned::fetch_response::FetchableTopicResponse {
259            topic: (self.topic).to_string(),
260            topic_id: (self.topic_id),
261            partitions: (self.partitions)
262                .iter()
263                .map(PartitionData::to_owned)
264                .collect(),
265            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
266        }
267    }
268}
269impl Encode for FetchableTopicResponse<'_> {
270    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
271        let flex = version >= 12;
272        if (0..=12).contains(&version) {
273            if flex {
274                let () = put_compact_string(buf, self.topic);
275            } else {
276                let () = put_string(buf, self.topic);
277            }
278        }
279        if version >= 13 {
280            crate::primitives::uuid::put_uuid(buf, self.topic_id);
281        }
282        if version >= 0 {
283            {
284                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
285                for it in &self.partitions {
286                    it.encode(buf, version)?;
287                }
288            };
289        }
290        if flex {
291            let tagged = WriteTaggedFields::new();
292            tagged.write(buf, &self.unknown_tagged_fields);
293        }
294        Ok(())
295    }
296    fn encoded_len(&self, version: i16) -> usize {
297        let flex = version >= 12;
298        let mut n: usize = 0;
299        if (0..=12).contains(&version) {
300            n += if flex {
301                compact_string_len(self.topic)
302            } else {
303                string_len(self.topic)
304            };
305        }
306        if version >= 13 {
307            n += 16;
308        }
309        if version >= 0 {
310            n += {
311                let prefix =
312                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
313                let body: usize = (self.partitions)
314                    .iter()
315                    .map(|it| it.encoded_len(version))
316                    .sum();
317                prefix + body
318            };
319        }
320        if flex {
321            let known_pairs: Vec<(u32, usize)> = Vec::new();
322            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
323        }
324        n
325    }
326}
327impl<'de> DecodeBorrow<'de> for FetchableTopicResponse<'de> {
328    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
329        let flex = version >= 12;
330        let mut out = Self::default();
331        if (0..=12).contains(&version) {
332            out.topic = if flex {
333                get_compact_string_borrowed(buf)?
334            } else {
335                get_string_borrowed(buf)?
336            };
337        }
338        if version >= 13 {
339            out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
340        }
341        if version >= 0 {
342            out.partitions = {
343                let n = crate::primitives::array::get_array_len(buf, flex)?;
344                let mut v = Vec::with_capacity(n);
345                for _ in 0..n {
346                    v.push(PartitionData::decode_borrow(buf, version)?);
347                }
348                v
349            };
350        }
351        if flex {
352            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
353        }
354        Ok(out)
355    }
356}
357#[cfg(test)]
358impl FetchableTopicResponse<'_> {
359    #[must_use]
360    pub fn populated(version: i16) -> Self {
361        let mut m = Self::default();
362        if (0..=12).contains(&version) {
363            m.topic = "x";
364        }
365        if version >= 13 {
366            m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
367        }
368        if version >= 0 {
369            m.partitions = vec![PartitionData::populated(version)];
370        }
371        m
372    }
373}
374#[derive(Debug, Clone, PartialEq, Eq)]
375pub struct PartitionData<'a> {
376    pub partition_index: i32,
377    pub error_code: i16,
378    pub high_watermark: i64,
379    pub last_stable_offset: i64,
380    pub log_start_offset: i64,
381    pub aborted_transactions: Option<Vec<AbortedTransaction>>,
382    pub preferred_read_replica: i32,
383    pub records: Option<crate::records::RecordsPayloadBorrowed<'a>>,
384    pub diverging_epoch: EpochEndOffset,
385    pub current_leader: LeaderIdAndEpoch,
386    pub snapshot_id: SnapshotId,
387    pub unknown_tagged_fields: UnknownTaggedFields,
388}
389impl Default for PartitionData<'_> {
390    fn default() -> Self {
391        Self {
392            partition_index: 0i32,
393            error_code: 0i16,
394            high_watermark: 0i64,
395            last_stable_offset: -1i64,
396            log_start_offset: -1i64,
397            aborted_transactions: Some(Vec::new()),
398            preferred_read_replica: -1i32,
399            records: None,
400            diverging_epoch: <EpochEndOffset>::default(),
401            current_leader: <LeaderIdAndEpoch>::default(),
402            snapshot_id: <SnapshotId>::default(),
403            unknown_tagged_fields: UnknownTaggedFields::default(),
404        }
405    }
406}
407impl<'a> PartitionData<'a> {
408    /// # Panics
409    ///
410    /// Panics if a records field contains an invalid encoded record batch.
411    #[must_use]
412    pub fn to_owned(&self) -> crate::owned::fetch_response::PartitionData {
413        crate::owned::fetch_response::PartitionData {
414            partition_index: (self.partition_index),
415            error_code: (self.error_code),
416            high_watermark: (self.high_watermark),
417            last_stable_offset: (self.last_stable_offset),
418            log_start_offset: (self.log_start_offset),
419            aborted_transactions: (self.aborted_transactions)
420                .as_ref()
421                .map(|v| v.iter().map(AbortedTransaction::to_owned).collect()),
422            preferred_read_replica: (self.preferred_read_replica),
423            records: (self.records)
424                .as_ref()
425                .map(|rb| rb.to_owned().expect("records to_owned")),
426            diverging_epoch: (self.diverging_epoch).to_owned(),
427            current_leader: (self.current_leader).to_owned(),
428            snapshot_id: (self.snapshot_id).to_owned(),
429            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
430        }
431    }
432    fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
433        if version >= 0 {
434            put_i32(buf, self.partition_index);
435        }
436    }
437    fn encode_field_1<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
438        if version >= 0 {
439            put_i16(buf, self.error_code);
440        }
441    }
442    fn encode_field_2<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
443        if version >= 0 {
444            put_i64(buf, self.high_watermark);
445        }
446    }
447    fn encode_field_3<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
448        if version >= 4 {
449            put_i64(buf, self.last_stable_offset);
450        }
451    }
452    fn encode_field_4<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
453        if version >= 5 {
454            put_i64(buf, self.log_start_offset);
455        }
456    }
457    fn encode_field_5<B: BufMut>(
458        &self,
459        buf: &mut B,
460        version: i16,
461        flex: bool,
462    ) -> Result<(), ProtocolError> {
463        if version >= 4 {
464            {
465                let len = (self.aborted_transactions).as_ref().map(Vec::len);
466                crate::primitives::array::put_nullable_array_len(buf, len, flex);
467                if let Some(v) = &self.aborted_transactions {
468                    for it in v {
469                        it.encode(buf, version)?;
470                    }
471                }
472            };
473        }
474        Ok(())
475    }
476    fn encode_field_6<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
477        if version >= 11 {
478            put_i32(buf, self.preferred_read_replica);
479        }
480    }
481    fn encode_field_7<B: BufMut>(
482        &self,
483        buf: &mut B,
484        version: i16,
485        flex: bool,
486    ) -> Result<(), ProtocolError> {
487        if version >= 0 {
488            match &self.records {
489                None => {
490                    if flex {
491                        let () = put_compact_nullable_bytes(buf, None);
492                    } else {
493                        let () = put_nullable_bytes(buf, None);
494                    }
495                }
496                Some(__rb) => {
497                    let mut __rb_buf = bytes::BytesMut::new();
498                    <crate::records::RecordsPayloadBorrowed as crate::Encode>::encode(
499                        __rb,
500                        &mut __rb_buf,
501                        version,
502                    )?;
503                    if flex {
504                        let () = put_compact_bytes(buf, &__rb_buf);
505                    } else {
506                        let () = put_bytes(buf, &__rb_buf);
507                    }
508                }
509            }
510        }
511        Ok(())
512    }
513    fn encode_tagged_fields<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
514        if flex {
515            let mut tagged = WriteTaggedFields::new();
516            if !(crate::codegen_helpers::is_default(&self.diverging_epoch)) {
517                let payload = encode_to_bytes(self.diverging_epoch.encoded_len(version), |b| {
518                    self.diverging_epoch.encode(b, version)?;
519                    Ok(())
520                });
521                tagged.add(0, payload);
522            }
523            if !(crate::codegen_helpers::is_default(&self.current_leader)) {
524                let payload = encode_to_bytes(self.current_leader.encoded_len(version), |b| {
525                    self.current_leader.encode(b, version)?;
526                    Ok(())
527                });
528                tagged.add(1, payload);
529            }
530            if !(crate::codegen_helpers::is_default(&self.snapshot_id)) {
531                let payload = encode_to_bytes(self.snapshot_id.encoded_len(version), |b| {
532                    self.snapshot_id.encode(b, version)?;
533                    Ok(())
534                });
535                tagged.add(2, payload);
536            }
537            tagged.write(buf, &self.unknown_tagged_fields);
538        }
539    }
540    fn decode_field_0(
541        out: &mut Self,
542        buf: &mut &'a [u8],
543        version: i16,
544        _flex: bool,
545    ) -> Result<(), ProtocolError> {
546        if version >= 0 {
547            out.partition_index = get_i32(buf)?;
548        }
549        Ok(())
550    }
551    fn decode_field_1(
552        out: &mut Self,
553        buf: &mut &'a [u8],
554        version: i16,
555        _flex: bool,
556    ) -> Result<(), ProtocolError> {
557        if version >= 0 {
558            out.error_code = get_i16(buf)?;
559        }
560        Ok(())
561    }
562    fn decode_field_2(
563        out: &mut Self,
564        buf: &mut &'a [u8],
565        version: i16,
566        _flex: bool,
567    ) -> Result<(), ProtocolError> {
568        if version >= 0 {
569            out.high_watermark = get_i64(buf)?;
570        }
571        Ok(())
572    }
573    fn decode_field_3(
574        out: &mut Self,
575        buf: &mut &'a [u8],
576        version: i16,
577        _flex: bool,
578    ) -> Result<(), ProtocolError> {
579        if version >= 4 {
580            out.last_stable_offset = get_i64(buf)?;
581        }
582        Ok(())
583    }
584    fn decode_field_4(
585        out: &mut Self,
586        buf: &mut &'a [u8],
587        version: i16,
588        _flex: bool,
589    ) -> Result<(), ProtocolError> {
590        if version >= 5 {
591            out.log_start_offset = get_i64(buf)?;
592        }
593        Ok(())
594    }
595    fn decode_field_5(
596        out: &mut Self,
597        buf: &mut &'a [u8],
598        version: i16,
599        flex: bool,
600    ) -> Result<(), ProtocolError> {
601        if version >= 4 {
602            out.aborted_transactions = {
603                let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
604                match opt {
605                    None => None,
606                    Some(n) => {
607                        let mut v = Vec::with_capacity(n);
608                        for _ in 0..n {
609                            v.push(AbortedTransaction::decode_borrow(buf, version)?);
610                        }
611                        Some(v)
612                    }
613                }
614            };
615        }
616        Ok(())
617    }
618    fn decode_field_6(
619        out: &mut Self,
620        buf: &mut &'a [u8],
621        version: i16,
622        _flex: bool,
623    ) -> Result<(), ProtocolError> {
624        if version >= 11 {
625            out.preferred_read_replica = get_i32(buf)?;
626        }
627        Ok(())
628    }
629    fn decode_field_7(
630        out: &mut Self,
631        buf: &mut &'a [u8],
632        version: i16,
633        flex: bool,
634    ) -> Result<(), ProtocolError> {
635        if version >= 0 {
636            out.records = {
637                let __rb_opt = if flex {
638                    get_compact_nullable_bytes_borrowed(buf)?
639                } else {
640                    get_nullable_bytes_borrowed(buf)?
641                };
642                match __rb_opt {
643                    None => None,
644                    Some(__rb_slice) => {
645                        let mut __rb_cur = __rb_slice;
646                        Some(<crate::records::RecordsPayloadBorrowed as crate::DecodeBorrow>::decode_borrow(&mut __rb_cur, version)?)
647                    }
648                }
649            };
650        }
651        Ok(())
652    }
653    fn decode_tagged_fields(
654        out: &mut Self,
655        buf: &mut &'a [u8],
656        version: i16,
657        flex: bool,
658    ) -> Result<(), ProtocolError> {
659        if flex {
660            let mut tag_diverging_epoch = None;
661            let mut tag_current_leader = None;
662            let mut tag_snapshot_id = None;
663            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
664                0 => {
665                    tag_diverging_epoch = Some({
666                        let b: &mut &[u8] = payload;
667                        EpochEndOffset::decode_borrow(b, version)?
668                    });
669                    Ok(true)
670                }
671                1 => {
672                    tag_current_leader = Some({
673                        let b: &mut &[u8] = payload;
674                        LeaderIdAndEpoch::decode_borrow(b, version)?
675                    });
676                    Ok(true)
677                }
678                2 => {
679                    tag_snapshot_id = Some({
680                        let b: &mut &[u8] = payload;
681                        SnapshotId::decode_borrow(b, version)?
682                    });
683                    Ok(true)
684                }
685                _ => Ok(false),
686            })?;
687            if let Some(v) = tag_diverging_epoch {
688                out.diverging_epoch = v;
689            }
690            if let Some(v) = tag_current_leader {
691                out.current_leader = v;
692            }
693            if let Some(v) = tag_snapshot_id {
694                out.snapshot_id = v;
695            }
696        }
697        Ok(())
698    }
699}
700impl Encode for PartitionData<'_> {
701    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
702        let flex = version >= 12;
703        self.encode_field_0(buf, version, flex);
704        self.encode_field_1(buf, version, flex);
705        self.encode_field_2(buf, version, flex);
706        self.encode_field_3(buf, version, flex);
707        self.encode_field_4(buf, version, flex);
708        self.encode_field_5(buf, version, flex)?;
709        self.encode_field_6(buf, version, flex);
710        self.encode_field_7(buf, version, flex)?;
711        self.encode_tagged_fields(buf, version, flex);
712        Ok(())
713    }
714    fn encoded_len(&self, version: i16) -> usize {
715        let flex = version >= 12;
716        let mut n: usize = 0;
717        if version >= 0 {
718            n += 4;
719        }
720        if version >= 0 {
721            n += 2;
722        }
723        if version >= 0 {
724            n += 8;
725        }
726        if version >= 4 {
727            n += 8;
728        }
729        if version >= 5 {
730            n += 8;
731        }
732        if version >= 4 {
733            n += {
734                let opt: Option<&Vec<_>> = (self.aborted_transactions).as_ref();
735                let prefix = crate::primitives::array::nullable_array_len_prefix_len(
736                    opt.map(std::vec::Vec::len),
737                    flex,
738                );
739                let body: usize =
740                    opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
741                prefix + body
742            };
743        }
744        if version >= 11 {
745            n += 4;
746        }
747        if version >= 0 {
748            n += match &self.records {
749                None => {
750                    if flex {
751                        crate::primitives::varint::uvarint_len(0)
752                    } else {
753                        4
754                    }
755                }
756                Some(__rb) => {
757                    let __rb_len =
758                        <crate::records::RecordsPayloadBorrowed as crate::Encode>::encoded_len(
759                            __rb, version,
760                        );
761                    if flex {
762                        crate::primitives::string_bytes::compact_bytes_len_from_size(__rb_len)
763                    } else {
764                        4 + __rb_len
765                    }
766                }
767            };
768        }
769        if flex {
770            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
771            if !(crate::codegen_helpers::is_default(&self.diverging_epoch)) {
772                known_pairs.push((0, self.diverging_epoch.encoded_len(version)));
773            }
774            if !(crate::codegen_helpers::is_default(&self.current_leader)) {
775                known_pairs.push((1, self.current_leader.encoded_len(version)));
776            }
777            if !(crate::codegen_helpers::is_default(&self.snapshot_id)) {
778                known_pairs.push((2, self.snapshot_id.encoded_len(version)));
779            }
780            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
781        }
782        n
783    }
784}
785impl<'de> DecodeBorrow<'de> for PartitionData<'de> {
786    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
787        let flex = version >= 12;
788        let mut out = Self::default();
789        Self::decode_field_0(&mut out, buf, version, flex)?;
790        Self::decode_field_1(&mut out, buf, version, flex)?;
791        Self::decode_field_2(&mut out, buf, version, flex)?;
792        Self::decode_field_3(&mut out, buf, version, flex)?;
793        Self::decode_field_4(&mut out, buf, version, flex)?;
794        Self::decode_field_5(&mut out, buf, version, flex)?;
795        Self::decode_field_6(&mut out, buf, version, flex)?;
796        Self::decode_field_7(&mut out, buf, version, flex)?;
797        Self::decode_tagged_fields(&mut out, buf, version, flex)?;
798        Ok(out)
799    }
800}
801#[cfg(test)]
802impl PartitionData<'_> {
803    #[must_use]
804    pub fn populated(version: i16) -> Self {
805        let mut m = Self::default();
806        if version >= 0 {
807            m.partition_index = 1i32;
808        }
809        if version >= 0 {
810            m.error_code = 1i16;
811        }
812        if version >= 0 {
813            m.high_watermark = 1i64;
814        }
815        if version >= 4 {
816            m.last_stable_offset = 1i64;
817        }
818        if version >= 5 {
819            m.log_start_offset = 1i64;
820        }
821        if version >= 4 {
822            m.aborted_transactions = Some(vec![AbortedTransaction::populated(version)]);
823        }
824        if version >= 11 {
825            m.preferred_read_replica = 1i32;
826        }
827        if version >= 12 {
828            m.diverging_epoch = EpochEndOffset::populated(version);
829        }
830        if version >= 12 {
831            m.current_leader = LeaderIdAndEpoch::populated(version);
832        }
833        if version >= 12 {
834            m.snapshot_id = SnapshotId::populated(version);
835        }
836        m
837    }
838}
839#[derive(Debug, Clone, PartialEq, Eq)]
840pub struct EpochEndOffset {
841    pub epoch: i32,
842    pub end_offset: i64,
843    pub unknown_tagged_fields: UnknownTaggedFields,
844}
845impl Default for EpochEndOffset {
846    fn default() -> Self {
847        Self {
848            epoch: -1i32,
849            end_offset: -1i64,
850            unknown_tagged_fields: UnknownTaggedFields::default(),
851        }
852    }
853}
854impl EpochEndOffset {
855    /// # Panics
856    ///
857    /// Panics if a records field contains an invalid encoded record batch.
858    #[must_use]
859    pub fn to_owned(&self) -> crate::owned::fetch_response::EpochEndOffset {
860        crate::owned::fetch_response::EpochEndOffset {
861            epoch: (self.epoch),
862            end_offset: (self.end_offset),
863            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
864        }
865    }
866}
867impl Encode for EpochEndOffset {
868    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
869        let flex = version >= 12;
870        if version >= 12 {
871            put_i32(buf, self.epoch);
872        }
873        if version >= 12 {
874            put_i64(buf, self.end_offset);
875        }
876        if flex {
877            let tagged = WriteTaggedFields::new();
878            tagged.write(buf, &self.unknown_tagged_fields);
879        }
880        Ok(())
881    }
882    fn encoded_len(&self, version: i16) -> usize {
883        let flex = version >= 12;
884        let mut n: usize = 0;
885        if version >= 12 {
886            n += 4;
887        }
888        if version >= 12 {
889            n += 8;
890        }
891        if flex {
892            let known_pairs: Vec<(u32, usize)> = Vec::new();
893            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
894        }
895        n
896    }
897}
898impl<'de> DecodeBorrow<'de> for EpochEndOffset {
899    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
900        let flex = version >= 12;
901        let mut out = Self::default();
902        if version >= 12 {
903            out.epoch = get_i32(buf)?;
904        }
905        if version >= 12 {
906            out.end_offset = get_i64(buf)?;
907        }
908        if flex {
909            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
910        }
911        Ok(out)
912    }
913}
914#[cfg(test)]
915impl EpochEndOffset {
916    #[must_use]
917    pub fn populated(version: i16) -> Self {
918        let mut m = Self::default();
919        if version >= 12 {
920            m.epoch = 1i32;
921        }
922        if version >= 12 {
923            m.end_offset = 1i64;
924        }
925        m
926    }
927}
928#[derive(Debug, Clone, PartialEq, Eq)]
929pub struct LeaderIdAndEpoch {
930    pub leader_id: i32,
931    pub leader_epoch: i32,
932    pub unknown_tagged_fields: UnknownTaggedFields,
933}
934impl Default for LeaderIdAndEpoch {
935    fn default() -> Self {
936        Self {
937            leader_id: -1i32,
938            leader_epoch: -1i32,
939            unknown_tagged_fields: UnknownTaggedFields::default(),
940        }
941    }
942}
943impl LeaderIdAndEpoch {
944    /// # Panics
945    ///
946    /// Panics if a records field contains an invalid encoded record batch.
947    #[must_use]
948    pub fn to_owned(&self) -> crate::owned::fetch_response::LeaderIdAndEpoch {
949        crate::owned::fetch_response::LeaderIdAndEpoch {
950            leader_id: (self.leader_id),
951            leader_epoch: (self.leader_epoch),
952            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
953        }
954    }
955}
956impl Encode for LeaderIdAndEpoch {
957    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
958        let flex = version >= 12;
959        if version >= 12 {
960            put_i32(buf, self.leader_id);
961        }
962        if version >= 12 {
963            put_i32(buf, self.leader_epoch);
964        }
965        if flex {
966            let tagged = WriteTaggedFields::new();
967            tagged.write(buf, &self.unknown_tagged_fields);
968        }
969        Ok(())
970    }
971    fn encoded_len(&self, version: i16) -> usize {
972        let flex = version >= 12;
973        let mut n: usize = 0;
974        if version >= 12 {
975            n += 4;
976        }
977        if version >= 12 {
978            n += 4;
979        }
980        if flex {
981            let known_pairs: Vec<(u32, usize)> = Vec::new();
982            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
983        }
984        n
985    }
986}
987impl<'de> DecodeBorrow<'de> for LeaderIdAndEpoch {
988    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
989        let flex = version >= 12;
990        let mut out = Self::default();
991        if version >= 12 {
992            out.leader_id = get_i32(buf)?;
993        }
994        if version >= 12 {
995            out.leader_epoch = get_i32(buf)?;
996        }
997        if flex {
998            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
999        }
1000        Ok(out)
1001    }
1002}
1003#[cfg(test)]
1004impl LeaderIdAndEpoch {
1005    #[must_use]
1006    pub fn populated(version: i16) -> Self {
1007        let mut m = Self::default();
1008        if version >= 12 {
1009            m.leader_id = 1i32;
1010        }
1011        if version >= 12 {
1012            m.leader_epoch = 1i32;
1013        }
1014        m
1015    }
1016}
1017#[derive(Debug, Clone, PartialEq, Eq)]
1018pub struct SnapshotId {
1019    pub end_offset: i64,
1020    pub epoch: i32,
1021    pub unknown_tagged_fields: UnknownTaggedFields,
1022}
1023impl Default for SnapshotId {
1024    fn default() -> Self {
1025        Self {
1026            end_offset: -1i64,
1027            epoch: -1i32,
1028            unknown_tagged_fields: UnknownTaggedFields::default(),
1029        }
1030    }
1031}
1032impl SnapshotId {
1033    /// # Panics
1034    ///
1035    /// Panics if a records field contains an invalid encoded record batch.
1036    #[must_use]
1037    pub fn to_owned(&self) -> crate::owned::fetch_response::SnapshotId {
1038        crate::owned::fetch_response::SnapshotId {
1039            end_offset: (self.end_offset),
1040            epoch: (self.epoch),
1041            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
1042        }
1043    }
1044}
1045impl Encode for SnapshotId {
1046    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
1047        let flex = version >= 12;
1048        if version >= 0 {
1049            put_i64(buf, self.end_offset);
1050        }
1051        if version >= 0 {
1052            put_i32(buf, self.epoch);
1053        }
1054        if flex {
1055            let tagged = WriteTaggedFields::new();
1056            tagged.write(buf, &self.unknown_tagged_fields);
1057        }
1058        Ok(())
1059    }
1060    fn encoded_len(&self, version: i16) -> usize {
1061        let flex = version >= 12;
1062        let mut n: usize = 0;
1063        if version >= 0 {
1064            n += 8;
1065        }
1066        if version >= 0 {
1067            n += 4;
1068        }
1069        if flex {
1070            let known_pairs: Vec<(u32, usize)> = Vec::new();
1071            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
1072        }
1073        n
1074    }
1075}
1076impl<'de> DecodeBorrow<'de> for SnapshotId {
1077    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
1078        let flex = version >= 12;
1079        let mut out = Self::default();
1080        if version >= 0 {
1081            out.end_offset = get_i64(buf)?;
1082        }
1083        if version >= 0 {
1084            out.epoch = get_i32(buf)?;
1085        }
1086        if flex {
1087            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
1088        }
1089        Ok(out)
1090    }
1091}
1092#[cfg(test)]
1093impl SnapshotId {
1094    #[must_use]
1095    pub fn populated(version: i16) -> Self {
1096        let mut m = Self::default();
1097        if version >= 0 {
1098            m.end_offset = 1i64;
1099        }
1100        if version >= 0 {
1101            m.epoch = 1i32;
1102        }
1103        m
1104    }
1105}
1106#[derive(Debug, Clone, PartialEq, Eq, Default)]
1107pub struct AbortedTransaction {
1108    pub producer_id: i64,
1109    pub first_offset: i64,
1110    pub unknown_tagged_fields: UnknownTaggedFields,
1111}
1112impl AbortedTransaction {
1113    /// # Panics
1114    ///
1115    /// Panics if a records field contains an invalid encoded record batch.
1116    #[must_use]
1117    pub fn to_owned(&self) -> crate::owned::fetch_response::AbortedTransaction {
1118        crate::owned::fetch_response::AbortedTransaction {
1119            producer_id: (self.producer_id),
1120            first_offset: (self.first_offset),
1121            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
1122        }
1123    }
1124}
1125impl Encode for AbortedTransaction {
1126    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
1127        let flex = version >= 12;
1128        if version >= 4 {
1129            put_i64(buf, self.producer_id);
1130        }
1131        if version >= 4 {
1132            put_i64(buf, self.first_offset);
1133        }
1134        if flex {
1135            let tagged = WriteTaggedFields::new();
1136            tagged.write(buf, &self.unknown_tagged_fields);
1137        }
1138        Ok(())
1139    }
1140    fn encoded_len(&self, version: i16) -> usize {
1141        let flex = version >= 12;
1142        let mut n: usize = 0;
1143        if version >= 4 {
1144            n += 8;
1145        }
1146        if version >= 4 {
1147            n += 8;
1148        }
1149        if flex {
1150            let known_pairs: Vec<(u32, usize)> = Vec::new();
1151            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
1152        }
1153        n
1154    }
1155}
1156impl<'de> DecodeBorrow<'de> for AbortedTransaction {
1157    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
1158        let flex = version >= 12;
1159        let mut out = Self::default();
1160        if version >= 4 {
1161            out.producer_id = get_i64(buf)?;
1162        }
1163        if version >= 4 {
1164            out.first_offset = get_i64(buf)?;
1165        }
1166        if flex {
1167            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
1168        }
1169        Ok(out)
1170    }
1171}
1172#[cfg(test)]
1173impl AbortedTransaction {
1174    #[must_use]
1175    pub fn populated(version: i16) -> Self {
1176        let mut m = Self::default();
1177        if version >= 4 {
1178            m.producer_id = 1i64;
1179        }
1180        if version >= 4 {
1181            m.first_offset = 1i64;
1182        }
1183        m
1184    }
1185}
1186#[derive(Debug, Clone, PartialEq, Eq, Default)]
1187pub struct NodeEndpoint<'a> {
1188    pub node_id: i32,
1189    pub host: &'a str,
1190    pub port: i32,
1191    pub rack: Option<&'a str>,
1192    pub unknown_tagged_fields: UnknownTaggedFields,
1193}
1194impl NodeEndpoint<'_> {
1195    /// # Panics
1196    ///
1197    /// Panics if a records field contains an invalid encoded record batch.
1198    #[must_use]
1199    pub fn to_owned(&self) -> crate::owned::fetch_response::NodeEndpoint {
1200        crate::owned::fetch_response::NodeEndpoint {
1201            node_id: (self.node_id),
1202            host: (self.host).to_string(),
1203            port: (self.port),
1204            rack: (self.rack).map(std::string::ToString::to_string),
1205            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
1206        }
1207    }
1208}
1209impl Encode for NodeEndpoint<'_> {
1210    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
1211        let flex = version >= 12;
1212        if version >= 16 {
1213            put_i32(buf, self.node_id);
1214        }
1215        if version >= 16 {
1216            if flex {
1217                let () = put_compact_string(buf, self.host);
1218            } else {
1219                let () = put_string(buf, self.host);
1220            }
1221        }
1222        if version >= 16 {
1223            put_i32(buf, self.port);
1224        }
1225        if version >= 16 {
1226            if flex {
1227                let () = put_compact_nullable_string(buf, self.rack);
1228            } else {
1229                let () = put_nullable_string(buf, self.rack);
1230            }
1231        }
1232        if flex {
1233            let tagged = WriteTaggedFields::new();
1234            tagged.write(buf, &self.unknown_tagged_fields);
1235        }
1236        Ok(())
1237    }
1238    fn encoded_len(&self, version: i16) -> usize {
1239        let flex = version >= 12;
1240        let mut n: usize = 0;
1241        if version >= 16 {
1242            n += 4;
1243        }
1244        if version >= 16 {
1245            n += if flex {
1246                compact_string_len(self.host)
1247            } else {
1248                string_len(self.host)
1249            };
1250        }
1251        if version >= 16 {
1252            n += 4;
1253        }
1254        if version >= 16 {
1255            n += if flex {
1256                compact_nullable_string_len(self.rack)
1257            } else {
1258                nullable_string_len(self.rack)
1259            };
1260        }
1261        if flex {
1262            let known_pairs: Vec<(u32, usize)> = Vec::new();
1263            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
1264        }
1265        n
1266    }
1267}
1268impl<'de> DecodeBorrow<'de> for NodeEndpoint<'de> {
1269    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
1270        let flex = version >= 12;
1271        let mut out = Self::default();
1272        if version >= 16 {
1273            out.node_id = get_i32(buf)?;
1274        }
1275        if version >= 16 {
1276            out.host = if flex {
1277                get_compact_string_borrowed(buf)?
1278            } else {
1279                get_string_borrowed(buf)?
1280            };
1281        }
1282        if version >= 16 {
1283            out.port = get_i32(buf)?;
1284        }
1285        if version >= 16 {
1286            out.rack = if flex {
1287                get_compact_nullable_string_borrowed(buf)?
1288            } else {
1289                get_nullable_string_borrowed(buf)?
1290            };
1291        }
1292        if flex {
1293            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
1294        }
1295        Ok(out)
1296    }
1297}
1298#[cfg(test)]
1299impl NodeEndpoint<'_> {
1300    #[must_use]
1301    pub fn populated(version: i16) -> Self {
1302        let mut m = Self::default();
1303        if version >= 16 {
1304            m.node_id = 1i32;
1305        }
1306        if version >= 16 {
1307            m.host = "x";
1308        }
1309        if version >= 16 {
1310            m.port = 1i32;
1311        }
1312        if version >= 16 {
1313            m.rack = Some("x");
1314        }
1315        m
1316    }
1317}