odradek_protocol/messages/
consumer_protocol_assignment.rs1#![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
21pub fn is_flexible(version: i16) -> bool {
23 let _ = version;
24 false
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct ConsumerProtocolAssignment {
31 pub assigned_partitions: Vec<TopicPartition>,
33 pub user_data: Option<bytes::Bytes>,
35 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 pub const MIN_VERSION: i16 = 0;
52 pub const MAX_VERSION: i16 = 3;
54
55 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 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
79 Self::decode_with_limits(buf, version, Limits::default())
80 }
81
82 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 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#[derive(Debug, Clone, PartialEq)]
133#[non_exhaustive]
134pub struct TopicPartition {
135 pub topic: String,
137 pub partitions: Vec<i32>,
139 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 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 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
178 Self::decode_with_limits(buf, version, Limits::default())
179 }
180
181 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 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}