kafka_protocol/messages/
offset_for_leader_epoch_response.rs

1//! OffsetForLeaderEpochResponse
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/OffsetForLeaderEpochResponse.json).
4// WARNING: the items of this module are generated and should not be edited directly
5#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15    buf::{ByteBuf, ByteBufMut},
16    compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17    Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20/// Valid versions: 0-4
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct EpochEndOffset {
24    /// The error code 0, or if there was no error.
25    ///
26    /// Supported API versions: 0-4
27    pub error_code: i16,
28
29    /// The partition index.
30    ///
31    /// Supported API versions: 0-4
32    pub partition: i32,
33
34    /// The leader epoch of the partition.
35    ///
36    /// Supported API versions: 1-4
37    pub leader_epoch: i32,
38
39    /// The end offset of the epoch.
40    ///
41    /// Supported API versions: 0-4
42    pub end_offset: i64,
43
44    /// Other tagged fields
45    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl EpochEndOffset {
49    /// Sets `error_code` to the passed value.
50    ///
51    /// The error code 0, or if there was no error.
52    ///
53    /// Supported API versions: 0-4
54    pub fn with_error_code(mut self, value: i16) -> Self {
55        self.error_code = value;
56        self
57    }
58    /// Sets `partition` to the passed value.
59    ///
60    /// The partition index.
61    ///
62    /// Supported API versions: 0-4
63    pub fn with_partition(mut self, value: i32) -> Self {
64        self.partition = value;
65        self
66    }
67    /// Sets `leader_epoch` to the passed value.
68    ///
69    /// The leader epoch of the partition.
70    ///
71    /// Supported API versions: 1-4
72    pub fn with_leader_epoch(mut self, value: i32) -> Self {
73        self.leader_epoch = value;
74        self
75    }
76    /// Sets `end_offset` to the passed value.
77    ///
78    /// The end offset of the epoch.
79    ///
80    /// Supported API versions: 0-4
81    pub fn with_end_offset(mut self, value: i64) -> Self {
82        self.end_offset = value;
83        self
84    }
85    /// Sets unknown_tagged_fields to the passed value.
86    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
87        self.unknown_tagged_fields = value;
88        self
89    }
90    /// Inserts an entry into unknown_tagged_fields.
91    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
92        self.unknown_tagged_fields.insert(key, value);
93        self
94    }
95}
96
97#[cfg(feature = "broker")]
98impl Encodable for EpochEndOffset {
99    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100        if version < 0 || version > 4 {
101            bail!("specified version not supported by this message type");
102        }
103        types::Int16.encode(buf, &self.error_code)?;
104        types::Int32.encode(buf, &self.partition)?;
105        if version >= 1 {
106            types::Int32.encode(buf, &self.leader_epoch)?;
107        }
108        types::Int64.encode(buf, &self.end_offset)?;
109        if version >= 4 {
110            let num_tagged_fields = self.unknown_tagged_fields.len();
111            if num_tagged_fields > std::u32::MAX as usize {
112                bail!(
113                    "Too many tagged fields to encode ({} fields)",
114                    num_tagged_fields
115                );
116            }
117            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
118
119            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
120        }
121        Ok(())
122    }
123    fn compute_size(&self, version: i16) -> Result<usize> {
124        let mut total_size = 0;
125        total_size += types::Int16.compute_size(&self.error_code)?;
126        total_size += types::Int32.compute_size(&self.partition)?;
127        if version >= 1 {
128            total_size += types::Int32.compute_size(&self.leader_epoch)?;
129        }
130        total_size += types::Int64.compute_size(&self.end_offset)?;
131        if version >= 4 {
132            let num_tagged_fields = self.unknown_tagged_fields.len();
133            if num_tagged_fields > std::u32::MAX as usize {
134                bail!(
135                    "Too many tagged fields to encode ({} fields)",
136                    num_tagged_fields
137                );
138            }
139            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
140
141            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
142        }
143        Ok(total_size)
144    }
145}
146
147#[cfg(feature = "client")]
148impl Decodable for EpochEndOffset {
149    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
150        if version < 0 || version > 4 {
151            bail!("specified version not supported by this message type");
152        }
153        let error_code = types::Int16.decode(buf)?;
154        let partition = types::Int32.decode(buf)?;
155        let leader_epoch = if version >= 1 {
156            types::Int32.decode(buf)?
157        } else {
158            -1
159        };
160        let end_offset = types::Int64.decode(buf)?;
161        let mut unknown_tagged_fields = BTreeMap::new();
162        if version >= 4 {
163            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
164            for _ in 0..num_tagged_fields {
165                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
166                let size: u32 = types::UnsignedVarInt.decode(buf)?;
167                let unknown_value = buf.try_get_bytes(size as usize)?;
168                unknown_tagged_fields.insert(tag as i32, unknown_value);
169            }
170        }
171        Ok(Self {
172            error_code,
173            partition,
174            leader_epoch,
175            end_offset,
176            unknown_tagged_fields,
177        })
178    }
179}
180
181impl Default for EpochEndOffset {
182    fn default() -> Self {
183        Self {
184            error_code: 0,
185            partition: 0,
186            leader_epoch: -1,
187            end_offset: -1,
188            unknown_tagged_fields: BTreeMap::new(),
189        }
190    }
191}
192
193impl Message for EpochEndOffset {
194    const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
195    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
196}
197
198/// Valid versions: 0-4
199#[non_exhaustive]
200#[derive(Debug, Clone, PartialEq)]
201pub struct OffsetForLeaderEpochResponse {
202    /// 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.
203    ///
204    /// Supported API versions: 2-4
205    pub throttle_time_ms: i32,
206
207    /// Each topic we fetched offsets for.
208    ///
209    /// Supported API versions: 0-4
210    pub topics: Vec<OffsetForLeaderTopicResult>,
211
212    /// Other tagged fields
213    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
214}
215
216impl OffsetForLeaderEpochResponse {
217    /// Sets `throttle_time_ms` to the passed value.
218    ///
219    /// 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.
220    ///
221    /// Supported API versions: 2-4
222    pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
223        self.throttle_time_ms = value;
224        self
225    }
226    /// Sets `topics` to the passed value.
227    ///
228    /// Each topic we fetched offsets for.
229    ///
230    /// Supported API versions: 0-4
231    pub fn with_topics(mut self, value: Vec<OffsetForLeaderTopicResult>) -> Self {
232        self.topics = value;
233        self
234    }
235    /// Sets unknown_tagged_fields to the passed value.
236    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
237        self.unknown_tagged_fields = value;
238        self
239    }
240    /// Inserts an entry into unknown_tagged_fields.
241    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
242        self.unknown_tagged_fields.insert(key, value);
243        self
244    }
245}
246
247#[cfg(feature = "broker")]
248impl Encodable for OffsetForLeaderEpochResponse {
249    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
250        if version < 0 || version > 4 {
251            bail!("specified version not supported by this message type");
252        }
253        if version >= 2 {
254            types::Int32.encode(buf, &self.throttle_time_ms)?;
255        }
256        if version >= 4 {
257            types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
258        } else {
259            types::Array(types::Struct { version }).encode(buf, &self.topics)?;
260        }
261        if version >= 4 {
262            let num_tagged_fields = self.unknown_tagged_fields.len();
263            if num_tagged_fields > std::u32::MAX as usize {
264                bail!(
265                    "Too many tagged fields to encode ({} fields)",
266                    num_tagged_fields
267                );
268            }
269            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
270
271            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
272        }
273        Ok(())
274    }
275    fn compute_size(&self, version: i16) -> Result<usize> {
276        let mut total_size = 0;
277        if version >= 2 {
278            total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
279        }
280        if version >= 4 {
281            total_size +=
282                types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
283        } else {
284            total_size += types::Array(types::Struct { version }).compute_size(&self.topics)?;
285        }
286        if version >= 4 {
287            let num_tagged_fields = self.unknown_tagged_fields.len();
288            if num_tagged_fields > std::u32::MAX as usize {
289                bail!(
290                    "Too many tagged fields to encode ({} fields)",
291                    num_tagged_fields
292                );
293            }
294            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
295
296            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
297        }
298        Ok(total_size)
299    }
300}
301
302#[cfg(feature = "client")]
303impl Decodable for OffsetForLeaderEpochResponse {
304    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
305        if version < 0 || version > 4 {
306            bail!("specified version not supported by this message type");
307        }
308        let throttle_time_ms = if version >= 2 {
309            types::Int32.decode(buf)?
310        } else {
311            0
312        };
313        let topics = if version >= 4 {
314            types::CompactArray(types::Struct { version }).decode(buf)?
315        } else {
316            types::Array(types::Struct { version }).decode(buf)?
317        };
318        let mut unknown_tagged_fields = BTreeMap::new();
319        if version >= 4 {
320            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
321            for _ in 0..num_tagged_fields {
322                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
323                let size: u32 = types::UnsignedVarInt.decode(buf)?;
324                let unknown_value = buf.try_get_bytes(size as usize)?;
325                unknown_tagged_fields.insert(tag as i32, unknown_value);
326            }
327        }
328        Ok(Self {
329            throttle_time_ms,
330            topics,
331            unknown_tagged_fields,
332        })
333    }
334}
335
336impl Default for OffsetForLeaderEpochResponse {
337    fn default() -> Self {
338        Self {
339            throttle_time_ms: 0,
340            topics: Default::default(),
341            unknown_tagged_fields: BTreeMap::new(),
342        }
343    }
344}
345
346impl Message for OffsetForLeaderEpochResponse {
347    const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
348    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
349}
350
351/// Valid versions: 0-4
352#[non_exhaustive]
353#[derive(Debug, Clone, PartialEq)]
354pub struct OffsetForLeaderTopicResult {
355    /// The topic name.
356    ///
357    /// Supported API versions: 0-4
358    pub topic: super::TopicName,
359
360    /// Each partition in the topic we fetched offsets for.
361    ///
362    /// Supported API versions: 0-4
363    pub partitions: Vec<EpochEndOffset>,
364
365    /// Other tagged fields
366    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
367}
368
369impl OffsetForLeaderTopicResult {
370    /// Sets `topic` to the passed value.
371    ///
372    /// The topic name.
373    ///
374    /// Supported API versions: 0-4
375    pub fn with_topic(mut self, value: super::TopicName) -> Self {
376        self.topic = value;
377        self
378    }
379    /// Sets `partitions` to the passed value.
380    ///
381    /// Each partition in the topic we fetched offsets for.
382    ///
383    /// Supported API versions: 0-4
384    pub fn with_partitions(mut self, value: Vec<EpochEndOffset>) -> Self {
385        self.partitions = value;
386        self
387    }
388    /// Sets unknown_tagged_fields to the passed value.
389    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
390        self.unknown_tagged_fields = value;
391        self
392    }
393    /// Inserts an entry into unknown_tagged_fields.
394    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
395        self.unknown_tagged_fields.insert(key, value);
396        self
397    }
398}
399
400#[cfg(feature = "broker")]
401impl Encodable for OffsetForLeaderTopicResult {
402    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
403        if version < 0 || version > 4 {
404            bail!("specified version not supported by this message type");
405        }
406        if version >= 4 {
407            types::CompactString.encode(buf, &self.topic)?;
408        } else {
409            types::String.encode(buf, &self.topic)?;
410        }
411        if version >= 4 {
412            types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
413        } else {
414            types::Array(types::Struct { version }).encode(buf, &self.partitions)?;
415        }
416        if version >= 4 {
417            let num_tagged_fields = self.unknown_tagged_fields.len();
418            if num_tagged_fields > std::u32::MAX as usize {
419                bail!(
420                    "Too many tagged fields to encode ({} fields)",
421                    num_tagged_fields
422                );
423            }
424            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
425
426            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
427        }
428        Ok(())
429    }
430    fn compute_size(&self, version: i16) -> Result<usize> {
431        let mut total_size = 0;
432        if version >= 4 {
433            total_size += types::CompactString.compute_size(&self.topic)?;
434        } else {
435            total_size += types::String.compute_size(&self.topic)?;
436        }
437        if version >= 4 {
438            total_size +=
439                types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
440        } else {
441            total_size += types::Array(types::Struct { version }).compute_size(&self.partitions)?;
442        }
443        if version >= 4 {
444            let num_tagged_fields = self.unknown_tagged_fields.len();
445            if num_tagged_fields > std::u32::MAX as usize {
446                bail!(
447                    "Too many tagged fields to encode ({} fields)",
448                    num_tagged_fields
449                );
450            }
451            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
452
453            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
454        }
455        Ok(total_size)
456    }
457}
458
459#[cfg(feature = "client")]
460impl Decodable for OffsetForLeaderTopicResult {
461    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
462        if version < 0 || version > 4 {
463            bail!("specified version not supported by this message type");
464        }
465        let topic = if version >= 4 {
466            types::CompactString.decode(buf)?
467        } else {
468            types::String.decode(buf)?
469        };
470        let partitions = if version >= 4 {
471            types::CompactArray(types::Struct { version }).decode(buf)?
472        } else {
473            types::Array(types::Struct { version }).decode(buf)?
474        };
475        let mut unknown_tagged_fields = BTreeMap::new();
476        if version >= 4 {
477            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
478            for _ in 0..num_tagged_fields {
479                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
480                let size: u32 = types::UnsignedVarInt.decode(buf)?;
481                let unknown_value = buf.try_get_bytes(size as usize)?;
482                unknown_tagged_fields.insert(tag as i32, unknown_value);
483            }
484        }
485        Ok(Self {
486            topic,
487            partitions,
488            unknown_tagged_fields,
489        })
490    }
491}
492
493impl Default for OffsetForLeaderTopicResult {
494    fn default() -> Self {
495        Self {
496            topic: Default::default(),
497            partitions: Default::default(),
498            unknown_tagged_fields: BTreeMap::new(),
499        }
500    }
501}
502
503impl Message for OffsetForLeaderTopicResult {
504    const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
505    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
506}
507
508impl HeaderVersion for OffsetForLeaderEpochResponse {
509    fn header_version(version: i16) -> i16 {
510        if version >= 4 {
511            1
512        } else {
513            0
514        }
515    }
516}