kafka_protocol/messages/
create_partitions_request.rs

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