Skip to main content

odradek_protocol/messages/
consumer_protocol_assignment.rs

1//! `ConsumerProtocolAssignment` — generated from `schemas/ConsumerProtocolAssignment.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    false
25}
26
27/// `ConsumerProtocolAssignment`, versions 0-3.
28#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct ConsumerProtocolAssignment {
31    /// The list of topics and partitions assigned to this consumer.
32    pub assigned_partitions: Vec<TopicPartition>,
33    /// User data.
34    pub user_data: Option<bytes::Bytes>,
35    /// Tagged fields not (yet) known to this crate, kept raw.
36    pub unknown_tagged_fields: Vec<RawTaggedField>,
37}
38
39impl Default for ConsumerProtocolAssignment {
40    fn default() -> Self {
41        Self {
42            assigned_partitions: Vec::new(),
43            user_data: None,
44            unknown_tagged_fields: Vec::new(),
45        }
46    }
47}
48
49impl ConsumerProtocolAssignment {
50    /// The lowest schema version this snapshot can speak.
51    pub const MIN_VERSION: i16 = 0;
52    /// The highest schema version this snapshot can speak.
53    pub const MAX_VERSION: i16 = 3;
54
55    /// Encode this `ConsumerProtocolAssignment` at `version`.
56    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
57        if is_flexible(version) {
58            wire::put_compact_array_len(buf, Some(self.assigned_partitions.len()));
59        } else {
60            wire::put_array_len(buf, Some(self.assigned_partitions.len()))?;
61        }
62        for item in &self.assigned_partitions {
63            item.encode(buf, version)?;
64        }
65        if is_flexible(version) {
66            wire::put_compact_nullable_bytes(buf, self.user_data.as_deref());
67        } else {
68            wire::put_nullable_bytes(buf, self.user_data.as_deref())?;
69        }
70        if is_flexible(version) {
71            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
72        }
73        Ok(())
74    }
75
76    /// Decode one `ConsumerProtocolAssignment`, bounding allocation by the default
77    /// [`Limits`] derived from `buf`'s remaining length.
78    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
79        Self::decode_with_limits(buf, version, Limits::default())
80    }
81
82    /// Decode one `ConsumerProtocolAssignment` under `limits`.
83    pub fn decode_with_limits(
84        buf: &mut impl Buf,
85        version: i16,
86        limits: Limits,
87    ) -> Result<Self, DecodeError> {
88        let mut budget = limits.budget(buf.remaining());
89        Self::decode_with_budget(buf, version, &mut budget)
90    }
91
92    /// Decode one `ConsumerProtocolAssignment` against an existing `budget`.
93    pub fn decode_with_budget(
94        buf: &mut impl Buf,
95        version: i16,
96        budget: &mut Budget,
97    ) -> Result<Self, DecodeError> {
98        let mut this = Self::default();
99        this.assigned_partitions = {
100            let len = if is_flexible(version) {
101                wire::get_compact_array_len(buf)?
102            } else {
103                wire::get_array_len(buf)?
104            };
105            match len {
106                None => return Err(DecodeError::InvalidLength(-1)),
107                Some(n) => {
108                    let mut items = Vec::new();
109                    for _ in 0..n {
110                        let mark = buf.remaining();
111                        let item = TopicPartition::decode_with_budget(buf, version, budget)?;
112                        budget.progress(mark, buf.remaining())?;
113                        budget.push(&mut items, item)?;
114                    }
115                    items
116                }
117            }
118        };
119        this.user_data = if is_flexible(version) {
120            wire::get_compact_nullable_bytes(buf)?
121        } else {
122            wire::get_nullable_bytes(buf)?
123        };
124        if is_flexible(version) {
125            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
126        }
127        Ok(this)
128    }
129}
130
131/// `TopicPartition` (nested in [`ConsumerProtocolAssignment`]).
132#[derive(Debug, Clone, PartialEq)]
133#[non_exhaustive]
134pub struct TopicPartition {
135    /// The topic name.
136    pub topic: String,
137    /// The list of partitions assigned to this consumer.
138    pub partitions: Vec<i32>,
139    /// Tagged fields not (yet) known to this crate, kept raw.
140    pub unknown_tagged_fields: Vec<RawTaggedField>,
141}
142
143impl Default for TopicPartition {
144    fn default() -> Self {
145        Self {
146            topic: String::new(),
147            partitions: Vec::new(),
148            unknown_tagged_fields: Vec::new(),
149        }
150    }
151}
152
153impl TopicPartition {
154    /// Encode this `TopicPartition` at `version`.
155    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
156        if is_flexible(version) {
157            wire::put_compact_string(buf, &self.topic);
158        } else {
159            wire::put_string(buf, &self.topic)?;
160        }
161        if is_flexible(version) {
162            wire::put_compact_array_len(buf, Some(self.partitions.len()));
163        } else {
164            wire::put_array_len(buf, Some(self.partitions.len()))?;
165        }
166        for item in &self.partitions {
167            buf.put_i32(*item);
168        }
169        if is_flexible(version) {
170            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
171        }
172        Ok(())
173    }
174
175    /// Decode one `TopicPartition`, bounding allocation by the default
176    /// [`Limits`] derived from `buf`'s remaining length.
177    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
178        Self::decode_with_limits(buf, version, Limits::default())
179    }
180
181    /// Decode one `TopicPartition` under `limits`.
182    pub fn decode_with_limits(
183        buf: &mut impl Buf,
184        version: i16,
185        limits: Limits,
186    ) -> Result<Self, DecodeError> {
187        let mut budget = limits.budget(buf.remaining());
188        Self::decode_with_budget(buf, version, &mut budget)
189    }
190
191    /// Decode one `TopicPartition` against an existing `budget`.
192    pub fn decode_with_budget(
193        buf: &mut impl Buf,
194        version: i16,
195        budget: &mut Budget,
196    ) -> Result<Self, DecodeError> {
197        let mut this = Self::default();
198        this.topic = if is_flexible(version) {
199            wire::get_compact_string(buf)?
200        } else {
201            wire::get_string(buf)?
202        };
203        this.partitions = {
204            let len = if is_flexible(version) {
205                wire::get_compact_array_len(buf)?
206            } else {
207                wire::get_array_len(buf)?
208            };
209            match len {
210                None => return Err(DecodeError::InvalidLength(-1)),
211                Some(n) => {
212                    let mut items = Vec::new();
213                    for _ in 0..n {
214                        budget.push(&mut items, wire::get_i32(buf)?)?;
215                    }
216                    items
217                }
218            }
219        };
220        if is_flexible(version) {
221            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
222        }
223        Ok(this)
224    }
225}