1#![allow(
9 clippy::field_reassign_with_default,
10 clippy::nonminimal_bool,
11 clippy::derivable_impls,
12 clippy::borrow_deref_ref,
13 clippy::double_parens,
14 unused_parens
15)]
16
17use bytes::{Buf, BufMut};
18
19use crate::budget::{Budget, Limits};
20#[allow(unused_imports)]
21use crate::error::{DecodeError, EncodeError};
22use crate::wire::{self, RawTaggedField};
23
24pub fn is_flexible(version: i16) -> bool {
26 let _ = version;
27 version >= 12
28}
29
30#[derive(Debug, Clone, PartialEq)]
32#[non_exhaustive]
33pub struct FetchRequest {
34 pub replica_id: i32,
36 pub max_wait_ms: i32,
38 pub min_bytes: i32,
40 pub max_bytes: i32,
42 pub isolation_level: i8,
44 pub session_id: i32,
46 pub session_epoch: i32,
48 pub topics: Vec<FetchTopic>,
50 pub forgotten_topics_data: Vec<ForgottenTopic>,
52 pub rack_id: String,
54 pub cluster_id: Option<Option<String>>,
57 pub replica_state: Option<ReplicaState>,
60 pub unknown_tagged_fields: Vec<RawTaggedField>,
62}
63
64impl Default for FetchRequest {
65 fn default() -> Self {
66 Self {
67 replica_id: -1,
68 max_wait_ms: 0,
69 min_bytes: 0,
70 max_bytes: 2147483647,
71 isolation_level: 0,
72 session_id: 0,
73 session_epoch: -1,
74 topics: Vec::new(),
75 forgotten_topics_data: Vec::new(),
76 rack_id: "".to_string(),
77 cluster_id: None,
78 replica_state: None,
79 unknown_tagged_fields: Vec::new(),
80 }
81 }
82}
83
84impl FetchRequest {
85 pub const API_KEY: i16 = 1;
87 pub const MIN_VERSION: i16 = 4;
89 pub const MAX_VERSION: i16 = 18;
91
92 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
94 if version <= 14 {
95 buf.put_i32(self.replica_id);
96 }
97 buf.put_i32(self.max_wait_ms);
98 buf.put_i32(self.min_bytes);
99 buf.put_i32(self.max_bytes);
100 buf.put_i8(self.isolation_level);
101 if version >= 7 {
102 buf.put_i32(self.session_id);
103 }
104 if version >= 7 {
105 buf.put_i32(self.session_epoch);
106 }
107 if is_flexible(version) {
108 wire::put_compact_array_len(buf, Some(self.topics.len()));
109 } else {
110 wire::put_array_len(buf, Some(self.topics.len()))?;
111 }
112 for item in &self.topics {
113 item.encode(buf, version)?;
114 }
115 if version >= 7 {
116 if is_flexible(version) {
117 wire::put_compact_array_len(buf, Some(self.forgotten_topics_data.len()));
118 } else {
119 wire::put_array_len(buf, Some(self.forgotten_topics_data.len()))?;
120 }
121 for item in &self.forgotten_topics_data {
122 item.encode(buf, version)?;
123 }
124 }
125 if version >= 11 {
126 if is_flexible(version) {
127 wire::put_compact_string(buf, &self.rack_id);
128 } else {
129 wire::put_string(buf, &self.rack_id)?;
130 }
131 }
132 if is_flexible(version) {
133 let mut tagged = self.unknown_tagged_fields.clone();
134 if version >= 12 {
135 if let Some(v) = &self.cluster_id {
136 let mut tag_data = bytes::BytesMut::new();
137 {
138 let buf = &mut tag_data;
139 if is_flexible(version) {
140 wire::put_compact_nullable_string(buf, (*v).as_deref());
141 } else {
142 wire::put_nullable_string(buf, (*v).as_deref())?;
143 }
144 }
145 tagged.push(RawTaggedField {
146 tag: 0,
147 data: tag_data.freeze(),
148 });
149 }
150 }
151 if version >= 15 {
152 if let Some(v) = &self.replica_state {
153 let mut tag_data = bytes::BytesMut::new();
154 {
155 let buf = &mut tag_data;
156 (*v).encode(buf, version)?;
157 }
158 tagged.push(RawTaggedField {
159 tag: 1,
160 data: tag_data.freeze(),
161 });
162 }
163 }
164 tagged.sort_by_key(|f| f.tag);
165 wire::put_tagged_fields(buf, &tagged);
166 }
167 Ok(())
168 }
169
170 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
173 Self::decode_with_limits(buf, version, Limits::default())
174 }
175
176 pub fn decode_with_limits(
178 buf: &mut impl Buf,
179 version: i16,
180 limits: Limits,
181 ) -> Result<Self, DecodeError> {
182 let mut budget = limits.budget(buf.remaining());
183 Self::decode_with_budget(buf, version, &mut budget)
184 }
185
186 pub fn decode_with_budget(
188 buf: &mut impl Buf,
189 version: i16,
190 budget: &mut Budget,
191 ) -> Result<Self, DecodeError> {
192 let mut this = Self::default();
193 if version <= 14 {
194 this.replica_id = wire::get_i32(buf)?;
195 }
196 this.max_wait_ms = wire::get_i32(buf)?;
197 this.min_bytes = wire::get_i32(buf)?;
198 this.max_bytes = wire::get_i32(buf)?;
199 this.isolation_level = wire::get_i8(buf)?;
200 if version >= 7 {
201 this.session_id = wire::get_i32(buf)?;
202 }
203 if version >= 7 {
204 this.session_epoch = wire::get_i32(buf)?;
205 }
206 this.topics = {
207 let len = if is_flexible(version) {
208 wire::get_compact_array_len(buf)?
209 } else {
210 wire::get_array_len(buf)?
211 };
212 match len {
213 None => return Err(DecodeError::InvalidLength(-1)),
214 Some(n) => {
215 let mut items = Vec::new();
216 for _ in 0..n {
217 let mark = buf.remaining();
218 let item = FetchTopic::decode_with_budget(buf, version, budget)?;
219 budget.progress(mark, buf.remaining())?;
220 budget.push(&mut items, item)?;
221 }
222 items
223 }
224 }
225 };
226 if version >= 7 {
227 this.forgotten_topics_data = {
228 let len = if is_flexible(version) {
229 wire::get_compact_array_len(buf)?
230 } else {
231 wire::get_array_len(buf)?
232 };
233 match len {
234 None => return Err(DecodeError::InvalidLength(-1)),
235 Some(n) => {
236 let mut items = Vec::new();
237 for _ in 0..n {
238 let mark = buf.remaining();
239 let item = ForgottenTopic::decode_with_budget(buf, version, budget)?;
240 budget.progress(mark, buf.remaining())?;
241 budget.push(&mut items, item)?;
242 }
243 items
244 }
245 }
246 };
247 }
248 if version >= 11 {
249 this.rack_id = if is_flexible(version) {
250 wire::get_compact_string(buf)?
251 } else {
252 wire::get_string(buf)?
253 };
254 }
255 if is_flexible(version) {
256 for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
257 if raw.tag == 0 && (version >= 12) {
258 let mut tag_data = raw.data;
259 let buf = &mut tag_data;
260 this.cluster_id = Some(if is_flexible(version) {
261 wire::get_compact_nullable_string(buf)?
262 } else {
263 wire::get_nullable_string(buf)?
264 });
265 if buf.has_remaining() {
266 return Err(DecodeError::InvalidLength(
267 i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
268 ));
269 }
270 } else if raw.tag == 1 && (version >= 15) {
271 let mut tag_data = raw.data;
272 let buf = &mut tag_data;
273 this.replica_state =
274 Some(ReplicaState::decode_with_budget(buf, version, budget)?);
275 if buf.has_remaining() {
276 return Err(DecodeError::InvalidLength(
277 i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
278 ));
279 }
280 } else {
281 budget.push(&mut this.unknown_tagged_fields, raw)?;
282 }
283 }
284 }
285 Ok(this)
286 }
287}
288
289impl crate::Message for FetchRequest {
290 const API_KEY: i16 = 1;
291 const MIN_VERSION: i16 = 4;
292 const MAX_VERSION: i16 = 18;
293
294 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
295 FetchRequest::encode(self, buf, version)
296 }
297 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
298 FetchRequest::decode(buf, version)
299 }
300 fn decode_with_limits(
301 buf: &mut impl Buf,
302 version: i16,
303 limits: Limits,
304 ) -> Result<Self, DecodeError> {
305 FetchRequest::decode_with_limits(buf, version, limits)
306 }
307}
308
309#[derive(Debug, Clone, PartialEq)]
311#[non_exhaustive]
312pub struct ReplicaState {
313 pub replica_id: i32,
315 pub replica_epoch: i64,
317 pub unknown_tagged_fields: Vec<RawTaggedField>,
319}
320
321impl Default for ReplicaState {
322 fn default() -> Self {
323 Self {
324 replica_id: -1,
325 replica_epoch: -1,
326 unknown_tagged_fields: Vec::new(),
327 }
328 }
329}
330
331impl ReplicaState {
332 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
334 if version >= 15 {
335 buf.put_i32(self.replica_id);
336 }
337 if version >= 15 {
338 buf.put_i64(self.replica_epoch);
339 }
340 if is_flexible(version) {
341 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
342 }
343 Ok(())
344 }
345
346 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
349 Self::decode_with_limits(buf, version, Limits::default())
350 }
351
352 pub fn decode_with_limits(
354 buf: &mut impl Buf,
355 version: i16,
356 limits: Limits,
357 ) -> Result<Self, DecodeError> {
358 let mut budget = limits.budget(buf.remaining());
359 Self::decode_with_budget(buf, version, &mut budget)
360 }
361
362 pub fn decode_with_budget(
364 buf: &mut impl Buf,
365 version: i16,
366 budget: &mut Budget,
367 ) -> Result<Self, DecodeError> {
368 let mut this = Self::default();
369 if version >= 15 {
370 this.replica_id = wire::get_i32(buf)?;
371 }
372 if version >= 15 {
373 this.replica_epoch = wire::get_i64(buf)?;
374 }
375 if is_flexible(version) {
376 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
377 }
378 Ok(this)
379 }
380}
381
382#[derive(Debug, Clone, PartialEq)]
384#[non_exhaustive]
385pub struct FetchTopic {
386 pub topic: String,
388 pub topic_id: [u8; 16],
390 pub partitions: Vec<FetchPartition>,
392 pub unknown_tagged_fields: Vec<RawTaggedField>,
394}
395
396impl Default for FetchTopic {
397 fn default() -> Self {
398 Self {
399 topic: String::new(),
400 topic_id: [0u8; 16],
401 partitions: Vec::new(),
402 unknown_tagged_fields: Vec::new(),
403 }
404 }
405}
406
407impl FetchTopic {
408 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
410 if version <= 12 {
411 if is_flexible(version) {
412 wire::put_compact_string(buf, &self.topic);
413 } else {
414 wire::put_string(buf, &self.topic)?;
415 }
416 }
417 if version >= 13 {
418 wire::put_uuid(buf, self.topic_id);
419 }
420 if is_flexible(version) {
421 wire::put_compact_array_len(buf, Some(self.partitions.len()));
422 } else {
423 wire::put_array_len(buf, Some(self.partitions.len()))?;
424 }
425 for item in &self.partitions {
426 item.encode(buf, version)?;
427 }
428 if is_flexible(version) {
429 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
430 }
431 Ok(())
432 }
433
434 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
437 Self::decode_with_limits(buf, version, Limits::default())
438 }
439
440 pub fn decode_with_limits(
442 buf: &mut impl Buf,
443 version: i16,
444 limits: Limits,
445 ) -> Result<Self, DecodeError> {
446 let mut budget = limits.budget(buf.remaining());
447 Self::decode_with_budget(buf, version, &mut budget)
448 }
449
450 pub fn decode_with_budget(
452 buf: &mut impl Buf,
453 version: i16,
454 budget: &mut Budget,
455 ) -> Result<Self, DecodeError> {
456 let mut this = Self::default();
457 if version <= 12 {
458 this.topic = if is_flexible(version) {
459 wire::get_compact_string(buf)?
460 } else {
461 wire::get_string(buf)?
462 };
463 }
464 if version >= 13 {
465 this.topic_id = wire::get_uuid(buf)?;
466 }
467 this.partitions = {
468 let len = if is_flexible(version) {
469 wire::get_compact_array_len(buf)?
470 } else {
471 wire::get_array_len(buf)?
472 };
473 match len {
474 None => return Err(DecodeError::InvalidLength(-1)),
475 Some(n) => {
476 let mut items = Vec::new();
477 for _ in 0..n {
478 let mark = buf.remaining();
479 let item = FetchPartition::decode_with_budget(buf, version, budget)?;
480 budget.progress(mark, buf.remaining())?;
481 budget.push(&mut items, item)?;
482 }
483 items
484 }
485 }
486 };
487 if is_flexible(version) {
488 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
489 }
490 Ok(this)
491 }
492}
493
494#[derive(Debug, Clone, PartialEq)]
496#[non_exhaustive]
497pub struct FetchPartition {
498 pub partition: i32,
500 pub current_leader_epoch: i32,
502 pub fetch_offset: i64,
504 pub last_fetched_epoch: i32,
506 pub log_start_offset: i64,
508 pub partition_max_bytes: i32,
510 pub replica_directory_id: Option<[u8; 16]>,
513 pub high_watermark: Option<i64>,
516 pub unknown_tagged_fields: Vec<RawTaggedField>,
518}
519
520impl Default for FetchPartition {
521 fn default() -> Self {
522 Self {
523 partition: 0,
524 current_leader_epoch: -1,
525 fetch_offset: 0,
526 last_fetched_epoch: -1,
527 log_start_offset: -1,
528 partition_max_bytes: 0,
529 replica_directory_id: None,
530 high_watermark: None,
531 unknown_tagged_fields: Vec::new(),
532 }
533 }
534}
535
536impl FetchPartition {
537 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
539 buf.put_i32(self.partition);
540 if version >= 9 {
541 buf.put_i32(self.current_leader_epoch);
542 }
543 buf.put_i64(self.fetch_offset);
544 if version >= 12 {
545 buf.put_i32(self.last_fetched_epoch);
546 }
547 if version >= 5 {
548 buf.put_i64(self.log_start_offset);
549 }
550 buf.put_i32(self.partition_max_bytes);
551 if is_flexible(version) {
552 let mut tagged = self.unknown_tagged_fields.clone();
553 if version >= 17 {
554 if let Some(v) = &self.replica_directory_id {
555 let mut tag_data = bytes::BytesMut::new();
556 {
557 let buf = &mut tag_data;
558 wire::put_uuid(buf, (*v));
559 }
560 tagged.push(RawTaggedField {
561 tag: 0,
562 data: tag_data.freeze(),
563 });
564 }
565 }
566 if version >= 18 {
567 if let Some(v) = &self.high_watermark {
568 let mut tag_data = bytes::BytesMut::new();
569 {
570 let buf = &mut tag_data;
571 buf.put_i64((*v));
572 }
573 tagged.push(RawTaggedField {
574 tag: 1,
575 data: tag_data.freeze(),
576 });
577 }
578 }
579 tagged.sort_by_key(|f| f.tag);
580 wire::put_tagged_fields(buf, &tagged);
581 }
582 Ok(())
583 }
584
585 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
588 Self::decode_with_limits(buf, version, Limits::default())
589 }
590
591 pub fn decode_with_limits(
593 buf: &mut impl Buf,
594 version: i16,
595 limits: Limits,
596 ) -> Result<Self, DecodeError> {
597 let mut budget = limits.budget(buf.remaining());
598 Self::decode_with_budget(buf, version, &mut budget)
599 }
600
601 pub fn decode_with_budget(
603 buf: &mut impl Buf,
604 version: i16,
605 budget: &mut Budget,
606 ) -> Result<Self, DecodeError> {
607 let mut this = Self::default();
608 this.partition = wire::get_i32(buf)?;
609 if version >= 9 {
610 this.current_leader_epoch = wire::get_i32(buf)?;
611 }
612 this.fetch_offset = wire::get_i64(buf)?;
613 if version >= 12 {
614 this.last_fetched_epoch = wire::get_i32(buf)?;
615 }
616 if version >= 5 {
617 this.log_start_offset = wire::get_i64(buf)?;
618 }
619 this.partition_max_bytes = wire::get_i32(buf)?;
620 if is_flexible(version) {
621 for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
622 if raw.tag == 0 && (version >= 17) {
623 let mut tag_data = raw.data;
624 let buf = &mut tag_data;
625 this.replica_directory_id = Some(wire::get_uuid(buf)?);
626 if buf.has_remaining() {
627 return Err(DecodeError::InvalidLength(
628 i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
629 ));
630 }
631 } else if raw.tag == 1 && (version >= 18) {
632 let mut tag_data = raw.data;
633 let buf = &mut tag_data;
634 this.high_watermark = Some(wire::get_i64(buf)?);
635 if buf.has_remaining() {
636 return Err(DecodeError::InvalidLength(
637 i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
638 ));
639 }
640 } else {
641 budget.push(&mut this.unknown_tagged_fields, raw)?;
642 }
643 }
644 }
645 Ok(this)
646 }
647}
648
649#[derive(Debug, Clone, PartialEq)]
651#[non_exhaustive]
652pub struct ForgottenTopic {
653 pub topic: String,
655 pub topic_id: [u8; 16],
657 pub partitions: Vec<i32>,
659 pub unknown_tagged_fields: Vec<RawTaggedField>,
661}
662
663impl Default for ForgottenTopic {
664 fn default() -> Self {
665 Self {
666 topic: String::new(),
667 topic_id: [0u8; 16],
668 partitions: Vec::new(),
669 unknown_tagged_fields: Vec::new(),
670 }
671 }
672}
673
674impl ForgottenTopic {
675 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
677 if (7..=12).contains(&version) {
678 if is_flexible(version) {
679 wire::put_compact_string(buf, &self.topic);
680 } else {
681 wire::put_string(buf, &self.topic)?;
682 }
683 }
684 if version >= 13 {
685 wire::put_uuid(buf, self.topic_id);
686 }
687 if version >= 7 {
688 if is_flexible(version) {
689 wire::put_compact_array_len(buf, Some(self.partitions.len()));
690 } else {
691 wire::put_array_len(buf, Some(self.partitions.len()))?;
692 }
693 for item in &self.partitions {
694 buf.put_i32(*item);
695 }
696 }
697 if is_flexible(version) {
698 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
699 }
700 Ok(())
701 }
702
703 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
706 Self::decode_with_limits(buf, version, Limits::default())
707 }
708
709 pub fn decode_with_limits(
711 buf: &mut impl Buf,
712 version: i16,
713 limits: Limits,
714 ) -> Result<Self, DecodeError> {
715 let mut budget = limits.budget(buf.remaining());
716 Self::decode_with_budget(buf, version, &mut budget)
717 }
718
719 pub fn decode_with_budget(
721 buf: &mut impl Buf,
722 version: i16,
723 budget: &mut Budget,
724 ) -> Result<Self, DecodeError> {
725 let mut this = Self::default();
726 if (7..=12).contains(&version) {
727 this.topic = if is_flexible(version) {
728 wire::get_compact_string(buf)?
729 } else {
730 wire::get_string(buf)?
731 };
732 }
733 if version >= 13 {
734 this.topic_id = wire::get_uuid(buf)?;
735 }
736 if version >= 7 {
737 this.partitions = {
738 let len = if is_flexible(version) {
739 wire::get_compact_array_len(buf)?
740 } else {
741 wire::get_array_len(buf)?
742 };
743 match len {
744 None => return Err(DecodeError::InvalidLength(-1)),
745 Some(n) => {
746 let mut items = Vec::new();
747 for _ in 0..n {
748 budget.push(&mut items, wire::get_i32(buf)?)?;
749 }
750 items
751 }
752 }
753 };
754 }
755 if is_flexible(version) {
756 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
757 }
758 Ok(this)
759 }
760}