Skip to main content

odradek_protocol/messages/
list_offsets_response.rs

1//! `ListOffsetsResponse` — generated from `schemas/ListOffsetsResponse.json`.
2//!
3//! @generated by `cargo xtask codegen`. Do not edit by hand.
4
5#![allow(
6    clippy::field_reassign_with_default,
7    clippy::nonminimal_bool,
8    clippy::derivable_impls,
9    clippy::borrow_deref_ref,
10    clippy::double_parens,
11    unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21/// Whether `version` of this message uses the flexible (compact) encoding.
22pub fn is_flexible(version: i16) -> bool {
23    let _ = version;
24    version >= 6
25}
26
27/// `ListOffsetsResponse`, versions 1-10.
28#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct ListOffsetsResponse {
31    /// 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.
32    pub throttle_time_ms: i32,
33    /// Each topic in the response.
34    pub topics: Vec<ListOffsetsTopicResponse>,
35    /// Tagged fields not (yet) known to this crate, kept raw.
36    pub unknown_tagged_fields: Vec<RawTaggedField>,
37}
38
39impl Default for ListOffsetsResponse {
40    fn default() -> Self {
41        Self {
42            throttle_time_ms: 0,
43            topics: Vec::new(),
44            unknown_tagged_fields: Vec::new(),
45        }
46    }
47}
48
49impl ListOffsetsResponse {
50    /// This message's api key in Kafka's registry.
51    pub const API_KEY: i16 = 2;
52    /// The lowest schema version this snapshot can speak.
53    pub const MIN_VERSION: i16 = 1;
54    /// The highest schema version this snapshot can speak.
55    pub const MAX_VERSION: i16 = 10;
56
57    /// Encode this `ListOffsetsResponse` at `version`.
58    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
59        if version >= 2 {
60            buf.put_i32(self.throttle_time_ms);
61        }
62        if is_flexible(version) {
63            wire::put_compact_array_len(buf, Some(self.topics.len()));
64        } else {
65            wire::put_array_len(buf, Some(self.topics.len()))?;
66        }
67        for item in &self.topics {
68            item.encode(buf, version)?;
69        }
70        if is_flexible(version) {
71            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
72        }
73        Ok(())
74    }
75
76    /// Decode one `ListOffsetsResponse`, bounding allocation by the default
77    /// [`Limits`] derived from `buf`'s remaining length.
78    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
79        Self::decode_with_limits(buf, version, Limits::default())
80    }
81
82    /// Decode one `ListOffsetsResponse` under `limits`.
83    pub fn decode_with_limits(
84        buf: &mut impl Buf,
85        version: i16,
86        limits: Limits,
87    ) -> Result<Self, DecodeError> {
88        let mut budget = limits.budget(buf.remaining());
89        Self::decode_with_budget(buf, version, &mut budget)
90    }
91
92    /// Decode one `ListOffsetsResponse` against an existing `budget`.
93    pub fn decode_with_budget(
94        buf: &mut impl Buf,
95        version: i16,
96        budget: &mut Budget,
97    ) -> Result<Self, DecodeError> {
98        let mut this = Self::default();
99        if version >= 2 {
100            this.throttle_time_ms = wire::get_i32(buf)?;
101        }
102        this.topics = {
103            let len = if is_flexible(version) {
104                wire::get_compact_array_len(buf)?
105            } else {
106                wire::get_array_len(buf)?
107            };
108            match len {
109                None => return Err(DecodeError::InvalidLength(-1)),
110                Some(n) => {
111                    let mut items = Vec::new();
112                    for _ in 0..n {
113                        let mark = buf.remaining();
114                        let item =
115                            ListOffsetsTopicResponse::decode_with_budget(buf, version, budget)?;
116                        budget.progress(mark, buf.remaining())?;
117                        budget.push(&mut items, item)?;
118                    }
119                    items
120                }
121            }
122        };
123        if is_flexible(version) {
124            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
125        }
126        Ok(this)
127    }
128}
129
130impl crate::Message for ListOffsetsResponse {
131    const API_KEY: i16 = 2;
132    const MIN_VERSION: i16 = 1;
133    const MAX_VERSION: i16 = 10;
134
135    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
136        ListOffsetsResponse::encode(self, buf, version)
137    }
138    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
139        ListOffsetsResponse::decode(buf, version)
140    }
141    fn decode_with_limits(
142        buf: &mut impl Buf,
143        version: i16,
144        limits: Limits,
145    ) -> Result<Self, DecodeError> {
146        ListOffsetsResponse::decode_with_limits(buf, version, limits)
147    }
148    fn throttle_time_ms(&self) -> Option<i32> {
149        Some(self.throttle_time_ms)
150    }
151}
152
153/// `ListOffsetsTopicResponse` (nested in [`ListOffsetsResponse`]).
154#[derive(Debug, Clone, PartialEq)]
155#[non_exhaustive]
156pub struct ListOffsetsTopicResponse {
157    /// The topic name.
158    pub name: String,
159    /// Each partition in the response.
160    pub partitions: Vec<ListOffsetsPartitionResponse>,
161    /// Tagged fields not (yet) known to this crate, kept raw.
162    pub unknown_tagged_fields: Vec<RawTaggedField>,
163}
164
165impl Default for ListOffsetsTopicResponse {
166    fn default() -> Self {
167        Self {
168            name: String::new(),
169            partitions: Vec::new(),
170            unknown_tagged_fields: Vec::new(),
171        }
172    }
173}
174
175impl ListOffsetsTopicResponse {
176    /// Encode this `ListOffsetsTopicResponse` at `version`.
177    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
178        if is_flexible(version) {
179            wire::put_compact_string(buf, &self.name);
180        } else {
181            wire::put_string(buf, &self.name)?;
182        }
183        if is_flexible(version) {
184            wire::put_compact_array_len(buf, Some(self.partitions.len()));
185        } else {
186            wire::put_array_len(buf, Some(self.partitions.len()))?;
187        }
188        for item in &self.partitions {
189            item.encode(buf, version)?;
190        }
191        if is_flexible(version) {
192            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
193        }
194        Ok(())
195    }
196
197    /// Decode one `ListOffsetsTopicResponse`, bounding allocation by the default
198    /// [`Limits`] derived from `buf`'s remaining length.
199    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
200        Self::decode_with_limits(buf, version, Limits::default())
201    }
202
203    /// Decode one `ListOffsetsTopicResponse` under `limits`.
204    pub fn decode_with_limits(
205        buf: &mut impl Buf,
206        version: i16,
207        limits: Limits,
208    ) -> Result<Self, DecodeError> {
209        let mut budget = limits.budget(buf.remaining());
210        Self::decode_with_budget(buf, version, &mut budget)
211    }
212
213    /// Decode one `ListOffsetsTopicResponse` against an existing `budget`.
214    pub fn decode_with_budget(
215        buf: &mut impl Buf,
216        version: i16,
217        budget: &mut Budget,
218    ) -> Result<Self, DecodeError> {
219        let mut this = Self::default();
220        this.name = if is_flexible(version) {
221            wire::get_compact_string(buf)?
222        } else {
223            wire::get_string(buf)?
224        };
225        this.partitions = {
226            let len = if is_flexible(version) {
227                wire::get_compact_array_len(buf)?
228            } else {
229                wire::get_array_len(buf)?
230            };
231            match len {
232                None => return Err(DecodeError::InvalidLength(-1)),
233                Some(n) => {
234                    let mut items = Vec::new();
235                    for _ in 0..n {
236                        let mark = buf.remaining();
237                        let item =
238                            ListOffsetsPartitionResponse::decode_with_budget(buf, version, budget)?;
239                        budget.progress(mark, buf.remaining())?;
240                        budget.push(&mut items, item)?;
241                    }
242                    items
243                }
244            }
245        };
246        if is_flexible(version) {
247            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
248        }
249        Ok(this)
250    }
251}
252
253/// `ListOffsetsPartitionResponse` (nested in [`ListOffsetsResponse`]).
254#[derive(Debug, Clone, PartialEq)]
255#[non_exhaustive]
256pub struct ListOffsetsPartitionResponse {
257    /// The partition index.
258    pub partition_index: i32,
259    /// The partition error code, or 0 if there was no error.
260    pub error_code: i16,
261    /// The timestamp associated with the returned offset.
262    pub timestamp: i64,
263    /// The returned offset.
264    pub offset: i64,
265    /// The leader epoch associated with the returned offset.
266    pub leader_epoch: i32,
267    /// Tagged fields not (yet) known to this crate, kept raw.
268    pub unknown_tagged_fields: Vec<RawTaggedField>,
269}
270
271impl Default for ListOffsetsPartitionResponse {
272    fn default() -> Self {
273        Self {
274            partition_index: 0,
275            error_code: 0,
276            timestamp: -1,
277            offset: -1,
278            leader_epoch: -1,
279            unknown_tagged_fields: Vec::new(),
280        }
281    }
282}
283
284impl ListOffsetsPartitionResponse {
285    /// Encode this `ListOffsetsPartitionResponse` at `version`.
286    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
287        buf.put_i32(self.partition_index);
288        buf.put_i16(self.error_code);
289        buf.put_i64(self.timestamp);
290        buf.put_i64(self.offset);
291        if version >= 4 {
292            buf.put_i32(self.leader_epoch);
293        }
294        if is_flexible(version) {
295            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
296        }
297        Ok(())
298    }
299
300    /// Decode one `ListOffsetsPartitionResponse`, bounding allocation by the default
301    /// [`Limits`] derived from `buf`'s remaining length.
302    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
303        Self::decode_with_limits(buf, version, Limits::default())
304    }
305
306    /// Decode one `ListOffsetsPartitionResponse` under `limits`.
307    pub fn decode_with_limits(
308        buf: &mut impl Buf,
309        version: i16,
310        limits: Limits,
311    ) -> Result<Self, DecodeError> {
312        let mut budget = limits.budget(buf.remaining());
313        Self::decode_with_budget(buf, version, &mut budget)
314    }
315
316    /// Decode one `ListOffsetsPartitionResponse` against an existing `budget`.
317    pub fn decode_with_budget(
318        buf: &mut impl Buf,
319        version: i16,
320        budget: &mut Budget,
321    ) -> Result<Self, DecodeError> {
322        let mut this = Self::default();
323        this.partition_index = wire::get_i32(buf)?;
324        this.error_code = wire::get_i16(buf)?;
325        this.timestamp = wire::get_i64(buf)?;
326        this.offset = wire::get_i64(buf)?;
327        if version >= 4 {
328            this.leader_epoch = wire::get_i32(buf)?;
329        }
330        if is_flexible(version) {
331            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
332        }
333        Ok(this)
334    }
335}