odradek_protocol/messages/
offset_commit_request.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 version >= 8
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct OffsetCommitRequest {
31 pub group_id: String,
33 pub generation_id_or_member_epoch: i32,
35 pub member_id: String,
37 pub group_instance_id: Option<String>,
39 pub retention_time_ms: i64,
41 pub topics: Vec<OffsetCommitRequestTopic>,
43 pub unknown_tagged_fields: Vec<RawTaggedField>,
45}
46
47impl Default for OffsetCommitRequest {
48 fn default() -> Self {
49 Self {
50 group_id: String::new(),
51 generation_id_or_member_epoch: -1,
52 member_id: String::new(),
53 group_instance_id: None,
54 retention_time_ms: -1,
55 topics: Vec::new(),
56 unknown_tagged_fields: Vec::new(),
57 }
58 }
59}
60
61impl OffsetCommitRequest {
62 pub const API_KEY: i16 = 8;
64 pub const MIN_VERSION: i16 = 2;
66 pub const MAX_VERSION: i16 = 10;
68
69 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
71 if is_flexible(version) {
72 wire::put_compact_string(buf, &self.group_id);
73 } else {
74 wire::put_string(buf, &self.group_id)?;
75 }
76 buf.put_i32(self.generation_id_or_member_epoch);
77 if is_flexible(version) {
78 wire::put_compact_string(buf, &self.member_id);
79 } else {
80 wire::put_string(buf, &self.member_id)?;
81 }
82 if version >= 7 {
83 if is_flexible(version) {
84 wire::put_compact_nullable_string(buf, self.group_instance_id.as_deref());
85 } else {
86 wire::put_nullable_string(buf, self.group_instance_id.as_deref())?;
87 }
88 }
89 if version <= 4 {
90 buf.put_i64(self.retention_time_ms);
91 }
92 if is_flexible(version) {
93 wire::put_compact_array_len(buf, Some(self.topics.len()));
94 } else {
95 wire::put_array_len(buf, Some(self.topics.len()))?;
96 }
97 for item in &self.topics {
98 item.encode(buf, version)?;
99 }
100 if is_flexible(version) {
101 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
102 }
103 Ok(())
104 }
105
106 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
109 Self::decode_with_limits(buf, version, Limits::default())
110 }
111
112 pub fn decode_with_limits(
114 buf: &mut impl Buf,
115 version: i16,
116 limits: Limits,
117 ) -> Result<Self, DecodeError> {
118 let mut budget = limits.budget(buf.remaining());
119 Self::decode_with_budget(buf, version, &mut budget)
120 }
121
122 pub fn decode_with_budget(
124 buf: &mut impl Buf,
125 version: i16,
126 budget: &mut Budget,
127 ) -> Result<Self, DecodeError> {
128 let mut this = Self::default();
129 this.group_id = if is_flexible(version) {
130 wire::get_compact_string(buf)?
131 } else {
132 wire::get_string(buf)?
133 };
134 this.generation_id_or_member_epoch = wire::get_i32(buf)?;
135 this.member_id = if is_flexible(version) {
136 wire::get_compact_string(buf)?
137 } else {
138 wire::get_string(buf)?
139 };
140 if version >= 7 {
141 this.group_instance_id = if is_flexible(version) {
142 wire::get_compact_nullable_string(buf)?
143 } else {
144 wire::get_nullable_string(buf)?
145 };
146 }
147 if version <= 4 {
148 this.retention_time_ms = wire::get_i64(buf)?;
149 }
150 this.topics = {
151 let len = if is_flexible(version) {
152 wire::get_compact_array_len(buf)?
153 } else {
154 wire::get_array_len(buf)?
155 };
156 match len {
157 None => return Err(DecodeError::InvalidLength(-1)),
158 Some(n) => {
159 let mut items = Vec::new();
160 for _ in 0..n {
161 let mark = buf.remaining();
162 let item =
163 OffsetCommitRequestTopic::decode_with_budget(buf, version, budget)?;
164 budget.progress(mark, buf.remaining())?;
165 budget.push(&mut items, item)?;
166 }
167 items
168 }
169 }
170 };
171 if is_flexible(version) {
172 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
173 }
174 Ok(this)
175 }
176}
177
178impl crate::Message for OffsetCommitRequest {
179 const API_KEY: i16 = 8;
180 const MIN_VERSION: i16 = 2;
181 const MAX_VERSION: i16 = 10;
182
183 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
184 OffsetCommitRequest::encode(self, buf, version)
185 }
186 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
187 OffsetCommitRequest::decode(buf, version)
188 }
189 fn decode_with_limits(
190 buf: &mut impl Buf,
191 version: i16,
192 limits: Limits,
193 ) -> Result<Self, DecodeError> {
194 OffsetCommitRequest::decode_with_limits(buf, version, limits)
195 }
196}
197
198#[derive(Debug, Clone, PartialEq)]
200#[non_exhaustive]
201pub struct OffsetCommitRequestTopic {
202 pub name: String,
204 pub topic_id: [u8; 16],
206 pub partitions: Vec<OffsetCommitRequestPartition>,
208 pub unknown_tagged_fields: Vec<RawTaggedField>,
210}
211
212impl Default for OffsetCommitRequestTopic {
213 fn default() -> Self {
214 Self {
215 name: String::new(),
216 topic_id: [0u8; 16],
217 partitions: Vec::new(),
218 unknown_tagged_fields: Vec::new(),
219 }
220 }
221}
222
223impl OffsetCommitRequestTopic {
224 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
226 if version <= 9 {
227 if is_flexible(version) {
228 wire::put_compact_string(buf, &self.name);
229 } else {
230 wire::put_string(buf, &self.name)?;
231 }
232 }
233 if version >= 10 {
234 wire::put_uuid(buf, self.topic_id);
235 }
236 if is_flexible(version) {
237 wire::put_compact_array_len(buf, Some(self.partitions.len()));
238 } else {
239 wire::put_array_len(buf, Some(self.partitions.len()))?;
240 }
241 for item in &self.partitions {
242 item.encode(buf, version)?;
243 }
244 if is_flexible(version) {
245 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
246 }
247 Ok(())
248 }
249
250 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
253 Self::decode_with_limits(buf, version, Limits::default())
254 }
255
256 pub fn decode_with_limits(
258 buf: &mut impl Buf,
259 version: i16,
260 limits: Limits,
261 ) -> Result<Self, DecodeError> {
262 let mut budget = limits.budget(buf.remaining());
263 Self::decode_with_budget(buf, version, &mut budget)
264 }
265
266 pub fn decode_with_budget(
268 buf: &mut impl Buf,
269 version: i16,
270 budget: &mut Budget,
271 ) -> Result<Self, DecodeError> {
272 let mut this = Self::default();
273 if version <= 9 {
274 this.name = if is_flexible(version) {
275 wire::get_compact_string(buf)?
276 } else {
277 wire::get_string(buf)?
278 };
279 }
280 if version >= 10 {
281 this.topic_id = wire::get_uuid(buf)?;
282 }
283 this.partitions = {
284 let len = if is_flexible(version) {
285 wire::get_compact_array_len(buf)?
286 } else {
287 wire::get_array_len(buf)?
288 };
289 match len {
290 None => return Err(DecodeError::InvalidLength(-1)),
291 Some(n) => {
292 let mut items = Vec::new();
293 for _ in 0..n {
294 let mark = buf.remaining();
295 let item =
296 OffsetCommitRequestPartition::decode_with_budget(buf, version, budget)?;
297 budget.progress(mark, buf.remaining())?;
298 budget.push(&mut items, item)?;
299 }
300 items
301 }
302 }
303 };
304 if is_flexible(version) {
305 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
306 }
307 Ok(this)
308 }
309}
310
311#[derive(Debug, Clone, PartialEq)]
313#[non_exhaustive]
314pub struct OffsetCommitRequestPartition {
315 pub partition_index: i32,
317 pub committed_offset: i64,
319 pub committed_leader_epoch: i32,
321 pub committed_metadata: Option<String>,
323 pub unknown_tagged_fields: Vec<RawTaggedField>,
325}
326
327impl Default for OffsetCommitRequestPartition {
328 fn default() -> Self {
329 Self {
330 partition_index: 0,
331 committed_offset: 0,
332 committed_leader_epoch: -1,
333 committed_metadata: Some(String::new()),
334 unknown_tagged_fields: Vec::new(),
335 }
336 }
337}
338
339impl OffsetCommitRequestPartition {
340 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
342 buf.put_i32(self.partition_index);
343 buf.put_i64(self.committed_offset);
344 if version >= 6 {
345 buf.put_i32(self.committed_leader_epoch);
346 }
347 if is_flexible(version) {
348 wire::put_compact_nullable_string(buf, self.committed_metadata.as_deref());
349 } else {
350 wire::put_nullable_string(buf, self.committed_metadata.as_deref())?;
351 }
352 if is_flexible(version) {
353 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
354 }
355 Ok(())
356 }
357
358 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
361 Self::decode_with_limits(buf, version, Limits::default())
362 }
363
364 pub fn decode_with_limits(
366 buf: &mut impl Buf,
367 version: i16,
368 limits: Limits,
369 ) -> Result<Self, DecodeError> {
370 let mut budget = limits.budget(buf.remaining());
371 Self::decode_with_budget(buf, version, &mut budget)
372 }
373
374 pub fn decode_with_budget(
376 buf: &mut impl Buf,
377 version: i16,
378 budget: &mut Budget,
379 ) -> Result<Self, DecodeError> {
380 let mut this = Self::default();
381 this.partition_index = wire::get_i32(buf)?;
382 this.committed_offset = wire::get_i64(buf)?;
383 if version >= 6 {
384 this.committed_leader_epoch = wire::get_i32(buf)?;
385 }
386 this.committed_metadata = if is_flexible(version) {
387 wire::get_compact_nullable_string(buf)?
388 } else {
389 wire::get_nullable_string(buf)?
390 };
391 if is_flexible(version) {
392 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
393 }
394 Ok(this)
395 }
396}