odradek_protocol/messages/
consumer_protocol_subscription.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 ConsumerProtocolSubscription {
31 pub topics: Vec<String>,
33 pub user_data: Option<bytes::Bytes>,
35 pub owned_partitions: Vec<TopicPartition>,
37 pub generation_id: i32,
39 pub rack_id: Option<String>,
41 pub unknown_tagged_fields: Vec<RawTaggedField>,
43}
44
45impl Default for ConsumerProtocolSubscription {
46 fn default() -> Self {
47 Self {
48 topics: Vec::new(),
49 user_data: None,
50 owned_partitions: Vec::new(),
51 generation_id: -1,
52 rack_id: None,
53 unknown_tagged_fields: Vec::new(),
54 }
55 }
56}
57
58impl ConsumerProtocolSubscription {
59 pub const MIN_VERSION: i16 = 0;
61 pub const MAX_VERSION: i16 = 3;
63
64 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
66 if is_flexible(version) {
67 wire::put_compact_array_len(buf, Some(self.topics.len()));
68 } else {
69 wire::put_array_len(buf, Some(self.topics.len()))?;
70 }
71 for item in &self.topics {
72 if is_flexible(version) {
73 wire::put_compact_string(buf, item);
74 } else {
75 wire::put_string(buf, item)?;
76 }
77 }
78 if is_flexible(version) {
79 wire::put_compact_nullable_bytes(buf, self.user_data.as_deref());
80 } else {
81 wire::put_nullable_bytes(buf, self.user_data.as_deref())?;
82 }
83 if version >= 1 {
84 if is_flexible(version) {
85 wire::put_compact_array_len(buf, Some(self.owned_partitions.len()));
86 } else {
87 wire::put_array_len(buf, Some(self.owned_partitions.len()))?;
88 }
89 for item in &self.owned_partitions {
90 item.encode(buf, version)?;
91 }
92 }
93 if version >= 2 {
94 buf.put_i32(self.generation_id);
95 }
96 if version >= 3 {
97 if is_flexible(version) {
98 wire::put_compact_nullable_string(buf, self.rack_id.as_deref());
99 } else {
100 wire::put_nullable_string(buf, self.rack_id.as_deref())?;
101 }
102 }
103 if is_flexible(version) {
104 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
105 }
106 Ok(())
107 }
108
109 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
112 Self::decode_with_limits(buf, version, Limits::default())
113 }
114
115 pub fn decode_with_limits(
117 buf: &mut impl Buf,
118 version: i16,
119 limits: Limits,
120 ) -> Result<Self, DecodeError> {
121 let mut budget = limits.budget(buf.remaining());
122 Self::decode_with_budget(buf, version, &mut budget)
123 }
124
125 pub fn decode_with_budget(
127 buf: &mut impl Buf,
128 version: i16,
129 budget: &mut Budget,
130 ) -> Result<Self, DecodeError> {
131 let mut this = Self::default();
132 this.topics = {
133 let len = if is_flexible(version) {
134 wire::get_compact_array_len(buf)?
135 } else {
136 wire::get_array_len(buf)?
137 };
138 match len {
139 None => return Err(DecodeError::InvalidLength(-1)),
140 Some(n) => {
141 let mut items = Vec::new();
142 for _ in 0..n {
143 budget.push(
144 &mut items,
145 if is_flexible(version) {
146 wire::get_compact_string(buf)?
147 } else {
148 wire::get_string(buf)?
149 },
150 )?;
151 }
152 items
153 }
154 }
155 };
156 this.user_data = if is_flexible(version) {
157 wire::get_compact_nullable_bytes(buf)?
158 } else {
159 wire::get_nullable_bytes(buf)?
160 };
161 if version >= 1 {
162 this.owned_partitions = {
163 let len = if is_flexible(version) {
164 wire::get_compact_array_len(buf)?
165 } else {
166 wire::get_array_len(buf)?
167 };
168 match len {
169 None => return Err(DecodeError::InvalidLength(-1)),
170 Some(n) => {
171 let mut items = Vec::new();
172 for _ in 0..n {
173 let mark = buf.remaining();
174 let item = TopicPartition::decode_with_budget(buf, version, budget)?;
175 budget.progress(mark, buf.remaining())?;
176 budget.push(&mut items, item)?;
177 }
178 items
179 }
180 }
181 };
182 }
183 if version >= 2 {
184 this.generation_id = wire::get_i32(buf)?;
185 }
186 if version >= 3 {
187 this.rack_id = if is_flexible(version) {
188 wire::get_compact_nullable_string(buf)?
189 } else {
190 wire::get_nullable_string(buf)?
191 };
192 }
193 if is_flexible(version) {
194 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
195 }
196 Ok(this)
197 }
198}
199
200#[derive(Debug, Clone, PartialEq)]
202#[non_exhaustive]
203pub struct TopicPartition {
204 pub topic: String,
206 pub partitions: Vec<i32>,
208 pub unknown_tagged_fields: Vec<RawTaggedField>,
210}
211
212impl Default for TopicPartition {
213 fn default() -> Self {
214 Self {
215 topic: String::new(),
216 partitions: Vec::new(),
217 unknown_tagged_fields: Vec::new(),
218 }
219 }
220}
221
222impl TopicPartition {
223 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
225 if version >= 1 {
226 if is_flexible(version) {
227 wire::put_compact_string(buf, &self.topic);
228 } else {
229 wire::put_string(buf, &self.topic)?;
230 }
231 }
232 if version >= 1 {
233 if is_flexible(version) {
234 wire::put_compact_array_len(buf, Some(self.partitions.len()));
235 } else {
236 wire::put_array_len(buf, Some(self.partitions.len()))?;
237 }
238 for item in &self.partitions {
239 buf.put_i32(*item);
240 }
241 }
242 if is_flexible(version) {
243 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
244 }
245 Ok(())
246 }
247
248 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
251 Self::decode_with_limits(buf, version, Limits::default())
252 }
253
254 pub fn decode_with_limits(
256 buf: &mut impl Buf,
257 version: i16,
258 limits: Limits,
259 ) -> Result<Self, DecodeError> {
260 let mut budget = limits.budget(buf.remaining());
261 Self::decode_with_budget(buf, version, &mut budget)
262 }
263
264 pub fn decode_with_budget(
266 buf: &mut impl Buf,
267 version: i16,
268 budget: &mut Budget,
269 ) -> Result<Self, DecodeError> {
270 let mut this = Self::default();
271 if version >= 1 {
272 this.topic = if is_flexible(version) {
273 wire::get_compact_string(buf)?
274 } else {
275 wire::get_string(buf)?
276 };
277 }
278 if version >= 1 {
279 this.partitions = {
280 let len = if is_flexible(version) {
281 wire::get_compact_array_len(buf)?
282 } else {
283 wire::get_array_len(buf)?
284 };
285 match len {
286 None => return Err(DecodeError::InvalidLength(-1)),
287 Some(n) => {
288 let mut items = Vec::new();
289 for _ in 0..n {
290 budget.push(&mut items, wire::get_i32(buf)?)?;
291 }
292 items
293 }
294 }
295 };
296 }
297 if is_flexible(version) {
298 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
299 }
300 Ok(this)
301 }
302}