kafka_protocol/messages/
alter_partition_response.rs

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