odradek_protocol/messages/
txn_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 >= 3
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct TxnOffsetCommitRequest {
31 pub transactional_id: String,
33 pub group_id: String,
35 pub producer_id: i64,
37 pub producer_epoch: i16,
39 pub generation_id: i32,
41 pub member_id: String,
43 pub group_instance_id: Option<String>,
45 pub topics: Vec<TxnOffsetCommitRequestTopic>,
47 pub unknown_tagged_fields: Vec<RawTaggedField>,
49}
50
51impl Default for TxnOffsetCommitRequest {
52 fn default() -> Self {
53 Self {
54 transactional_id: String::new(),
55 group_id: String::new(),
56 producer_id: 0,
57 producer_epoch: 0,
58 generation_id: -1,
59 member_id: "".to_string(),
60 group_instance_id: None,
61 topics: Vec::new(),
62 unknown_tagged_fields: Vec::new(),
63 }
64 }
65}
66
67impl TxnOffsetCommitRequest {
68 pub const API_KEY: i16 = 28;
70 pub const MIN_VERSION: i16 = 0;
72 pub const MAX_VERSION: i16 = 5;
74
75 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
77 if is_flexible(version) {
78 wire::put_compact_string(buf, &self.transactional_id);
79 } else {
80 wire::put_string(buf, &self.transactional_id)?;
81 }
82 if is_flexible(version) {
83 wire::put_compact_string(buf, &self.group_id);
84 } else {
85 wire::put_string(buf, &self.group_id)?;
86 }
87 buf.put_i64(self.producer_id);
88 buf.put_i16(self.producer_epoch);
89 if version >= 3 {
90 buf.put_i32(self.generation_id);
91 }
92 if version >= 3 {
93 if is_flexible(version) {
94 wire::put_compact_string(buf, &self.member_id);
95 } else {
96 wire::put_string(buf, &self.member_id)?;
97 }
98 }
99 if version >= 3 {
100 if is_flexible(version) {
101 wire::put_compact_nullable_string(buf, self.group_instance_id.as_deref());
102 } else {
103 wire::put_nullable_string(buf, self.group_instance_id.as_deref())?;
104 }
105 }
106 if is_flexible(version) {
107 wire::put_compact_array_len(buf, Some(self.topics.len()));
108 } else {
109 wire::put_array_len(buf, Some(self.topics.len()))?;
110 }
111 for item in &self.topics {
112 item.encode(buf, version)?;
113 }
114 if is_flexible(version) {
115 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
116 }
117 Ok(())
118 }
119
120 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
123 Self::decode_with_limits(buf, version, Limits::default())
124 }
125
126 pub fn decode_with_limits(
128 buf: &mut impl Buf,
129 version: i16,
130 limits: Limits,
131 ) -> Result<Self, DecodeError> {
132 let mut budget = limits.budget(buf.remaining());
133 Self::decode_with_budget(buf, version, &mut budget)
134 }
135
136 pub fn decode_with_budget(
138 buf: &mut impl Buf,
139 version: i16,
140 budget: &mut Budget,
141 ) -> Result<Self, DecodeError> {
142 let mut this = Self::default();
143 this.transactional_id = if is_flexible(version) {
144 wire::get_compact_string(buf)?
145 } else {
146 wire::get_string(buf)?
147 };
148 this.group_id = if is_flexible(version) {
149 wire::get_compact_string(buf)?
150 } else {
151 wire::get_string(buf)?
152 };
153 this.producer_id = wire::get_i64(buf)?;
154 this.producer_epoch = wire::get_i16(buf)?;
155 if version >= 3 {
156 this.generation_id = wire::get_i32(buf)?;
157 }
158 if version >= 3 {
159 this.member_id = if is_flexible(version) {
160 wire::get_compact_string(buf)?
161 } else {
162 wire::get_string(buf)?
163 };
164 }
165 if version >= 3 {
166 this.group_instance_id = if is_flexible(version) {
167 wire::get_compact_nullable_string(buf)?
168 } else {
169 wire::get_nullable_string(buf)?
170 };
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 =
185 TxnOffsetCommitRequestTopic::decode_with_budget(buf, version, budget)?;
186 budget.progress(mark, buf.remaining())?;
187 budget.push(&mut items, item)?;
188 }
189 items
190 }
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
200impl crate::Message for TxnOffsetCommitRequest {
201 const API_KEY: i16 = 28;
202 const MIN_VERSION: i16 = 0;
203 const MAX_VERSION: i16 = 5;
204
205 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
206 TxnOffsetCommitRequest::encode(self, buf, version)
207 }
208 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
209 TxnOffsetCommitRequest::decode(buf, version)
210 }
211 fn decode_with_limits(
212 buf: &mut impl Buf,
213 version: i16,
214 limits: Limits,
215 ) -> Result<Self, DecodeError> {
216 TxnOffsetCommitRequest::decode_with_limits(buf, version, limits)
217 }
218}
219
220#[derive(Debug, Clone, PartialEq)]
222#[non_exhaustive]
223pub struct TxnOffsetCommitRequestTopic {
224 pub name: String,
226 pub partitions: Vec<TxnOffsetCommitRequestPartition>,
228 pub unknown_tagged_fields: Vec<RawTaggedField>,
230}
231
232impl Default for TxnOffsetCommitRequestTopic {
233 fn default() -> Self {
234 Self {
235 name: String::new(),
236 partitions: Vec::new(),
237 unknown_tagged_fields: Vec::new(),
238 }
239 }
240}
241
242impl TxnOffsetCommitRequestTopic {
243 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
245 if is_flexible(version) {
246 wire::put_compact_string(buf, &self.name);
247 } else {
248 wire::put_string(buf, &self.name)?;
249 }
250 if is_flexible(version) {
251 wire::put_compact_array_len(buf, Some(self.partitions.len()));
252 } else {
253 wire::put_array_len(buf, Some(self.partitions.len()))?;
254 }
255 for item in &self.partitions {
256 item.encode(buf, version)?;
257 }
258 if is_flexible(version) {
259 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
260 }
261 Ok(())
262 }
263
264 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
267 Self::decode_with_limits(buf, version, Limits::default())
268 }
269
270 pub fn decode_with_limits(
272 buf: &mut impl Buf,
273 version: i16,
274 limits: Limits,
275 ) -> Result<Self, DecodeError> {
276 let mut budget = limits.budget(buf.remaining());
277 Self::decode_with_budget(buf, version, &mut budget)
278 }
279
280 pub fn decode_with_budget(
282 buf: &mut impl Buf,
283 version: i16,
284 budget: &mut Budget,
285 ) -> Result<Self, DecodeError> {
286 let mut this = Self::default();
287 this.name = if is_flexible(version) {
288 wire::get_compact_string(buf)?
289 } else {
290 wire::get_string(buf)?
291 };
292 this.partitions = {
293 let len = if is_flexible(version) {
294 wire::get_compact_array_len(buf)?
295 } else {
296 wire::get_array_len(buf)?
297 };
298 match len {
299 None => return Err(DecodeError::InvalidLength(-1)),
300 Some(n) => {
301 let mut items = Vec::new();
302 for _ in 0..n {
303 let mark = buf.remaining();
304 let item = TxnOffsetCommitRequestPartition::decode_with_budget(
305 buf, version, budget,
306 )?;
307 budget.progress(mark, buf.remaining())?;
308 budget.push(&mut items, item)?;
309 }
310 items
311 }
312 }
313 };
314 if is_flexible(version) {
315 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
316 }
317 Ok(this)
318 }
319}
320
321#[derive(Debug, Clone, PartialEq)]
323#[non_exhaustive]
324pub struct TxnOffsetCommitRequestPartition {
325 pub partition_index: i32,
327 pub committed_offset: i64,
329 pub committed_leader_epoch: i32,
331 pub committed_metadata: Option<String>,
333 pub unknown_tagged_fields: Vec<RawTaggedField>,
335}
336
337impl Default for TxnOffsetCommitRequestPartition {
338 fn default() -> Self {
339 Self {
340 partition_index: 0,
341 committed_offset: 0,
342 committed_leader_epoch: -1,
343 committed_metadata: Some(String::new()),
344 unknown_tagged_fields: Vec::new(),
345 }
346 }
347}
348
349impl TxnOffsetCommitRequestPartition {
350 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
352 buf.put_i32(self.partition_index);
353 buf.put_i64(self.committed_offset);
354 if version >= 2 {
355 buf.put_i32(self.committed_leader_epoch);
356 }
357 if is_flexible(version) {
358 wire::put_compact_nullable_string(buf, self.committed_metadata.as_deref());
359 } else {
360 wire::put_nullable_string(buf, self.committed_metadata.as_deref())?;
361 }
362 if is_flexible(version) {
363 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
364 }
365 Ok(())
366 }
367
368 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
371 Self::decode_with_limits(buf, version, Limits::default())
372 }
373
374 pub fn decode_with_limits(
376 buf: &mut impl Buf,
377 version: i16,
378 limits: Limits,
379 ) -> Result<Self, DecodeError> {
380 let mut budget = limits.budget(buf.remaining());
381 Self::decode_with_budget(buf, version, &mut budget)
382 }
383
384 pub fn decode_with_budget(
386 buf: &mut impl Buf,
387 version: i16,
388 budget: &mut Budget,
389 ) -> Result<Self, DecodeError> {
390 let mut this = Self::default();
391 this.partition_index = wire::get_i32(buf)?;
392 this.committed_offset = wire::get_i64(buf)?;
393 if version >= 2 {
394 this.committed_leader_epoch = wire::get_i32(buf)?;
395 }
396 this.committed_metadata = if is_flexible(version) {
397 wire::get_compact_nullable_string(buf)?
398 } else {
399 wire::get_nullable_string(buf)?
400 };
401 if is_flexible(version) {
402 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
403 }
404 Ok(this)
405 }
406}