Skip to main content

odradek_protocol/messages/
metadata_response.rs

1//! `MetadataResponse` — generated from `schemas/MetadataResponse.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 >= 9
25}
26
27/// `MetadataResponse`, versions 0-13.
28#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct MetadataResponse {
31    /// 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.
32    pub throttle_time_ms: i32,
33    /// A list of brokers present in the cluster.
34    pub brokers: Vec<MetadataResponseBroker>,
35    /// The cluster ID that responding broker belongs to.
36    pub cluster_id: Option<String>,
37    /// The ID of the controller broker.
38    pub controller_id: i32,
39    /// Each topic in the response.
40    pub topics: Vec<MetadataResponseTopic>,
41    /// 32-bit bitfield to represent authorized operations for this cluster.
42    pub cluster_authorized_operations: i32,
43    /// The top-level error code, or 0 if there was no error.
44    pub error_code: i16,
45    /// Tagged fields not (yet) known to this crate, kept raw.
46    pub unknown_tagged_fields: Vec<RawTaggedField>,
47}
48
49impl Default for MetadataResponse {
50    fn default() -> Self {
51        Self {
52            throttle_time_ms: 0,
53            brokers: Vec::new(),
54            cluster_id: None,
55            controller_id: -1,
56            topics: Vec::new(),
57            cluster_authorized_operations: -2147483648,
58            error_code: 0,
59            unknown_tagged_fields: Vec::new(),
60        }
61    }
62}
63
64impl MetadataResponse {
65    /// This message's api key in Kafka's registry.
66    pub const API_KEY: i16 = 3;
67    /// The lowest schema version this snapshot can speak.
68    pub const MIN_VERSION: i16 = 0;
69    /// The highest schema version this snapshot can speak.
70    pub const MAX_VERSION: i16 = 13;
71
72    /// Encode this `MetadataResponse` at `version`.
73    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
74        if version >= 3 {
75            buf.put_i32(self.throttle_time_ms);
76        }
77        if is_flexible(version) {
78            wire::put_compact_array_len(buf, Some(self.brokers.len()));
79        } else {
80            wire::put_array_len(buf, Some(self.brokers.len()))?;
81        }
82        for item in &self.brokers {
83            item.encode(buf, version)?;
84        }
85        if version >= 2 {
86            if is_flexible(version) {
87                wire::put_compact_nullable_string(buf, self.cluster_id.as_deref());
88            } else {
89                wire::put_nullable_string(buf, self.cluster_id.as_deref())?;
90            }
91        }
92        if version >= 1 {
93            buf.put_i32(self.controller_id);
94        }
95        if is_flexible(version) {
96            wire::put_compact_array_len(buf, Some(self.topics.len()));
97        } else {
98            wire::put_array_len(buf, Some(self.topics.len()))?;
99        }
100        for item in &self.topics {
101            item.encode(buf, version)?;
102        }
103        if (8..=10).contains(&version) {
104            buf.put_i32(self.cluster_authorized_operations);
105        }
106        if version >= 13 {
107            buf.put_i16(self.error_code);
108        }
109        if is_flexible(version) {
110            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
111        }
112        Ok(())
113    }
114
115    /// Decode one `MetadataResponse`, bounding allocation by the default
116    /// [`Limits`] derived from `buf`'s remaining length.
117    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
118        Self::decode_with_limits(buf, version, Limits::default())
119    }
120
121    /// Decode one `MetadataResponse` under `limits`.
122    pub fn decode_with_limits(
123        buf: &mut impl Buf,
124        version: i16,
125        limits: Limits,
126    ) -> Result<Self, DecodeError> {
127        let mut budget = limits.budget(buf.remaining());
128        Self::decode_with_budget(buf, version, &mut budget)
129    }
130
131    /// Decode one `MetadataResponse` against an existing `budget`.
132    pub fn decode_with_budget(
133        buf: &mut impl Buf,
134        version: i16,
135        budget: &mut Budget,
136    ) -> Result<Self, DecodeError> {
137        let mut this = Self::default();
138        if version >= 3 {
139            this.throttle_time_ms = wire::get_i32(buf)?;
140        }
141        this.brokers = {
142            let len = if is_flexible(version) {
143                wire::get_compact_array_len(buf)?
144            } else {
145                wire::get_array_len(buf)?
146            };
147            match len {
148                None => return Err(DecodeError::InvalidLength(-1)),
149                Some(n) => {
150                    let mut items = Vec::new();
151                    for _ in 0..n {
152                        let mark = buf.remaining();
153                        let item =
154                            MetadataResponseBroker::decode_with_budget(buf, version, budget)?;
155                        budget.progress(mark, buf.remaining())?;
156                        budget.push(&mut items, item)?;
157                    }
158                    items
159                }
160            }
161        };
162        if version >= 2 {
163            this.cluster_id = if is_flexible(version) {
164                wire::get_compact_nullable_string(buf)?
165            } else {
166                wire::get_nullable_string(buf)?
167            };
168        }
169        if version >= 1 {
170            this.controller_id = wire::get_i32(buf)?;
171        }
172        this.topics = {
173            let len = if is_flexible(version) {
174                wire::get_compact_array_len(buf)?
175            } else {
176                wire::get_array_len(buf)?
177            };
178            match len {
179                None => return Err(DecodeError::InvalidLength(-1)),
180                Some(n) => {
181                    let mut items = Vec::new();
182                    for _ in 0..n {
183                        let mark = buf.remaining();
184                        let item = MetadataResponseTopic::decode_with_budget(buf, version, budget)?;
185                        budget.progress(mark, buf.remaining())?;
186                        budget.push(&mut items, item)?;
187                    }
188                    items
189                }
190            }
191        };
192        if (8..=10).contains(&version) {
193            this.cluster_authorized_operations = wire::get_i32(buf)?;
194        }
195        if version >= 13 {
196            this.error_code = wire::get_i16(buf)?;
197        }
198        if is_flexible(version) {
199            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
200        }
201        Ok(this)
202    }
203}
204
205impl crate::Message for MetadataResponse {
206    const API_KEY: i16 = 3;
207    const MIN_VERSION: i16 = 0;
208    const MAX_VERSION: i16 = 13;
209
210    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
211        MetadataResponse::encode(self, buf, version)
212    }
213    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
214        MetadataResponse::decode(buf, version)
215    }
216    fn decode_with_limits(
217        buf: &mut impl Buf,
218        version: i16,
219        limits: Limits,
220    ) -> Result<Self, DecodeError> {
221        MetadataResponse::decode_with_limits(buf, version, limits)
222    }
223    fn throttle_time_ms(&self) -> Option<i32> {
224        Some(self.throttle_time_ms)
225    }
226}
227
228/// `MetadataResponseBroker` (nested in [`MetadataResponse`]).
229#[derive(Debug, Clone, PartialEq)]
230#[non_exhaustive]
231pub struct MetadataResponseBroker {
232    /// The broker ID.
233    pub node_id: i32,
234    /// The broker hostname.
235    pub host: String,
236    /// The broker port.
237    pub port: i32,
238    /// The rack of the broker, or null if it has not been assigned to a rack.
239    pub rack: Option<String>,
240    /// Tagged fields not (yet) known to this crate, kept raw.
241    pub unknown_tagged_fields: Vec<RawTaggedField>,
242}
243
244impl Default for MetadataResponseBroker {
245    fn default() -> Self {
246        Self {
247            node_id: 0,
248            host: String::new(),
249            port: 0,
250            rack: None,
251            unknown_tagged_fields: Vec::new(),
252        }
253    }
254}
255
256impl MetadataResponseBroker {
257    /// Encode this `MetadataResponseBroker` at `version`.
258    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
259        buf.put_i32(self.node_id);
260        if is_flexible(version) {
261            wire::put_compact_string(buf, &self.host);
262        } else {
263            wire::put_string(buf, &self.host)?;
264        }
265        buf.put_i32(self.port);
266        if version >= 1 {
267            if is_flexible(version) {
268                wire::put_compact_nullable_string(buf, self.rack.as_deref());
269            } else {
270                wire::put_nullable_string(buf, self.rack.as_deref())?;
271            }
272        }
273        if is_flexible(version) {
274            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
275        }
276        Ok(())
277    }
278
279    /// Decode one `MetadataResponseBroker`, bounding allocation by the default
280    /// [`Limits`] derived from `buf`'s remaining length.
281    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
282        Self::decode_with_limits(buf, version, Limits::default())
283    }
284
285    /// Decode one `MetadataResponseBroker` under `limits`.
286    pub fn decode_with_limits(
287        buf: &mut impl Buf,
288        version: i16,
289        limits: Limits,
290    ) -> Result<Self, DecodeError> {
291        let mut budget = limits.budget(buf.remaining());
292        Self::decode_with_budget(buf, version, &mut budget)
293    }
294
295    /// Decode one `MetadataResponseBroker` against an existing `budget`.
296    pub fn decode_with_budget(
297        buf: &mut impl Buf,
298        version: i16,
299        budget: &mut Budget,
300    ) -> Result<Self, DecodeError> {
301        let mut this = Self::default();
302        this.node_id = wire::get_i32(buf)?;
303        this.host = if is_flexible(version) {
304            wire::get_compact_string(buf)?
305        } else {
306            wire::get_string(buf)?
307        };
308        this.port = wire::get_i32(buf)?;
309        if version >= 1 {
310            this.rack = if is_flexible(version) {
311                wire::get_compact_nullable_string(buf)?
312            } else {
313                wire::get_nullable_string(buf)?
314            };
315        }
316        if is_flexible(version) {
317            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
318        }
319        Ok(this)
320    }
321}
322
323/// `MetadataResponseTopic` (nested in [`MetadataResponse`]).
324#[derive(Debug, Clone, PartialEq)]
325#[non_exhaustive]
326pub struct MetadataResponseTopic {
327    /// The topic error, or 0 if there was no error.
328    pub error_code: i16,
329    /// The topic name. Null for non-existing topics queried by ID. This is never null when ErrorCode is zero. One of Name and TopicId is always populated.
330    pub name: Option<String>,
331    /// The topic id. Zero for non-existing topics queried by name. This is never zero when ErrorCode is zero. One of Name and TopicId is always populated.
332    pub topic_id: [u8; 16],
333    /// True if the topic is internal.
334    pub is_internal: bool,
335    /// Each partition in the topic.
336    pub partitions: Vec<MetadataResponsePartition>,
337    /// 32-bit bitfield to represent authorized operations for this topic.
338    pub topic_authorized_operations: i32,
339    /// Tagged fields not (yet) known to this crate, kept raw.
340    pub unknown_tagged_fields: Vec<RawTaggedField>,
341}
342
343impl Default for MetadataResponseTopic {
344    fn default() -> Self {
345        Self {
346            error_code: 0,
347            name: Some(String::new()),
348            topic_id: [0u8; 16],
349            is_internal: false,
350            partitions: Vec::new(),
351            topic_authorized_operations: -2147483648,
352            unknown_tagged_fields: Vec::new(),
353        }
354    }
355}
356
357impl MetadataResponseTopic {
358    /// Encode this `MetadataResponseTopic` at `version`.
359    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
360        buf.put_i16(self.error_code);
361        if self.name.is_none() && !(version >= 12) {
362            return Err(EncodeError::NullField("Name"));
363        }
364        if is_flexible(version) {
365            wire::put_compact_nullable_string(buf, self.name.as_deref());
366        } else {
367            wire::put_nullable_string(buf, self.name.as_deref())?;
368        }
369        if version >= 10 {
370            wire::put_uuid(buf, self.topic_id);
371        }
372        if version >= 1 {
373            buf.put_u8(u8::from(self.is_internal));
374        }
375        if is_flexible(version) {
376            wire::put_compact_array_len(buf, Some(self.partitions.len()));
377        } else {
378            wire::put_array_len(buf, Some(self.partitions.len()))?;
379        }
380        for item in &self.partitions {
381            item.encode(buf, version)?;
382        }
383        if version >= 8 {
384            buf.put_i32(self.topic_authorized_operations);
385        }
386        if is_flexible(version) {
387            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
388        }
389        Ok(())
390    }
391
392    /// Decode one `MetadataResponseTopic`, bounding allocation by the default
393    /// [`Limits`] derived from `buf`'s remaining length.
394    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
395        Self::decode_with_limits(buf, version, Limits::default())
396    }
397
398    /// Decode one `MetadataResponseTopic` under `limits`.
399    pub fn decode_with_limits(
400        buf: &mut impl Buf,
401        version: i16,
402        limits: Limits,
403    ) -> Result<Self, DecodeError> {
404        let mut budget = limits.budget(buf.remaining());
405        Self::decode_with_budget(buf, version, &mut budget)
406    }
407
408    /// Decode one `MetadataResponseTopic` against an existing `budget`.
409    pub fn decode_with_budget(
410        buf: &mut impl Buf,
411        version: i16,
412        budget: &mut Budget,
413    ) -> Result<Self, DecodeError> {
414        let mut this = Self::default();
415        this.error_code = wire::get_i16(buf)?;
416        this.name = {
417            let raw = if is_flexible(version) {
418                wire::get_compact_nullable_string(buf)?
419            } else {
420                wire::get_nullable_string(buf)?
421            };
422            if raw.is_none() && !(version >= 12) {
423                return Err(DecodeError::InvalidLength(-1));
424            }
425            raw
426        };
427        if version >= 10 {
428            this.topic_id = wire::get_uuid(buf)?;
429        }
430        if version >= 1 {
431            this.is_internal = wire::get_bool(buf)?;
432        }
433        this.partitions = {
434            let len = if is_flexible(version) {
435                wire::get_compact_array_len(buf)?
436            } else {
437                wire::get_array_len(buf)?
438            };
439            match len {
440                None => return Err(DecodeError::InvalidLength(-1)),
441                Some(n) => {
442                    let mut items = Vec::new();
443                    for _ in 0..n {
444                        let mark = buf.remaining();
445                        let item =
446                            MetadataResponsePartition::decode_with_budget(buf, version, budget)?;
447                        budget.progress(mark, buf.remaining())?;
448                        budget.push(&mut items, item)?;
449                    }
450                    items
451                }
452            }
453        };
454        if version >= 8 {
455            this.topic_authorized_operations = wire::get_i32(buf)?;
456        }
457        if is_flexible(version) {
458            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
459        }
460        Ok(this)
461    }
462}
463
464/// `MetadataResponsePartition` (nested in [`MetadataResponse`]).
465#[derive(Debug, Clone, PartialEq)]
466#[non_exhaustive]
467pub struct MetadataResponsePartition {
468    /// The partition error, or 0 if there was no error.
469    pub error_code: i16,
470    /// The partition index.
471    pub partition_index: i32,
472    /// The ID of the leader broker.
473    pub leader_id: i32,
474    /// The leader epoch of this partition.
475    pub leader_epoch: i32,
476    /// The set of all nodes that host this partition.
477    pub replica_nodes: Vec<i32>,
478    /// The set of nodes that are in sync with the leader for this partition.
479    pub isr_nodes: Vec<i32>,
480    /// The set of offline replicas of this partition.
481    pub offline_replicas: Vec<i32>,
482    /// Tagged fields not (yet) known to this crate, kept raw.
483    pub unknown_tagged_fields: Vec<RawTaggedField>,
484}
485
486impl Default for MetadataResponsePartition {
487    fn default() -> Self {
488        Self {
489            error_code: 0,
490            partition_index: 0,
491            leader_id: 0,
492            leader_epoch: -1,
493            replica_nodes: Vec::new(),
494            isr_nodes: Vec::new(),
495            offline_replicas: Vec::new(),
496            unknown_tagged_fields: Vec::new(),
497        }
498    }
499}
500
501impl MetadataResponsePartition {
502    /// Encode this `MetadataResponsePartition` at `version`.
503    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
504        buf.put_i16(self.error_code);
505        buf.put_i32(self.partition_index);
506        buf.put_i32(self.leader_id);
507        if version >= 7 {
508            buf.put_i32(self.leader_epoch);
509        }
510        if is_flexible(version) {
511            wire::put_compact_array_len(buf, Some(self.replica_nodes.len()));
512        } else {
513            wire::put_array_len(buf, Some(self.replica_nodes.len()))?;
514        }
515        for item in &self.replica_nodes {
516            buf.put_i32(*item);
517        }
518        if is_flexible(version) {
519            wire::put_compact_array_len(buf, Some(self.isr_nodes.len()));
520        } else {
521            wire::put_array_len(buf, Some(self.isr_nodes.len()))?;
522        }
523        for item in &self.isr_nodes {
524            buf.put_i32(*item);
525        }
526        if version >= 5 {
527            if is_flexible(version) {
528                wire::put_compact_array_len(buf, Some(self.offline_replicas.len()));
529            } else {
530                wire::put_array_len(buf, Some(self.offline_replicas.len()))?;
531            }
532            for item in &self.offline_replicas {
533                buf.put_i32(*item);
534            }
535        }
536        if is_flexible(version) {
537            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
538        }
539        Ok(())
540    }
541
542    /// Decode one `MetadataResponsePartition`, bounding allocation by the default
543    /// [`Limits`] derived from `buf`'s remaining length.
544    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
545        Self::decode_with_limits(buf, version, Limits::default())
546    }
547
548    /// Decode one `MetadataResponsePartition` under `limits`.
549    pub fn decode_with_limits(
550        buf: &mut impl Buf,
551        version: i16,
552        limits: Limits,
553    ) -> Result<Self, DecodeError> {
554        let mut budget = limits.budget(buf.remaining());
555        Self::decode_with_budget(buf, version, &mut budget)
556    }
557
558    /// Decode one `MetadataResponsePartition` against an existing `budget`.
559    pub fn decode_with_budget(
560        buf: &mut impl Buf,
561        version: i16,
562        budget: &mut Budget,
563    ) -> Result<Self, DecodeError> {
564        let mut this = Self::default();
565        this.error_code = wire::get_i16(buf)?;
566        this.partition_index = wire::get_i32(buf)?;
567        this.leader_id = wire::get_i32(buf)?;
568        if version >= 7 {
569            this.leader_epoch = wire::get_i32(buf)?;
570        }
571        this.replica_nodes = {
572            let len = if is_flexible(version) {
573                wire::get_compact_array_len(buf)?
574            } else {
575                wire::get_array_len(buf)?
576            };
577            match len {
578                None => return Err(DecodeError::InvalidLength(-1)),
579                Some(n) => {
580                    let mut items = Vec::new();
581                    for _ in 0..n {
582                        budget.push(&mut items, wire::get_i32(buf)?)?;
583                    }
584                    items
585                }
586            }
587        };
588        this.isr_nodes = {
589            let len = if is_flexible(version) {
590                wire::get_compact_array_len(buf)?
591            } else {
592                wire::get_array_len(buf)?
593            };
594            match len {
595                None => return Err(DecodeError::InvalidLength(-1)),
596                Some(n) => {
597                    let mut items = Vec::new();
598                    for _ in 0..n {
599                        budget.push(&mut items, wire::get_i32(buf)?)?;
600                    }
601                    items
602                }
603            }
604        };
605        if version >= 5 {
606            this.offline_replicas = {
607                let len = if is_flexible(version) {
608                    wire::get_compact_array_len(buf)?
609                } else {
610                    wire::get_array_len(buf)?
611                };
612                match len {
613                    None => return Err(DecodeError::InvalidLength(-1)),
614                    Some(n) => {
615                        let mut items = Vec::new();
616                        for _ in 0..n {
617                            budget.push(&mut items, wire::get_i32(buf)?)?;
618                        }
619                        items
620                    }
621                }
622            };
623        }
624        if is_flexible(version) {
625            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
626        }
627        Ok(this)
628    }
629}