kafka_protocol/messages/
offset_commit_response.rs

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