Skip to main content

odradek_protocol/messages/
create_topics_request.rs

1//! `CreateTopicsRequest` — generated from `schemas/CreateTopicsRequest.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 >= 5
25}
26
27/// `CreateTopicsRequest`, versions 2-7.
28#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct CreateTopicsRequest {
31    /// The topics to create.
32    pub topics: Vec<CreatableTopic>,
33    /// How long to wait in milliseconds before timing out the request.
34    pub timeout_ms: i32,
35    /// If true, check that the topics can be created as specified, but don't create anything.
36    pub validate_only: bool,
37    /// Tagged fields not (yet) known to this crate, kept raw.
38    pub unknown_tagged_fields: Vec<RawTaggedField>,
39}
40
41impl Default for CreateTopicsRequest {
42    fn default() -> Self {
43        Self {
44            topics: Vec::new(),
45            timeout_ms: 60000,
46            validate_only: false,
47            unknown_tagged_fields: Vec::new(),
48        }
49    }
50}
51
52impl CreateTopicsRequest {
53    /// This message's api key in Kafka's registry.
54    pub const API_KEY: i16 = 19;
55    /// The lowest schema version this snapshot can speak.
56    pub const MIN_VERSION: i16 = 2;
57    /// The highest schema version this snapshot can speak.
58    pub const MAX_VERSION: i16 = 7;
59
60    /// Encode this `CreateTopicsRequest` at `version`.
61    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
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        buf.put_i32(self.timeout_ms);
71        buf.put_u8(u8::from(self.validate_only));
72        if is_flexible(version) {
73            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
74        }
75        Ok(())
76    }
77
78    /// Decode one `CreateTopicsRequest`, bounding allocation by the default
79    /// [`Limits`] derived from `buf`'s remaining length.
80    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
81        Self::decode_with_limits(buf, version, Limits::default())
82    }
83
84    /// Decode one `CreateTopicsRequest` under `limits`.
85    pub fn decode_with_limits(
86        buf: &mut impl Buf,
87        version: i16,
88        limits: Limits,
89    ) -> Result<Self, DecodeError> {
90        let mut budget = limits.budget(buf.remaining());
91        Self::decode_with_budget(buf, version, &mut budget)
92    }
93
94    /// Decode one `CreateTopicsRequest` against an existing `budget`.
95    pub fn decode_with_budget(
96        buf: &mut impl Buf,
97        version: i16,
98        budget: &mut Budget,
99    ) -> Result<Self, DecodeError> {
100        let mut this = Self::default();
101        this.topics = {
102            let len = if is_flexible(version) {
103                wire::get_compact_array_len(buf)?
104            } else {
105                wire::get_array_len(buf)?
106            };
107            match len {
108                None => return Err(DecodeError::InvalidLength(-1)),
109                Some(n) => {
110                    let mut items = Vec::new();
111                    for _ in 0..n {
112                        let mark = buf.remaining();
113                        let item = CreatableTopic::decode_with_budget(buf, version, budget)?;
114                        budget.progress(mark, buf.remaining())?;
115                        budget.push(&mut items, item)?;
116                    }
117                    items
118                }
119            }
120        };
121        this.timeout_ms = wire::get_i32(buf)?;
122        this.validate_only = wire::get_bool(buf)?;
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 CreateTopicsRequest {
131    const API_KEY: i16 = 19;
132    const MIN_VERSION: i16 = 2;
133    const MAX_VERSION: i16 = 7;
134
135    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
136        CreateTopicsRequest::encode(self, buf, version)
137    }
138    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
139        CreateTopicsRequest::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        CreateTopicsRequest::decode_with_limits(buf, version, limits)
147    }
148}
149
150/// `CreatableTopic` (nested in [`CreateTopicsRequest`]).
151#[derive(Debug, Clone, PartialEq)]
152#[non_exhaustive]
153pub struct CreatableTopic {
154    /// The topic name.
155    pub name: String,
156    /// The number of partitions to create in the topic, or -1 if we are either specifying a manual partition assignment or using the default partitions.
157    pub num_partitions: i32,
158    /// The number of replicas to create for each partition in the topic, or -1 if we are either specifying a manual partition assignment or using the default replication factor.
159    pub replication_factor: i16,
160    /// The manual partition assignment, or the empty array if we are using automatic assignment.
161    pub assignments: Vec<CreatableReplicaAssignment>,
162    /// The custom topic configurations to set.
163    pub configs: Vec<CreatableTopicConfig>,
164    /// Tagged fields not (yet) known to this crate, kept raw.
165    pub unknown_tagged_fields: Vec<RawTaggedField>,
166}
167
168impl Default for CreatableTopic {
169    fn default() -> Self {
170        Self {
171            name: String::new(),
172            num_partitions: 0,
173            replication_factor: 0,
174            assignments: Vec::new(),
175            configs: Vec::new(),
176            unknown_tagged_fields: Vec::new(),
177        }
178    }
179}
180
181impl CreatableTopic {
182    /// Encode this `CreatableTopic` at `version`.
183    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
184        if is_flexible(version) {
185            wire::put_compact_string(buf, &self.name);
186        } else {
187            wire::put_string(buf, &self.name)?;
188        }
189        buf.put_i32(self.num_partitions);
190        buf.put_i16(self.replication_factor);
191        if is_flexible(version) {
192            wire::put_compact_array_len(buf, Some(self.assignments.len()));
193        } else {
194            wire::put_array_len(buf, Some(self.assignments.len()))?;
195        }
196        for item in &self.assignments {
197            item.encode(buf, version)?;
198        }
199        if is_flexible(version) {
200            wire::put_compact_array_len(buf, Some(self.configs.len()));
201        } else {
202            wire::put_array_len(buf, Some(self.configs.len()))?;
203        }
204        for item in &self.configs {
205            item.encode(buf, version)?;
206        }
207        if is_flexible(version) {
208            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
209        }
210        Ok(())
211    }
212
213    /// Decode one `CreatableTopic`, bounding allocation by the default
214    /// [`Limits`] derived from `buf`'s remaining length.
215    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
216        Self::decode_with_limits(buf, version, Limits::default())
217    }
218
219    /// Decode one `CreatableTopic` under `limits`.
220    pub fn decode_with_limits(
221        buf: &mut impl Buf,
222        version: i16,
223        limits: Limits,
224    ) -> Result<Self, DecodeError> {
225        let mut budget = limits.budget(buf.remaining());
226        Self::decode_with_budget(buf, version, &mut budget)
227    }
228
229    /// Decode one `CreatableTopic` against an existing `budget`.
230    pub fn decode_with_budget(
231        buf: &mut impl Buf,
232        version: i16,
233        budget: &mut Budget,
234    ) -> Result<Self, DecodeError> {
235        let mut this = Self::default();
236        this.name = if is_flexible(version) {
237            wire::get_compact_string(buf)?
238        } else {
239            wire::get_string(buf)?
240        };
241        this.num_partitions = wire::get_i32(buf)?;
242        this.replication_factor = wire::get_i16(buf)?;
243        this.assignments = {
244            let len = if is_flexible(version) {
245                wire::get_compact_array_len(buf)?
246            } else {
247                wire::get_array_len(buf)?
248            };
249            match len {
250                None => return Err(DecodeError::InvalidLength(-1)),
251                Some(n) => {
252                    let mut items = Vec::new();
253                    for _ in 0..n {
254                        let mark = buf.remaining();
255                        let item =
256                            CreatableReplicaAssignment::decode_with_budget(buf, version, budget)?;
257                        budget.progress(mark, buf.remaining())?;
258                        budget.push(&mut items, item)?;
259                    }
260                    items
261                }
262            }
263        };
264        this.configs = {
265            let len = if is_flexible(version) {
266                wire::get_compact_array_len(buf)?
267            } else {
268                wire::get_array_len(buf)?
269            };
270            match len {
271                None => return Err(DecodeError::InvalidLength(-1)),
272                Some(n) => {
273                    let mut items = Vec::new();
274                    for _ in 0..n {
275                        let mark = buf.remaining();
276                        let item = CreatableTopicConfig::decode_with_budget(buf, version, budget)?;
277                        budget.progress(mark, buf.remaining())?;
278                        budget.push(&mut items, item)?;
279                    }
280                    items
281                }
282            }
283        };
284        if is_flexible(version) {
285            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
286        }
287        Ok(this)
288    }
289}
290
291/// `CreatableReplicaAssignment` (nested in [`CreateTopicsRequest`]).
292#[derive(Debug, Clone, PartialEq)]
293#[non_exhaustive]
294pub struct CreatableReplicaAssignment {
295    /// The partition index.
296    pub partition_index: i32,
297    /// The brokers to place the partition on.
298    pub broker_ids: Vec<i32>,
299    /// Tagged fields not (yet) known to this crate, kept raw.
300    pub unknown_tagged_fields: Vec<RawTaggedField>,
301}
302
303impl Default for CreatableReplicaAssignment {
304    fn default() -> Self {
305        Self {
306            partition_index: 0,
307            broker_ids: Vec::new(),
308            unknown_tagged_fields: Vec::new(),
309        }
310    }
311}
312
313impl CreatableReplicaAssignment {
314    /// Encode this `CreatableReplicaAssignment` at `version`.
315    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
316        buf.put_i32(self.partition_index);
317        if is_flexible(version) {
318            wire::put_compact_array_len(buf, Some(self.broker_ids.len()));
319        } else {
320            wire::put_array_len(buf, Some(self.broker_ids.len()))?;
321        }
322        for item in &self.broker_ids {
323            buf.put_i32(*item);
324        }
325        if is_flexible(version) {
326            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
327        }
328        Ok(())
329    }
330
331    /// Decode one `CreatableReplicaAssignment`, bounding allocation by the default
332    /// [`Limits`] derived from `buf`'s remaining length.
333    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
334        Self::decode_with_limits(buf, version, Limits::default())
335    }
336
337    /// Decode one `CreatableReplicaAssignment` under `limits`.
338    pub fn decode_with_limits(
339        buf: &mut impl Buf,
340        version: i16,
341        limits: Limits,
342    ) -> Result<Self, DecodeError> {
343        let mut budget = limits.budget(buf.remaining());
344        Self::decode_with_budget(buf, version, &mut budget)
345    }
346
347    /// Decode one `CreatableReplicaAssignment` against an existing `budget`.
348    pub fn decode_with_budget(
349        buf: &mut impl Buf,
350        version: i16,
351        budget: &mut Budget,
352    ) -> Result<Self, DecodeError> {
353        let mut this = Self::default();
354        this.partition_index = wire::get_i32(buf)?;
355        this.broker_ids = {
356            let len = if is_flexible(version) {
357                wire::get_compact_array_len(buf)?
358            } else {
359                wire::get_array_len(buf)?
360            };
361            match len {
362                None => return Err(DecodeError::InvalidLength(-1)),
363                Some(n) => {
364                    let mut items = Vec::new();
365                    for _ in 0..n {
366                        budget.push(&mut items, wire::get_i32(buf)?)?;
367                    }
368                    items
369                }
370            }
371        };
372        if is_flexible(version) {
373            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
374        }
375        Ok(this)
376    }
377}
378
379/// `CreatableTopicConfig` (nested in [`CreateTopicsRequest`]).
380#[derive(Debug, Clone, PartialEq)]
381#[non_exhaustive]
382pub struct CreatableTopicConfig {
383    /// The configuration name.
384    pub name: String,
385    /// The configuration value.
386    pub value: Option<String>,
387    /// Tagged fields not (yet) known to this crate, kept raw.
388    pub unknown_tagged_fields: Vec<RawTaggedField>,
389}
390
391impl Default for CreatableTopicConfig {
392    fn default() -> Self {
393        Self {
394            name: String::new(),
395            value: Some(String::new()),
396            unknown_tagged_fields: Vec::new(),
397        }
398    }
399}
400
401impl CreatableTopicConfig {
402    /// Encode this `CreatableTopicConfig` at `version`.
403    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
404        if is_flexible(version) {
405            wire::put_compact_string(buf, &self.name);
406        } else {
407            wire::put_string(buf, &self.name)?;
408        }
409        if is_flexible(version) {
410            wire::put_compact_nullable_string(buf, self.value.as_deref());
411        } else {
412            wire::put_nullable_string(buf, self.value.as_deref())?;
413        }
414        if is_flexible(version) {
415            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
416        }
417        Ok(())
418    }
419
420    /// Decode one `CreatableTopicConfig`, bounding allocation by the default
421    /// [`Limits`] derived from `buf`'s remaining length.
422    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
423        Self::decode_with_limits(buf, version, Limits::default())
424    }
425
426    /// Decode one `CreatableTopicConfig` under `limits`.
427    pub fn decode_with_limits(
428        buf: &mut impl Buf,
429        version: i16,
430        limits: Limits,
431    ) -> Result<Self, DecodeError> {
432        let mut budget = limits.budget(buf.remaining());
433        Self::decode_with_budget(buf, version, &mut budget)
434    }
435
436    /// Decode one `CreatableTopicConfig` against an existing `budget`.
437    pub fn decode_with_budget(
438        buf: &mut impl Buf,
439        version: i16,
440        budget: &mut Budget,
441    ) -> Result<Self, DecodeError> {
442        let mut this = Self::default();
443        this.name = if is_flexible(version) {
444            wire::get_compact_string(buf)?
445        } else {
446            wire::get_string(buf)?
447        };
448        this.value = if is_flexible(version) {
449            wire::get_compact_nullable_string(buf)?
450        } else {
451            wire::get_nullable_string(buf)?
452        };
453        if is_flexible(version) {
454            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
455        }
456        Ok(this)
457    }
458}