Skip to main content

odradek_protocol/messages/
fetch_response.rs

1//! `FetchResponse` — generated from `schemas/FetchResponse.json`.
2//!
3//! @generated by `cargo xtask codegen`. Do not edit by hand.
4//!
5//! Tagged fields are materialized as `Option` struct fields (`None` = absent
6//! on the wire); unknown tags still round-trip through `unknown_tagged_fields`.
7
8#![allow(
9    clippy::field_reassign_with_default,
10    clippy::nonminimal_bool,
11    clippy::derivable_impls,
12    clippy::borrow_deref_ref,
13    clippy::double_parens,
14    unused_parens
15)]
16
17use bytes::{Buf, BufMut};
18
19use crate::budget::{Budget, Limits};
20#[allow(unused_imports)]
21use crate::error::{DecodeError, EncodeError};
22use crate::wire::{self, RawTaggedField};
23
24/// Whether `version` of this message uses the flexible (compact) encoding.
25pub fn is_flexible(version: i16) -> bool {
26    let _ = version;
27    version >= 12
28}
29
30/// `FetchResponse`, versions 4-18.
31#[derive(Debug, Clone, PartialEq)]
32#[non_exhaustive]
33pub struct FetchResponse {
34    /// The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
35    pub throttle_time_ms: i32,
36    /// The top level response error code.
37    pub error_code: i16,
38    /// The fetch session ID, or 0 if this is not part of a fetch session.
39    pub session_id: i32,
40    /// The response topics.
41    pub responses: Vec<FetchableTopicResponse>,
42    /// Endpoints for all current-leaders enumerated in PartitionData, with errors NOT_LEADER_OR_FOLLOWER & FENCED_LEADER_EPOCH.
43    /// (Tagged field 0; `None` = absent on the wire.)
44    pub node_endpoints: Option<Vec<NodeEndpoint>>,
45    /// Tagged fields not (yet) known to this crate, kept raw.
46    pub unknown_tagged_fields: Vec<RawTaggedField>,
47}
48
49impl Default for FetchResponse {
50    fn default() -> Self {
51        Self {
52            throttle_time_ms: 0,
53            error_code: 0,
54            session_id: 0,
55            responses: Vec::new(),
56            node_endpoints: None,
57            unknown_tagged_fields: Vec::new(),
58        }
59    }
60}
61
62impl FetchResponse {
63    /// This message's api key in Kafka's registry.
64    pub const API_KEY: i16 = 1;
65    /// The lowest schema version this snapshot can speak.
66    pub const MIN_VERSION: i16 = 4;
67    /// The highest schema version this snapshot can speak.
68    pub const MAX_VERSION: i16 = 18;
69
70    /// Encode this `FetchResponse` at `version`.
71    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
72        buf.put_i32(self.throttle_time_ms);
73        if version >= 7 {
74            buf.put_i16(self.error_code);
75        }
76        if version >= 7 {
77            buf.put_i32(self.session_id);
78        }
79        if is_flexible(version) {
80            wire::put_compact_array_len(buf, Some(self.responses.len()));
81        } else {
82            wire::put_array_len(buf, Some(self.responses.len()))?;
83        }
84        for item in &self.responses {
85            item.encode(buf, version)?;
86        }
87        if is_flexible(version) {
88            let mut tagged = self.unknown_tagged_fields.clone();
89            if version >= 16 {
90                if let Some(v) = &self.node_endpoints {
91                    let mut tag_data = bytes::BytesMut::new();
92                    {
93                        let buf = &mut tag_data;
94                        if is_flexible(version) {
95                            wire::put_compact_array_len(buf, Some((*v).len()));
96                        } else {
97                            wire::put_array_len(buf, Some((*v).len()))?;
98                        }
99                        for item in &(*v) {
100                            item.encode(buf, version)?;
101                        }
102                    }
103                    tagged.push(RawTaggedField {
104                        tag: 0,
105                        data: tag_data.freeze(),
106                    });
107                }
108            }
109            tagged.sort_by_key(|f| f.tag);
110            wire::put_tagged_fields(buf, &tagged);
111        }
112        Ok(())
113    }
114
115    /// Decode one `FetchResponse`, bounding allocation by the default
116    /// [`Limits`] derived from `buf`'s remaining length.
117    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
118        Self::decode_with_limits(buf, version, Limits::default())
119    }
120
121    /// Decode one `FetchResponse` under `limits`.
122    pub fn decode_with_limits(
123        buf: &mut impl Buf,
124        version: i16,
125        limits: Limits,
126    ) -> Result<Self, DecodeError> {
127        let mut budget = limits.budget(buf.remaining());
128        Self::decode_with_budget(buf, version, &mut budget)
129    }
130
131    /// Decode one `FetchResponse` against an existing `budget`.
132    pub fn decode_with_budget(
133        buf: &mut impl Buf,
134        version: i16,
135        budget: &mut Budget,
136    ) -> Result<Self, DecodeError> {
137        let mut this = Self::default();
138        this.throttle_time_ms = wire::get_i32(buf)?;
139        if version >= 7 {
140            this.error_code = wire::get_i16(buf)?;
141        }
142        if version >= 7 {
143            this.session_id = wire::get_i32(buf)?;
144        }
145        this.responses = {
146            let len = if is_flexible(version) {
147                wire::get_compact_array_len(buf)?
148            } else {
149                wire::get_array_len(buf)?
150            };
151            match len {
152                None => return Err(DecodeError::InvalidLength(-1)),
153                Some(n) => {
154                    let mut items = Vec::new();
155                    for _ in 0..n {
156                        let mark = buf.remaining();
157                        let item =
158                            FetchableTopicResponse::decode_with_budget(buf, version, budget)?;
159                        budget.progress(mark, buf.remaining())?;
160                        budget.push(&mut items, item)?;
161                    }
162                    items
163                }
164            }
165        };
166        if is_flexible(version) {
167            for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
168                if raw.tag == 0 && (version >= 16) {
169                    let mut tag_data = raw.data;
170                    let buf = &mut tag_data;
171                    this.node_endpoints = Some({
172                        let len = if is_flexible(version) {
173                            wire::get_compact_array_len(buf)?
174                        } else {
175                            wire::get_array_len(buf)?
176                        };
177                        match len {
178                            None => return Err(DecodeError::InvalidLength(-1)),
179                            Some(n) => {
180                                let mut items = Vec::new();
181                                for _ in 0..n {
182                                    let mark = buf.remaining();
183                                    let item =
184                                        NodeEndpoint::decode_with_budget(buf, version, budget)?;
185                                    budget.progress(mark, buf.remaining())?;
186                                    budget.push(&mut items, item)?;
187                                }
188                                items
189                            }
190                        }
191                    });
192                    if buf.has_remaining() {
193                        return Err(DecodeError::InvalidLength(
194                            i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
195                        ));
196                    }
197                } else {
198                    budget.push(&mut this.unknown_tagged_fields, raw)?;
199                }
200            }
201        }
202        Ok(this)
203    }
204}
205
206impl crate::Message for FetchResponse {
207    const API_KEY: i16 = 1;
208    const MIN_VERSION: i16 = 4;
209    const MAX_VERSION: i16 = 18;
210
211    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
212        FetchResponse::encode(self, buf, version)
213    }
214    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
215        FetchResponse::decode(buf, version)
216    }
217    fn decode_with_limits(
218        buf: &mut impl Buf,
219        version: i16,
220        limits: Limits,
221    ) -> Result<Self, DecodeError> {
222        FetchResponse::decode_with_limits(buf, version, limits)
223    }
224    fn throttle_time_ms(&self) -> Option<i32> {
225        Some(self.throttle_time_ms)
226    }
227}
228
229/// `FetchableTopicResponse` (nested in [`FetchResponse`]).
230#[derive(Debug, Clone, PartialEq)]
231#[non_exhaustive]
232pub struct FetchableTopicResponse {
233    /// The topic name.
234    pub topic: String,
235    /// The unique topic ID.
236    pub topic_id: [u8; 16],
237    /// The topic partitions.
238    pub partitions: Vec<PartitionData>,
239    /// Tagged fields not (yet) known to this crate, kept raw.
240    pub unknown_tagged_fields: Vec<RawTaggedField>,
241}
242
243impl Default for FetchableTopicResponse {
244    fn default() -> Self {
245        Self {
246            topic: String::new(),
247            topic_id: [0u8; 16],
248            partitions: Vec::new(),
249            unknown_tagged_fields: Vec::new(),
250        }
251    }
252}
253
254impl FetchableTopicResponse {
255    /// Encode this `FetchableTopicResponse` at `version`.
256    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
257        if version <= 12 {
258            if is_flexible(version) {
259                wire::put_compact_string(buf, &self.topic);
260            } else {
261                wire::put_string(buf, &self.topic)?;
262            }
263        }
264        if version >= 13 {
265            wire::put_uuid(buf, self.topic_id);
266        }
267        if is_flexible(version) {
268            wire::put_compact_array_len(buf, Some(self.partitions.len()));
269        } else {
270            wire::put_array_len(buf, Some(self.partitions.len()))?;
271        }
272        for item in &self.partitions {
273            item.encode(buf, version)?;
274        }
275        if is_flexible(version) {
276            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
277        }
278        Ok(())
279    }
280
281    /// Decode one `FetchableTopicResponse`, bounding allocation by the default
282    /// [`Limits`] derived from `buf`'s remaining length.
283    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
284        Self::decode_with_limits(buf, version, Limits::default())
285    }
286
287    /// Decode one `FetchableTopicResponse` under `limits`.
288    pub fn decode_with_limits(
289        buf: &mut impl Buf,
290        version: i16,
291        limits: Limits,
292    ) -> Result<Self, DecodeError> {
293        let mut budget = limits.budget(buf.remaining());
294        Self::decode_with_budget(buf, version, &mut budget)
295    }
296
297    /// Decode one `FetchableTopicResponse` against an existing `budget`.
298    pub fn decode_with_budget(
299        buf: &mut impl Buf,
300        version: i16,
301        budget: &mut Budget,
302    ) -> Result<Self, DecodeError> {
303        let mut this = Self::default();
304        if version <= 12 {
305            this.topic = if is_flexible(version) {
306                wire::get_compact_string(buf)?
307            } else {
308                wire::get_string(buf)?
309            };
310        }
311        if version >= 13 {
312            this.topic_id = wire::get_uuid(buf)?;
313        }
314        this.partitions = {
315            let len = if is_flexible(version) {
316                wire::get_compact_array_len(buf)?
317            } else {
318                wire::get_array_len(buf)?
319            };
320            match len {
321                None => return Err(DecodeError::InvalidLength(-1)),
322                Some(n) => {
323                    let mut items = Vec::new();
324                    for _ in 0..n {
325                        let mark = buf.remaining();
326                        let item = PartitionData::decode_with_budget(buf, version, budget)?;
327                        budget.progress(mark, buf.remaining())?;
328                        budget.push(&mut items, item)?;
329                    }
330                    items
331                }
332            }
333        };
334        if is_flexible(version) {
335            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
336        }
337        Ok(this)
338    }
339}
340
341/// `PartitionData` (nested in [`FetchResponse`]).
342#[derive(Debug, Clone, PartialEq)]
343#[non_exhaustive]
344pub struct PartitionData {
345    /// The partition index.
346    pub partition_index: i32,
347    /// The error code, or 0 if there was no fetch error.
348    pub error_code: i16,
349    /// The current high water mark.
350    pub high_watermark: i64,
351    /// The last stable offset (or LSO) of the partition. This is the last offset such that the state of all transactional records prior to this offset have been decided (ABORTED or COMMITTED).
352    pub last_stable_offset: i64,
353    /// The current log start offset.
354    pub log_start_offset: i64,
355    /// The aborted transactions.
356    pub aborted_transactions: Option<Vec<AbortedTransaction>>,
357    /// The preferred read replica for the consumer to use on its next fetch request.
358    pub preferred_read_replica: i32,
359    /// The record data.
360    pub records: Option<bytes::Bytes>,
361    /// In case divergence is detected based on the `LastFetchedEpoch` and `FetchOffset` in the request, this field indicates the largest epoch and its end offset such that subsequent records are known to diverge.
362    /// (Tagged field 0; `None` = absent on the wire.)
363    pub diverging_epoch: Option<EpochEndOffset>,
364    /// The current leader of the partition.
365    /// (Tagged field 1; `None` = absent on the wire.)
366    pub current_leader: Option<LeaderIdAndEpoch>,
367    /// In the case of fetching an offset less than the LogStartOffset, this is the end offset and epoch that should be used in the FetchSnapshot request.
368    /// (Tagged field 2; `None` = absent on the wire.)
369    pub snapshot_id: Option<SnapshotId>,
370    /// Tagged fields not (yet) known to this crate, kept raw.
371    pub unknown_tagged_fields: Vec<RawTaggedField>,
372}
373
374impl Default for PartitionData {
375    fn default() -> Self {
376        Self {
377            partition_index: 0,
378            error_code: 0,
379            high_watermark: 0,
380            last_stable_offset: -1,
381            log_start_offset: -1,
382            aborted_transactions: Some(Vec::new()),
383            preferred_read_replica: -1,
384            records: Some(bytes::Bytes::new()),
385            diverging_epoch: None,
386            current_leader: None,
387            snapshot_id: None,
388            unknown_tagged_fields: Vec::new(),
389        }
390    }
391}
392
393impl PartitionData {
394    /// Encode this `PartitionData` at `version`.
395    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
396        buf.put_i32(self.partition_index);
397        buf.put_i16(self.error_code);
398        buf.put_i64(self.high_watermark);
399        buf.put_i64(self.last_stable_offset);
400        if version >= 5 {
401            buf.put_i64(self.log_start_offset);
402        }
403        match &self.aborted_transactions {
404            Some(items) => {
405                if is_flexible(version) {
406                    wire::put_compact_array_len(buf, Some(items.len()));
407                } else {
408                    wire::put_array_len(buf, Some(items.len()))?;
409                }
410                for item in items {
411                    item.encode(buf, version)?;
412                }
413            }
414            None => {
415                if is_flexible(version) {
416                    wire::put_compact_array_len(buf, None);
417                } else {
418                    wire::put_array_len(buf, None)?;
419                }
420            }
421        }
422        if version >= 11 {
423            buf.put_i32(self.preferred_read_replica);
424        }
425        if is_flexible(version) {
426            wire::put_compact_nullable_bytes(buf, self.records.as_deref());
427        } else {
428            wire::put_nullable_bytes(buf, self.records.as_deref())?;
429        }
430        if is_flexible(version) {
431            let mut tagged = self.unknown_tagged_fields.clone();
432            if version >= 12 {
433                if let Some(v) = &self.diverging_epoch {
434                    let mut tag_data = bytes::BytesMut::new();
435                    {
436                        let buf = &mut tag_data;
437                        (*v).encode(buf, version)?;
438                    }
439                    tagged.push(RawTaggedField {
440                        tag: 0,
441                        data: tag_data.freeze(),
442                    });
443                }
444            }
445            if version >= 12 {
446                if let Some(v) = &self.current_leader {
447                    let mut tag_data = bytes::BytesMut::new();
448                    {
449                        let buf = &mut tag_data;
450                        (*v).encode(buf, version)?;
451                    }
452                    tagged.push(RawTaggedField {
453                        tag: 1,
454                        data: tag_data.freeze(),
455                    });
456                }
457            }
458            if version >= 12 {
459                if let Some(v) = &self.snapshot_id {
460                    let mut tag_data = bytes::BytesMut::new();
461                    {
462                        let buf = &mut tag_data;
463                        (*v).encode(buf, version)?;
464                    }
465                    tagged.push(RawTaggedField {
466                        tag: 2,
467                        data: tag_data.freeze(),
468                    });
469                }
470            }
471            tagged.sort_by_key(|f| f.tag);
472            wire::put_tagged_fields(buf, &tagged);
473        }
474        Ok(())
475    }
476
477    /// Decode one `PartitionData`, bounding allocation by the default
478    /// [`Limits`] derived from `buf`'s remaining length.
479    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
480        Self::decode_with_limits(buf, version, Limits::default())
481    }
482
483    /// Decode one `PartitionData` under `limits`.
484    pub fn decode_with_limits(
485        buf: &mut impl Buf,
486        version: i16,
487        limits: Limits,
488    ) -> Result<Self, DecodeError> {
489        let mut budget = limits.budget(buf.remaining());
490        Self::decode_with_budget(buf, version, &mut budget)
491    }
492
493    /// Decode one `PartitionData` against an existing `budget`.
494    pub fn decode_with_budget(
495        buf: &mut impl Buf,
496        version: i16,
497        budget: &mut Budget,
498    ) -> Result<Self, DecodeError> {
499        let mut this = Self::default();
500        this.partition_index = wire::get_i32(buf)?;
501        this.error_code = wire::get_i16(buf)?;
502        this.high_watermark = wire::get_i64(buf)?;
503        this.last_stable_offset = wire::get_i64(buf)?;
504        if version >= 5 {
505            this.log_start_offset = wire::get_i64(buf)?;
506        }
507        this.aborted_transactions = {
508            let len = if is_flexible(version) {
509                wire::get_compact_array_len(buf)?
510            } else {
511                wire::get_array_len(buf)?
512            };
513            match len {
514                None => None,
515                Some(n) => {
516                    let mut items = Vec::new();
517                    for _ in 0..n {
518                        let mark = buf.remaining();
519                        let item = AbortedTransaction::decode_with_budget(buf, version, budget)?;
520                        budget.progress(mark, buf.remaining())?;
521                        budget.push(&mut items, item)?;
522                    }
523                    Some(items)
524                }
525            }
526        };
527        if version >= 11 {
528            this.preferred_read_replica = wire::get_i32(buf)?;
529        }
530        this.records = if is_flexible(version) {
531            wire::get_compact_nullable_bytes(buf)?
532        } else {
533            wire::get_nullable_bytes(buf)?
534        };
535        if is_flexible(version) {
536            for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
537                if raw.tag == 0 && (version >= 12) {
538                    let mut tag_data = raw.data;
539                    let buf = &mut tag_data;
540                    this.diverging_epoch =
541                        Some(EpochEndOffset::decode_with_budget(buf, version, budget)?);
542                    if buf.has_remaining() {
543                        return Err(DecodeError::InvalidLength(
544                            i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
545                        ));
546                    }
547                } else if raw.tag == 1 && (version >= 12) {
548                    let mut tag_data = raw.data;
549                    let buf = &mut tag_data;
550                    this.current_leader =
551                        Some(LeaderIdAndEpoch::decode_with_budget(buf, version, budget)?);
552                    if buf.has_remaining() {
553                        return Err(DecodeError::InvalidLength(
554                            i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
555                        ));
556                    }
557                } else if raw.tag == 2 && (version >= 12) {
558                    let mut tag_data = raw.data;
559                    let buf = &mut tag_data;
560                    this.snapshot_id = Some(SnapshotId::decode_with_budget(buf, version, budget)?);
561                    if buf.has_remaining() {
562                        return Err(DecodeError::InvalidLength(
563                            i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
564                        ));
565                    }
566                } else {
567                    budget.push(&mut this.unknown_tagged_fields, raw)?;
568                }
569            }
570        }
571        Ok(this)
572    }
573}
574
575/// `EpochEndOffset` (nested in [`FetchResponse`]).
576#[derive(Debug, Clone, PartialEq)]
577#[non_exhaustive]
578pub struct EpochEndOffset {
579    /// The largest epoch.
580    pub epoch: i32,
581    /// The end offset of the epoch.
582    pub end_offset: i64,
583    /// Tagged fields not (yet) known to this crate, kept raw.
584    pub unknown_tagged_fields: Vec<RawTaggedField>,
585}
586
587impl Default for EpochEndOffset {
588    fn default() -> Self {
589        Self {
590            epoch: -1,
591            end_offset: -1,
592            unknown_tagged_fields: Vec::new(),
593        }
594    }
595}
596
597impl EpochEndOffset {
598    /// Encode this `EpochEndOffset` at `version`.
599    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
600        if version >= 12 {
601            buf.put_i32(self.epoch);
602        }
603        if version >= 12 {
604            buf.put_i64(self.end_offset);
605        }
606        if is_flexible(version) {
607            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
608        }
609        Ok(())
610    }
611
612    /// Decode one `EpochEndOffset`, bounding allocation by the default
613    /// [`Limits`] derived from `buf`'s remaining length.
614    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
615        Self::decode_with_limits(buf, version, Limits::default())
616    }
617
618    /// Decode one `EpochEndOffset` under `limits`.
619    pub fn decode_with_limits(
620        buf: &mut impl Buf,
621        version: i16,
622        limits: Limits,
623    ) -> Result<Self, DecodeError> {
624        let mut budget = limits.budget(buf.remaining());
625        Self::decode_with_budget(buf, version, &mut budget)
626    }
627
628    /// Decode one `EpochEndOffset` against an existing `budget`.
629    pub fn decode_with_budget(
630        buf: &mut impl Buf,
631        version: i16,
632        budget: &mut Budget,
633    ) -> Result<Self, DecodeError> {
634        let mut this = Self::default();
635        if version >= 12 {
636            this.epoch = wire::get_i32(buf)?;
637        }
638        if version >= 12 {
639            this.end_offset = wire::get_i64(buf)?;
640        }
641        if is_flexible(version) {
642            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
643        }
644        Ok(this)
645    }
646}
647
648/// `LeaderIdAndEpoch` (nested in [`FetchResponse`]).
649#[derive(Debug, Clone, PartialEq)]
650#[non_exhaustive]
651pub struct LeaderIdAndEpoch {
652    /// The ID of the current leader or -1 if the leader is unknown.
653    pub leader_id: i32,
654    /// The latest known leader epoch.
655    pub leader_epoch: i32,
656    /// Tagged fields not (yet) known to this crate, kept raw.
657    pub unknown_tagged_fields: Vec<RawTaggedField>,
658}
659
660impl Default for LeaderIdAndEpoch {
661    fn default() -> Self {
662        Self {
663            leader_id: -1,
664            leader_epoch: -1,
665            unknown_tagged_fields: Vec::new(),
666        }
667    }
668}
669
670impl LeaderIdAndEpoch {
671    /// Encode this `LeaderIdAndEpoch` at `version`.
672    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
673        if version >= 12 {
674            buf.put_i32(self.leader_id);
675        }
676        if version >= 12 {
677            buf.put_i32(self.leader_epoch);
678        }
679        if is_flexible(version) {
680            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
681        }
682        Ok(())
683    }
684
685    /// Decode one `LeaderIdAndEpoch`, bounding allocation by the default
686    /// [`Limits`] derived from `buf`'s remaining length.
687    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
688        Self::decode_with_limits(buf, version, Limits::default())
689    }
690
691    /// Decode one `LeaderIdAndEpoch` under `limits`.
692    pub fn decode_with_limits(
693        buf: &mut impl Buf,
694        version: i16,
695        limits: Limits,
696    ) -> Result<Self, DecodeError> {
697        let mut budget = limits.budget(buf.remaining());
698        Self::decode_with_budget(buf, version, &mut budget)
699    }
700
701    /// Decode one `LeaderIdAndEpoch` against an existing `budget`.
702    pub fn decode_with_budget(
703        buf: &mut impl Buf,
704        version: i16,
705        budget: &mut Budget,
706    ) -> Result<Self, DecodeError> {
707        let mut this = Self::default();
708        if version >= 12 {
709            this.leader_id = wire::get_i32(buf)?;
710        }
711        if version >= 12 {
712            this.leader_epoch = wire::get_i32(buf)?;
713        }
714        if is_flexible(version) {
715            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
716        }
717        Ok(this)
718    }
719}
720
721/// `SnapshotId` (nested in [`FetchResponse`]).
722#[derive(Debug, Clone, PartialEq)]
723#[non_exhaustive]
724pub struct SnapshotId {
725    /// The end offset of the epoch.
726    pub end_offset: i64,
727    /// The largest epoch.
728    pub epoch: i32,
729    /// Tagged fields not (yet) known to this crate, kept raw.
730    pub unknown_tagged_fields: Vec<RawTaggedField>,
731}
732
733impl Default for SnapshotId {
734    fn default() -> Self {
735        Self {
736            end_offset: -1,
737            epoch: -1,
738            unknown_tagged_fields: Vec::new(),
739        }
740    }
741}
742
743impl SnapshotId {
744    /// Encode this `SnapshotId` at `version`.
745    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
746        buf.put_i64(self.end_offset);
747        buf.put_i32(self.epoch);
748        if is_flexible(version) {
749            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
750        }
751        Ok(())
752    }
753
754    /// Decode one `SnapshotId`, bounding allocation by the default
755    /// [`Limits`] derived from `buf`'s remaining length.
756    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
757        Self::decode_with_limits(buf, version, Limits::default())
758    }
759
760    /// Decode one `SnapshotId` under `limits`.
761    pub fn decode_with_limits(
762        buf: &mut impl Buf,
763        version: i16,
764        limits: Limits,
765    ) -> Result<Self, DecodeError> {
766        let mut budget = limits.budget(buf.remaining());
767        Self::decode_with_budget(buf, version, &mut budget)
768    }
769
770    /// Decode one `SnapshotId` against an existing `budget`.
771    pub fn decode_with_budget(
772        buf: &mut impl Buf,
773        version: i16,
774        budget: &mut Budget,
775    ) -> Result<Self, DecodeError> {
776        let mut this = Self::default();
777        this.end_offset = wire::get_i64(buf)?;
778        this.epoch = wire::get_i32(buf)?;
779        if is_flexible(version) {
780            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
781        }
782        Ok(this)
783    }
784}
785
786/// `AbortedTransaction` (nested in [`FetchResponse`]).
787#[derive(Debug, Clone, PartialEq)]
788#[non_exhaustive]
789pub struct AbortedTransaction {
790    /// The producer id associated with the aborted transaction.
791    pub producer_id: i64,
792    /// The first offset in the aborted transaction.
793    pub first_offset: i64,
794    /// Tagged fields not (yet) known to this crate, kept raw.
795    pub unknown_tagged_fields: Vec<RawTaggedField>,
796}
797
798impl Default for AbortedTransaction {
799    fn default() -> Self {
800        Self {
801            producer_id: 0,
802            first_offset: 0,
803            unknown_tagged_fields: Vec::new(),
804        }
805    }
806}
807
808impl AbortedTransaction {
809    /// Encode this `AbortedTransaction` at `version`.
810    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
811        buf.put_i64(self.producer_id);
812        buf.put_i64(self.first_offset);
813        if is_flexible(version) {
814            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
815        }
816        Ok(())
817    }
818
819    /// Decode one `AbortedTransaction`, bounding allocation by the default
820    /// [`Limits`] derived from `buf`'s remaining length.
821    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
822        Self::decode_with_limits(buf, version, Limits::default())
823    }
824
825    /// Decode one `AbortedTransaction` under `limits`.
826    pub fn decode_with_limits(
827        buf: &mut impl Buf,
828        version: i16,
829        limits: Limits,
830    ) -> Result<Self, DecodeError> {
831        let mut budget = limits.budget(buf.remaining());
832        Self::decode_with_budget(buf, version, &mut budget)
833    }
834
835    /// Decode one `AbortedTransaction` against an existing `budget`.
836    pub fn decode_with_budget(
837        buf: &mut impl Buf,
838        version: i16,
839        budget: &mut Budget,
840    ) -> Result<Self, DecodeError> {
841        let mut this = Self::default();
842        this.producer_id = wire::get_i64(buf)?;
843        this.first_offset = wire::get_i64(buf)?;
844        if is_flexible(version) {
845            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
846        }
847        Ok(this)
848    }
849}
850
851/// `NodeEndpoint` (nested in [`FetchResponse`]).
852#[derive(Debug, Clone, PartialEq)]
853#[non_exhaustive]
854pub struct NodeEndpoint {
855    /// The ID of the associated node.
856    pub node_id: i32,
857    /// The node's hostname.
858    pub host: String,
859    /// The node's port.
860    pub port: i32,
861    /// The rack of the node, or null if it has not been assigned to a rack.
862    pub rack: Option<String>,
863    /// Tagged fields not (yet) known to this crate, kept raw.
864    pub unknown_tagged_fields: Vec<RawTaggedField>,
865}
866
867impl Default for NodeEndpoint {
868    fn default() -> Self {
869        Self {
870            node_id: 0,
871            host: String::new(),
872            port: 0,
873            rack: None,
874            unknown_tagged_fields: Vec::new(),
875        }
876    }
877}
878
879impl NodeEndpoint {
880    /// Encode this `NodeEndpoint` at `version`.
881    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
882        if version >= 16 {
883            buf.put_i32(self.node_id);
884        }
885        if version >= 16 {
886            if is_flexible(version) {
887                wire::put_compact_string(buf, &self.host);
888            } else {
889                wire::put_string(buf, &self.host)?;
890            }
891        }
892        if version >= 16 {
893            buf.put_i32(self.port);
894        }
895        if version >= 16 {
896            if is_flexible(version) {
897                wire::put_compact_nullable_string(buf, self.rack.as_deref());
898            } else {
899                wire::put_nullable_string(buf, self.rack.as_deref())?;
900            }
901        }
902        if is_flexible(version) {
903            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
904        }
905        Ok(())
906    }
907
908    /// Decode one `NodeEndpoint`, bounding allocation by the default
909    /// [`Limits`] derived from `buf`'s remaining length.
910    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
911        Self::decode_with_limits(buf, version, Limits::default())
912    }
913
914    /// Decode one `NodeEndpoint` under `limits`.
915    pub fn decode_with_limits(
916        buf: &mut impl Buf,
917        version: i16,
918        limits: Limits,
919    ) -> Result<Self, DecodeError> {
920        let mut budget = limits.budget(buf.remaining());
921        Self::decode_with_budget(buf, version, &mut budget)
922    }
923
924    /// Decode one `NodeEndpoint` against an existing `budget`.
925    pub fn decode_with_budget(
926        buf: &mut impl Buf,
927        version: i16,
928        budget: &mut Budget,
929    ) -> Result<Self, DecodeError> {
930        let mut this = Self::default();
931        if version >= 16 {
932            this.node_id = wire::get_i32(buf)?;
933        }
934        if version >= 16 {
935            this.host = if is_flexible(version) {
936                wire::get_compact_string(buf)?
937            } else {
938                wire::get_string(buf)?
939            };
940        }
941        if version >= 16 {
942            this.port = wire::get_i32(buf)?;
943        }
944        if version >= 16 {
945            this.rack = if is_flexible(version) {
946                wire::get_compact_nullable_string(buf)?
947            } else {
948                wire::get_nullable_string(buf)?
949            };
950        }
951        if is_flexible(version) {
952            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
953        }
954        Ok(this)
955    }
956}