1#![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 >= 6
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct OffsetFetchResponse {
31 pub throttle_time_ms: i32,
33 pub topics: Vec<OffsetFetchResponseTopic>,
35 pub error_code: i16,
37 pub groups: Vec<OffsetFetchResponseGroup>,
39 pub unknown_tagged_fields: Vec<RawTaggedField>,
41}
42
43impl Default for OffsetFetchResponse {
44 fn default() -> Self {
45 Self {
46 throttle_time_ms: 0,
47 topics: Vec::new(),
48 error_code: 0,
49 groups: Vec::new(),
50 unknown_tagged_fields: Vec::new(),
51 }
52 }
53}
54
55impl OffsetFetchResponse {
56 pub const API_KEY: i16 = 9;
58 pub const MIN_VERSION: i16 = 1;
60 pub const MAX_VERSION: i16 = 10;
62
63 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
65 if version >= 3 {
66 buf.put_i32(self.throttle_time_ms);
67 }
68 if version <= 7 {
69 if is_flexible(version) {
70 wire::put_compact_array_len(buf, Some(self.topics.len()));
71 } else {
72 wire::put_array_len(buf, Some(self.topics.len()))?;
73 }
74 for item in &self.topics {
75 item.encode(buf, version)?;
76 }
77 }
78 if (2..=7).contains(&version) {
79 buf.put_i16(self.error_code);
80 }
81 if version >= 8 {
82 if is_flexible(version) {
83 wire::put_compact_array_len(buf, Some(self.groups.len()));
84 } else {
85 wire::put_array_len(buf, Some(self.groups.len()))?;
86 }
87 for item in &self.groups {
88 item.encode(buf, version)?;
89 }
90 }
91 if is_flexible(version) {
92 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
93 }
94 Ok(())
95 }
96
97 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
100 Self::decode_with_limits(buf, version, Limits::default())
101 }
102
103 pub fn decode_with_limits(
105 buf: &mut impl Buf,
106 version: i16,
107 limits: Limits,
108 ) -> Result<Self, DecodeError> {
109 let mut budget = limits.budget(buf.remaining());
110 Self::decode_with_budget(buf, version, &mut budget)
111 }
112
113 pub fn decode_with_budget(
115 buf: &mut impl Buf,
116 version: i16,
117 budget: &mut Budget,
118 ) -> Result<Self, DecodeError> {
119 let mut this = Self::default();
120 if version >= 3 {
121 this.throttle_time_ms = wire::get_i32(buf)?;
122 }
123 if version <= 7 {
124 this.topics = {
125 let len = if is_flexible(version) {
126 wire::get_compact_array_len(buf)?
127 } else {
128 wire::get_array_len(buf)?
129 };
130 match len {
131 None => return Err(DecodeError::InvalidLength(-1)),
132 Some(n) => {
133 let mut items = Vec::new();
134 for _ in 0..n {
135 let mark = buf.remaining();
136 let item =
137 OffsetFetchResponseTopic::decode_with_budget(buf, version, budget)?;
138 budget.progress(mark, buf.remaining())?;
139 budget.push(&mut items, item)?;
140 }
141 items
142 }
143 }
144 };
145 }
146 if (2..=7).contains(&version) {
147 this.error_code = wire::get_i16(buf)?;
148 }
149 if version >= 8 {
150 this.groups = {
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 OffsetFetchResponseGroup::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 }
172 if is_flexible(version) {
173 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
174 }
175 Ok(this)
176 }
177}
178
179impl crate::Message for OffsetFetchResponse {
180 const API_KEY: i16 = 9;
181 const MIN_VERSION: i16 = 1;
182 const MAX_VERSION: i16 = 10;
183
184 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
185 OffsetFetchResponse::encode(self, buf, version)
186 }
187 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
188 OffsetFetchResponse::decode(buf, version)
189 }
190 fn decode_with_limits(
191 buf: &mut impl Buf,
192 version: i16,
193 limits: Limits,
194 ) -> Result<Self, DecodeError> {
195 OffsetFetchResponse::decode_with_limits(buf, version, limits)
196 }
197 fn throttle_time_ms(&self) -> Option<i32> {
198 Some(self.throttle_time_ms)
199 }
200}
201
202#[derive(Debug, Clone, PartialEq)]
204#[non_exhaustive]
205pub struct OffsetFetchResponseTopic {
206 pub name: String,
208 pub partitions: Vec<OffsetFetchResponsePartition>,
210 pub unknown_tagged_fields: Vec<RawTaggedField>,
212}
213
214impl Default for OffsetFetchResponseTopic {
215 fn default() -> Self {
216 Self {
217 name: String::new(),
218 partitions: Vec::new(),
219 unknown_tagged_fields: Vec::new(),
220 }
221 }
222}
223
224impl OffsetFetchResponseTopic {
225 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
227 if version <= 7 {
228 if is_flexible(version) {
229 wire::put_compact_string(buf, &self.name);
230 } else {
231 wire::put_string(buf, &self.name)?;
232 }
233 }
234 if version <= 7 {
235 if is_flexible(version) {
236 wire::put_compact_array_len(buf, Some(self.partitions.len()));
237 } else {
238 wire::put_array_len(buf, Some(self.partitions.len()))?;
239 }
240 for item in &self.partitions {
241 item.encode(buf, version)?;
242 }
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 <= 7 {
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 <= 7 {
281 this.partitions = {
282 let len = if is_flexible(version) {
283 wire::get_compact_array_len(buf)?
284 } else {
285 wire::get_array_len(buf)?
286 };
287 match len {
288 None => return Err(DecodeError::InvalidLength(-1)),
289 Some(n) => {
290 let mut items = Vec::new();
291 for _ in 0..n {
292 let mark = buf.remaining();
293 let item = OffsetFetchResponsePartition::decode_with_budget(
294 buf, version, budget,
295 )?;
296 budget.progress(mark, buf.remaining())?;
297 budget.push(&mut items, item)?;
298 }
299 items
300 }
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 OffsetFetchResponsePartition {
315 pub partition_index: i32,
317 pub committed_offset: i64,
319 pub committed_leader_epoch: i32,
321 pub metadata: Option<String>,
323 pub error_code: i16,
325 pub unknown_tagged_fields: Vec<RawTaggedField>,
327}
328
329impl Default for OffsetFetchResponsePartition {
330 fn default() -> Self {
331 Self {
332 partition_index: 0,
333 committed_offset: 0,
334 committed_leader_epoch: -1,
335 metadata: Some(String::new()),
336 error_code: 0,
337 unknown_tagged_fields: Vec::new(),
338 }
339 }
340}
341
342impl OffsetFetchResponsePartition {
343 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
345 if version <= 7 {
346 buf.put_i32(self.partition_index);
347 }
348 if version <= 7 {
349 buf.put_i64(self.committed_offset);
350 }
351 if (5..=7).contains(&version) {
352 buf.put_i32(self.committed_leader_epoch);
353 }
354 if version <= 7 {
355 if is_flexible(version) {
356 wire::put_compact_nullable_string(buf, self.metadata.as_deref());
357 } else {
358 wire::put_nullable_string(buf, self.metadata.as_deref())?;
359 }
360 }
361 if version <= 7 {
362 buf.put_i16(self.error_code);
363 }
364 if is_flexible(version) {
365 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
366 }
367 Ok(())
368 }
369
370 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
373 Self::decode_with_limits(buf, version, Limits::default())
374 }
375
376 pub fn decode_with_limits(
378 buf: &mut impl Buf,
379 version: i16,
380 limits: Limits,
381 ) -> Result<Self, DecodeError> {
382 let mut budget = limits.budget(buf.remaining());
383 Self::decode_with_budget(buf, version, &mut budget)
384 }
385
386 pub fn decode_with_budget(
388 buf: &mut impl Buf,
389 version: i16,
390 budget: &mut Budget,
391 ) -> Result<Self, DecodeError> {
392 let mut this = Self::default();
393 if version <= 7 {
394 this.partition_index = wire::get_i32(buf)?;
395 }
396 if version <= 7 {
397 this.committed_offset = wire::get_i64(buf)?;
398 }
399 if (5..=7).contains(&version) {
400 this.committed_leader_epoch = wire::get_i32(buf)?;
401 }
402 if version <= 7 {
403 this.metadata = if is_flexible(version) {
404 wire::get_compact_nullable_string(buf)?
405 } else {
406 wire::get_nullable_string(buf)?
407 };
408 }
409 if version <= 7 {
410 this.error_code = wire::get_i16(buf)?;
411 }
412 if is_flexible(version) {
413 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
414 }
415 Ok(this)
416 }
417}
418
419#[derive(Debug, Clone, PartialEq)]
421#[non_exhaustive]
422pub struct OffsetFetchResponseGroup {
423 pub group_id: String,
425 pub topics: Vec<OffsetFetchResponseTopics>,
427 pub error_code: i16,
429 pub unknown_tagged_fields: Vec<RawTaggedField>,
431}
432
433impl Default for OffsetFetchResponseGroup {
434 fn default() -> Self {
435 Self {
436 group_id: String::new(),
437 topics: Vec::new(),
438 error_code: 0,
439 unknown_tagged_fields: Vec::new(),
440 }
441 }
442}
443
444impl OffsetFetchResponseGroup {
445 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
447 if version >= 8 {
448 if is_flexible(version) {
449 wire::put_compact_string(buf, &self.group_id);
450 } else {
451 wire::put_string(buf, &self.group_id)?;
452 }
453 }
454 if version >= 8 {
455 if is_flexible(version) {
456 wire::put_compact_array_len(buf, Some(self.topics.len()));
457 } else {
458 wire::put_array_len(buf, Some(self.topics.len()))?;
459 }
460 for item in &self.topics {
461 item.encode(buf, version)?;
462 }
463 }
464 if version >= 8 {
465 buf.put_i16(self.error_code);
466 }
467 if is_flexible(version) {
468 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
469 }
470 Ok(())
471 }
472
473 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
476 Self::decode_with_limits(buf, version, Limits::default())
477 }
478
479 pub fn decode_with_limits(
481 buf: &mut impl Buf,
482 version: i16,
483 limits: Limits,
484 ) -> Result<Self, DecodeError> {
485 let mut budget = limits.budget(buf.remaining());
486 Self::decode_with_budget(buf, version, &mut budget)
487 }
488
489 pub fn decode_with_budget(
491 buf: &mut impl Buf,
492 version: i16,
493 budget: &mut Budget,
494 ) -> Result<Self, DecodeError> {
495 let mut this = Self::default();
496 if version >= 8 {
497 this.group_id = if is_flexible(version) {
498 wire::get_compact_string(buf)?
499 } else {
500 wire::get_string(buf)?
501 };
502 }
503 if version >= 8 {
504 this.topics = {
505 let len = if is_flexible(version) {
506 wire::get_compact_array_len(buf)?
507 } else {
508 wire::get_array_len(buf)?
509 };
510 match len {
511 None => return Err(DecodeError::InvalidLength(-1)),
512 Some(n) => {
513 let mut items = Vec::new();
514 for _ in 0..n {
515 let mark = buf.remaining();
516 let item = OffsetFetchResponseTopics::decode_with_budget(
517 buf, version, budget,
518 )?;
519 budget.progress(mark, buf.remaining())?;
520 budget.push(&mut items, item)?;
521 }
522 items
523 }
524 }
525 };
526 }
527 if version >= 8 {
528 this.error_code = wire::get_i16(buf)?;
529 }
530 if is_flexible(version) {
531 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
532 }
533 Ok(this)
534 }
535}
536
537#[derive(Debug, Clone, PartialEq)]
539#[non_exhaustive]
540pub struct OffsetFetchResponseTopics {
541 pub name: String,
543 pub topic_id: [u8; 16],
545 pub partitions: Vec<OffsetFetchResponsePartitions>,
547 pub unknown_tagged_fields: Vec<RawTaggedField>,
549}
550
551impl Default for OffsetFetchResponseTopics {
552 fn default() -> Self {
553 Self {
554 name: String::new(),
555 topic_id: [0u8; 16],
556 partitions: Vec::new(),
557 unknown_tagged_fields: Vec::new(),
558 }
559 }
560}
561
562impl OffsetFetchResponseTopics {
563 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
565 if (8..=9).contains(&version) {
566 if is_flexible(version) {
567 wire::put_compact_string(buf, &self.name);
568 } else {
569 wire::put_string(buf, &self.name)?;
570 }
571 }
572 if version >= 10 {
573 wire::put_uuid(buf, self.topic_id);
574 }
575 if version >= 8 {
576 if is_flexible(version) {
577 wire::put_compact_array_len(buf, Some(self.partitions.len()));
578 } else {
579 wire::put_array_len(buf, Some(self.partitions.len()))?;
580 }
581 for item in &self.partitions {
582 item.encode(buf, version)?;
583 }
584 }
585 if is_flexible(version) {
586 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
587 }
588 Ok(())
589 }
590
591 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
594 Self::decode_with_limits(buf, version, Limits::default())
595 }
596
597 pub fn decode_with_limits(
599 buf: &mut impl Buf,
600 version: i16,
601 limits: Limits,
602 ) -> Result<Self, DecodeError> {
603 let mut budget = limits.budget(buf.remaining());
604 Self::decode_with_budget(buf, version, &mut budget)
605 }
606
607 pub fn decode_with_budget(
609 buf: &mut impl Buf,
610 version: i16,
611 budget: &mut Budget,
612 ) -> Result<Self, DecodeError> {
613 let mut this = Self::default();
614 if (8..=9).contains(&version) {
615 this.name = if is_flexible(version) {
616 wire::get_compact_string(buf)?
617 } else {
618 wire::get_string(buf)?
619 };
620 }
621 if version >= 10 {
622 this.topic_id = wire::get_uuid(buf)?;
623 }
624 if version >= 8 {
625 this.partitions = {
626 let len = if is_flexible(version) {
627 wire::get_compact_array_len(buf)?
628 } else {
629 wire::get_array_len(buf)?
630 };
631 match len {
632 None => return Err(DecodeError::InvalidLength(-1)),
633 Some(n) => {
634 let mut items = Vec::new();
635 for _ in 0..n {
636 let mark = buf.remaining();
637 let item = OffsetFetchResponsePartitions::decode_with_budget(
638 buf, version, budget,
639 )?;
640 budget.progress(mark, buf.remaining())?;
641 budget.push(&mut items, item)?;
642 }
643 items
644 }
645 }
646 };
647 }
648 if is_flexible(version) {
649 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
650 }
651 Ok(this)
652 }
653}
654
655#[derive(Debug, Clone, PartialEq)]
657#[non_exhaustive]
658pub struct OffsetFetchResponsePartitions {
659 pub partition_index: i32,
661 pub committed_offset: i64,
663 pub committed_leader_epoch: i32,
665 pub metadata: Option<String>,
667 pub error_code: i16,
669 pub unknown_tagged_fields: Vec<RawTaggedField>,
671}
672
673impl Default for OffsetFetchResponsePartitions {
674 fn default() -> Self {
675 Self {
676 partition_index: 0,
677 committed_offset: 0,
678 committed_leader_epoch: -1,
679 metadata: Some(String::new()),
680 error_code: 0,
681 unknown_tagged_fields: Vec::new(),
682 }
683 }
684}
685
686impl OffsetFetchResponsePartitions {
687 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
689 if version >= 8 {
690 buf.put_i32(self.partition_index);
691 }
692 if version >= 8 {
693 buf.put_i64(self.committed_offset);
694 }
695 if version >= 8 {
696 buf.put_i32(self.committed_leader_epoch);
697 }
698 if version >= 8 {
699 if is_flexible(version) {
700 wire::put_compact_nullable_string(buf, self.metadata.as_deref());
701 } else {
702 wire::put_nullable_string(buf, self.metadata.as_deref())?;
703 }
704 }
705 if version >= 8 {
706 buf.put_i16(self.error_code);
707 }
708 if is_flexible(version) {
709 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
710 }
711 Ok(())
712 }
713
714 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
717 Self::decode_with_limits(buf, version, Limits::default())
718 }
719
720 pub fn decode_with_limits(
722 buf: &mut impl Buf,
723 version: i16,
724 limits: Limits,
725 ) -> Result<Self, DecodeError> {
726 let mut budget = limits.budget(buf.remaining());
727 Self::decode_with_budget(buf, version, &mut budget)
728 }
729
730 pub fn decode_with_budget(
732 buf: &mut impl Buf,
733 version: i16,
734 budget: &mut Budget,
735 ) -> Result<Self, DecodeError> {
736 let mut this = Self::default();
737 if version >= 8 {
738 this.partition_index = wire::get_i32(buf)?;
739 }
740 if version >= 8 {
741 this.committed_offset = wire::get_i64(buf)?;
742 }
743 if version >= 8 {
744 this.committed_leader_epoch = wire::get_i32(buf)?;
745 }
746 if version >= 8 {
747 this.metadata = if is_flexible(version) {
748 wire::get_compact_nullable_string(buf)?
749 } else {
750 wire::get_nullable_string(buf)?
751 };
752 }
753 if version >= 8 {
754 this.error_code = wire::get_i16(buf)?;
755 }
756 if is_flexible(version) {
757 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
758 }
759 Ok(this)
760 }
761}