Skip to main content

odradek_protocol/messages/
create_topics_response.rs

1//! `CreateTopicsResponse` — generated from `schemas/CreateTopicsResponse.json`.
2//!
3//! @generated by `cargo xtask codegen`. Do not edit by hand.
4//!
5//! Tagged fields are materialized as `Option` struct fields (`None` = absent
6//! on the wire); unknown tags still round-trip through `unknown_tagged_fields`.
7
8#![allow(
9    clippy::field_reassign_with_default,
10    clippy::nonminimal_bool,
11    clippy::derivable_impls,
12    clippy::borrow_deref_ref,
13    clippy::double_parens,
14    unused_parens
15)]
16
17use bytes::{Buf, BufMut};
18
19use crate::budget::{Budget, Limits};
20#[allow(unused_imports)]
21use crate::error::{DecodeError, EncodeError};
22use crate::wire::{self, RawTaggedField};
23
24/// Whether `version` of this message uses the flexible (compact) encoding.
25pub fn is_flexible(version: i16) -> bool {
26    let _ = version;
27    version >= 5
28}
29
30/// `CreateTopicsResponse`, versions 2-7.
31#[derive(Debug, Clone, PartialEq)]
32#[non_exhaustive]
33pub struct CreateTopicsResponse {
34    /// 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.
35    pub throttle_time_ms: i32,
36    /// Results for each topic we tried to create.
37    pub topics: Vec<CreatableTopicResult>,
38    /// Tagged fields not (yet) known to this crate, kept raw.
39    pub unknown_tagged_fields: Vec<RawTaggedField>,
40}
41
42impl Default for CreateTopicsResponse {
43    fn default() -> Self {
44        Self {
45            throttle_time_ms: 0,
46            topics: Vec::new(),
47            unknown_tagged_fields: Vec::new(),
48        }
49    }
50}
51
52impl CreateTopicsResponse {
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 `CreateTopicsResponse` at `version`.
61    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
62        buf.put_i32(self.throttle_time_ms);
63        if is_flexible(version) {
64            wire::put_compact_array_len(buf, Some(self.topics.len()));
65        } else {
66            wire::put_array_len(buf, Some(self.topics.len()))?;
67        }
68        for item in &self.topics {
69            item.encode(buf, version)?;
70        }
71        if is_flexible(version) {
72            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
73        }
74        Ok(())
75    }
76
77    /// Decode one `CreateTopicsResponse`, bounding allocation by the default
78    /// [`Limits`] derived from `buf`'s remaining length.
79    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
80        Self::decode_with_limits(buf, version, Limits::default())
81    }
82
83    /// Decode one `CreateTopicsResponse` under `limits`.
84    pub fn decode_with_limits(
85        buf: &mut impl Buf,
86        version: i16,
87        limits: Limits,
88    ) -> Result<Self, DecodeError> {
89        let mut budget = limits.budget(buf.remaining());
90        Self::decode_with_budget(buf, version, &mut budget)
91    }
92
93    /// Decode one `CreateTopicsResponse` against an existing `budget`.
94    pub fn decode_with_budget(
95        buf: &mut impl Buf,
96        version: i16,
97        budget: &mut Budget,
98    ) -> Result<Self, DecodeError> {
99        let mut this = Self::default();
100        this.throttle_time_ms = wire::get_i32(buf)?;
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 = CreatableTopicResult::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        if is_flexible(version) {
122            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
123        }
124        Ok(this)
125    }
126}
127
128impl crate::Message for CreateTopicsResponse {
129    const API_KEY: i16 = 19;
130    const MIN_VERSION: i16 = 2;
131    const MAX_VERSION: i16 = 7;
132
133    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
134        CreateTopicsResponse::encode(self, buf, version)
135    }
136    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
137        CreateTopicsResponse::decode(buf, version)
138    }
139    fn decode_with_limits(
140        buf: &mut impl Buf,
141        version: i16,
142        limits: Limits,
143    ) -> Result<Self, DecodeError> {
144        CreateTopicsResponse::decode_with_limits(buf, version, limits)
145    }
146    fn throttle_time_ms(&self) -> Option<i32> {
147        Some(self.throttle_time_ms)
148    }
149}
150
151/// `CreatableTopicResult` (nested in [`CreateTopicsResponse`]).
152#[derive(Debug, Clone, PartialEq)]
153#[non_exhaustive]
154pub struct CreatableTopicResult {
155    /// The topic name.
156    pub name: String,
157    /// The unique topic ID.
158    pub topic_id: [u8; 16],
159    /// The error code, or 0 if there was no error.
160    pub error_code: i16,
161    /// The error message, or null if there was no error.
162    pub error_message: Option<String>,
163    /// Number of partitions of the topic.
164    pub num_partitions: i32,
165    /// Replication factor of the topic.
166    pub replication_factor: i16,
167    /// Configuration of the topic.
168    pub configs: Option<Vec<CreatableTopicConfigs>>,
169    /// Optional topic config error returned if configs are not returned in the response.
170    /// (Tagged field 0; `None` = absent on the wire.)
171    pub topic_config_error_code: Option<i16>,
172    /// Tagged fields not (yet) known to this crate, kept raw.
173    pub unknown_tagged_fields: Vec<RawTaggedField>,
174}
175
176impl Default for CreatableTopicResult {
177    fn default() -> Self {
178        Self {
179            name: String::new(),
180            topic_id: [0u8; 16],
181            error_code: 0,
182            error_message: Some(String::new()),
183            num_partitions: -1,
184            replication_factor: -1,
185            configs: Some(Vec::new()),
186            topic_config_error_code: None,
187            unknown_tagged_fields: Vec::new(),
188        }
189    }
190}
191
192impl CreatableTopicResult {
193    /// Encode this `CreatableTopicResult` at `version`.
194    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
195        if is_flexible(version) {
196            wire::put_compact_string(buf, &self.name);
197        } else {
198            wire::put_string(buf, &self.name)?;
199        }
200        if version >= 7 {
201            wire::put_uuid(buf, self.topic_id);
202        }
203        buf.put_i16(self.error_code);
204        if is_flexible(version) {
205            wire::put_compact_nullable_string(buf, self.error_message.as_deref());
206        } else {
207            wire::put_nullable_string(buf, self.error_message.as_deref())?;
208        }
209        if version >= 5 {
210            buf.put_i32(self.num_partitions);
211        }
212        if version >= 5 {
213            buf.put_i16(self.replication_factor);
214        }
215        if version >= 5 {
216            match &self.configs {
217                Some(items) => {
218                    if is_flexible(version) {
219                        wire::put_compact_array_len(buf, Some(items.len()));
220                    } else {
221                        wire::put_array_len(buf, Some(items.len()))?;
222                    }
223                    for item in items {
224                        item.encode(buf, version)?;
225                    }
226                }
227                None => {
228                    if is_flexible(version) {
229                        wire::put_compact_array_len(buf, None);
230                    } else {
231                        wire::put_array_len(buf, None)?;
232                    }
233                }
234            }
235        }
236        if is_flexible(version) {
237            let mut tagged = self.unknown_tagged_fields.clone();
238            if version >= 5 {
239                if let Some(v) = &self.topic_config_error_code {
240                    let mut tag_data = bytes::BytesMut::new();
241                    {
242                        let buf = &mut tag_data;
243                        buf.put_i16((*v));
244                    }
245                    tagged.push(RawTaggedField {
246                        tag: 0,
247                        data: tag_data.freeze(),
248                    });
249                }
250            }
251            tagged.sort_by_key(|f| f.tag);
252            wire::put_tagged_fields(buf, &tagged);
253        }
254        Ok(())
255    }
256
257    /// Decode one `CreatableTopicResult`, bounding allocation by the default
258    /// [`Limits`] derived from `buf`'s remaining length.
259    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
260        Self::decode_with_limits(buf, version, Limits::default())
261    }
262
263    /// Decode one `CreatableTopicResult` under `limits`.
264    pub fn decode_with_limits(
265        buf: &mut impl Buf,
266        version: i16,
267        limits: Limits,
268    ) -> Result<Self, DecodeError> {
269        let mut budget = limits.budget(buf.remaining());
270        Self::decode_with_budget(buf, version, &mut budget)
271    }
272
273    /// Decode one `CreatableTopicResult` against an existing `budget`.
274    pub fn decode_with_budget(
275        buf: &mut impl Buf,
276        version: i16,
277        budget: &mut Budget,
278    ) -> Result<Self, DecodeError> {
279        let mut this = Self::default();
280        this.name = if is_flexible(version) {
281            wire::get_compact_string(buf)?
282        } else {
283            wire::get_string(buf)?
284        };
285        if version >= 7 {
286            this.topic_id = wire::get_uuid(buf)?;
287        }
288        this.error_code = wire::get_i16(buf)?;
289        this.error_message = if is_flexible(version) {
290            wire::get_compact_nullable_string(buf)?
291        } else {
292            wire::get_nullable_string(buf)?
293        };
294        if version >= 5 {
295            this.num_partitions = wire::get_i32(buf)?;
296        }
297        if version >= 5 {
298            this.replication_factor = wire::get_i16(buf)?;
299        }
300        if version >= 5 {
301            this.configs = {
302                let len = if is_flexible(version) {
303                    wire::get_compact_array_len(buf)?
304                } else {
305                    wire::get_array_len(buf)?
306                };
307                match len {
308                    None => None,
309                    Some(n) => {
310                        let mut items = Vec::new();
311                        for _ in 0..n {
312                            let mark = buf.remaining();
313                            let item =
314                                CreatableTopicConfigs::decode_with_budget(buf, version, budget)?;
315                            budget.progress(mark, buf.remaining())?;
316                            budget.push(&mut items, item)?;
317                        }
318                        Some(items)
319                    }
320                }
321            };
322        }
323        if is_flexible(version) {
324            for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
325                if raw.tag == 0 && (version >= 5) {
326                    let mut tag_data = raw.data;
327                    let buf = &mut tag_data;
328                    this.topic_config_error_code = Some(wire::get_i16(buf)?);
329                    if buf.has_remaining() {
330                        return Err(DecodeError::InvalidLength(
331                            i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
332                        ));
333                    }
334                } else {
335                    budget.push(&mut this.unknown_tagged_fields, raw)?;
336                }
337            }
338        }
339        Ok(this)
340    }
341}
342
343/// `CreatableTopicConfigs` (nested in [`CreateTopicsResponse`]).
344#[derive(Debug, Clone, PartialEq)]
345#[non_exhaustive]
346pub struct CreatableTopicConfigs {
347    /// The configuration name.
348    pub name: String,
349    /// The configuration value.
350    pub value: Option<String>,
351    /// True if the configuration is read-only.
352    pub read_only: bool,
353    /// The configuration source.
354    pub config_source: i8,
355    /// True if this configuration is sensitive.
356    pub is_sensitive: bool,
357    /// Tagged fields not (yet) known to this crate, kept raw.
358    pub unknown_tagged_fields: Vec<RawTaggedField>,
359}
360
361impl Default for CreatableTopicConfigs {
362    fn default() -> Self {
363        Self {
364            name: String::new(),
365            value: Some(String::new()),
366            read_only: false,
367            config_source: -1,
368            is_sensitive: false,
369            unknown_tagged_fields: Vec::new(),
370        }
371    }
372}
373
374impl CreatableTopicConfigs {
375    /// Encode this `CreatableTopicConfigs` at `version`.
376    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
377        if version >= 5 {
378            if is_flexible(version) {
379                wire::put_compact_string(buf, &self.name);
380            } else {
381                wire::put_string(buf, &self.name)?;
382            }
383        }
384        if version >= 5 {
385            if is_flexible(version) {
386                wire::put_compact_nullable_string(buf, self.value.as_deref());
387            } else {
388                wire::put_nullable_string(buf, self.value.as_deref())?;
389            }
390        }
391        if version >= 5 {
392            buf.put_u8(u8::from(self.read_only));
393        }
394        if version >= 5 {
395            buf.put_i8(self.config_source);
396        }
397        if version >= 5 {
398            buf.put_u8(u8::from(self.is_sensitive));
399        }
400        if is_flexible(version) {
401            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
402        }
403        Ok(())
404    }
405
406    /// Decode one `CreatableTopicConfigs`, bounding allocation by the default
407    /// [`Limits`] derived from `buf`'s remaining length.
408    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
409        Self::decode_with_limits(buf, version, Limits::default())
410    }
411
412    /// Decode one `CreatableTopicConfigs` under `limits`.
413    pub fn decode_with_limits(
414        buf: &mut impl Buf,
415        version: i16,
416        limits: Limits,
417    ) -> Result<Self, DecodeError> {
418        let mut budget = limits.budget(buf.remaining());
419        Self::decode_with_budget(buf, version, &mut budget)
420    }
421
422    /// Decode one `CreatableTopicConfigs` against an existing `budget`.
423    pub fn decode_with_budget(
424        buf: &mut impl Buf,
425        version: i16,
426        budget: &mut Budget,
427    ) -> Result<Self, DecodeError> {
428        let mut this = Self::default();
429        if version >= 5 {
430            this.name = if is_flexible(version) {
431                wire::get_compact_string(buf)?
432            } else {
433                wire::get_string(buf)?
434            };
435        }
436        if version >= 5 {
437            this.value = if is_flexible(version) {
438                wire::get_compact_nullable_string(buf)?
439            } else {
440                wire::get_nullable_string(buf)?
441            };
442        }
443        if version >= 5 {
444            this.read_only = wire::get_bool(buf)?;
445        }
446        if version >= 5 {
447            this.config_source = wire::get_i8(buf)?;
448        }
449        if version >= 5 {
450            this.is_sensitive = wire::get_bool(buf)?;
451        }
452        if is_flexible(version) {
453            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
454        }
455        Ok(this)
456    }
457}