1use bytes::BufMut;
3
4use crate::{
5 DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6 primitives::{
7 fixed::{get_i8, get_i32, get_i64, put_i8, put_i32, put_i64},
8 string_bytes::{
9 compact_nullable_string_len, compact_string_len, nullable_string_len,
10 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
11 string_len,
12 },
13 string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
14 },
15 tagged_fields::{WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len},
16};
17pub const API_KEY: i16 = 1;
18pub const MIN_VERSION: i16 = 4;
19pub const MAX_VERSION: i16 = 18;
20pub const LATEST_STABLE_VERSION: i16 = 18;
21pub const FLEXIBLE_MIN: i16 = 12;
22#[inline]
23#[must_use]
24pub fn is_flexible(version: i16) -> bool {
25 version >= FLEXIBLE_MIN
26}
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct FetchRequest<'a> {
29 pub replica_id: i32,
30 pub max_wait_ms: i32,
31 pub min_bytes: i32,
32 pub max_bytes: i32,
33 pub isolation_level: i8,
34 pub session_id: i32,
35 pub session_epoch: i32,
36 pub topics: Vec<FetchTopic<'a>>,
37 pub forgotten_topics_data: Vec<ForgottenTopic<'a>>,
38 pub rack_id: &'a str,
39 pub cluster_id: Option<String>,
40 pub replica_state: ReplicaState,
41 pub unknown_tagged_fields: UnknownTaggedFields,
42}
43impl Default for FetchRequest<'_> {
44 fn default() -> Self {
45 Self {
46 replica_id: -1i32,
47 max_wait_ms: 0i32,
48 min_bytes: 0i32,
49 max_bytes: 2_147_483_647i32,
50 isolation_level: 0i8,
51 session_id: 0i32,
52 session_epoch: -1i32,
53 topics: Vec::new(),
54 forgotten_topics_data: Vec::new(),
55 rack_id: "",
56 cluster_id: None,
57 replica_state: <ReplicaState>::default(),
58 unknown_tagged_fields: UnknownTaggedFields::default(),
59 }
60 }
61}
62impl<'a> FetchRequest<'a> {
63 #[must_use]
67 pub fn to_owned(&self) -> crate::owned::fetch_request::FetchRequest {
68 crate::owned::fetch_request::FetchRequest {
69 replica_id: (self.replica_id),
70 max_wait_ms: (self.max_wait_ms),
71 min_bytes: (self.min_bytes),
72 max_bytes: (self.max_bytes),
73 isolation_level: (self.isolation_level),
74 session_id: (self.session_id),
75 session_epoch: (self.session_epoch),
76 topics: (self.topics).iter().map(FetchTopic::to_owned).collect(),
77 forgotten_topics_data: (self.forgotten_topics_data)
78 .iter()
79 .map(ForgottenTopic::to_owned)
80 .collect(),
81 rack_id: (self.rack_id).to_string(),
82 cluster_id: self.cluster_id.clone(),
83 replica_state: (self.replica_state).to_owned(),
84 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
85 }
86 }
87 fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
88 if (0..=14).contains(&version) {
89 put_i32(buf, self.replica_id);
90 }
91 }
92 fn encode_field_1<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
93 if version >= 0 {
94 put_i32(buf, self.max_wait_ms);
95 }
96 }
97 fn encode_field_2<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
98 if version >= 0 {
99 put_i32(buf, self.min_bytes);
100 }
101 }
102 fn encode_field_3<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
103 if version >= 3 {
104 put_i32(buf, self.max_bytes);
105 }
106 }
107 fn encode_field_4<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
108 if version >= 4 {
109 put_i8(buf, self.isolation_level);
110 }
111 }
112 fn encode_field_5<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
113 if version >= 7 {
114 put_i32(buf, self.session_id);
115 }
116 }
117 fn encode_field_6<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
118 if version >= 7 {
119 put_i32(buf, self.session_epoch);
120 }
121 }
122 fn encode_field_7<B: BufMut>(
123 &self,
124 buf: &mut B,
125 version: i16,
126 flex: bool,
127 ) -> Result<(), ProtocolError> {
128 if version >= 0 {
129 {
130 crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
131 for it in &self.topics {
132 it.encode(buf, version)?;
133 }
134 };
135 }
136 Ok(())
137 }
138 fn encode_field_8<B: BufMut>(
139 &self,
140 buf: &mut B,
141 version: i16,
142 flex: bool,
143 ) -> Result<(), ProtocolError> {
144 if version >= 7 {
145 {
146 crate::primitives::array::put_array_len(
147 buf,
148 (self.forgotten_topics_data).len(),
149 flex,
150 );
151 for it in &self.forgotten_topics_data {
152 it.encode(buf, version)?;
153 }
154 };
155 }
156 Ok(())
157 }
158 fn encode_field_9<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
159 if version >= 11 {
160 if flex {
161 let () = put_compact_string(buf, self.rack_id);
162 } else {
163 let () = put_string(buf, self.rack_id);
164 }
165 }
166 }
167 fn encode_tagged_fields<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
168 if flex {
169 let mut tagged = WriteTaggedFields::new();
170 if self.cluster_id.is_some() {
171 let payload = encode_to_bytes(
172 if flex {
173 compact_nullable_string_len(self.cluster_id.as_deref())
174 } else {
175 nullable_string_len(self.cluster_id.as_deref())
176 },
177 |b| {
178 if flex {
179 let () = put_compact_nullable_string(b, self.cluster_id.as_deref());
180 } else {
181 let () = put_nullable_string(b, self.cluster_id.as_deref());
182 }
183 Ok(())
184 },
185 );
186 tagged.add(0, payload);
187 }
188 if version >= 15 && !(crate::codegen_helpers::is_default(&self.replica_state)) {
189 let payload = encode_to_bytes(self.replica_state.encoded_len(version), |b| {
190 self.replica_state.encode(b, version)?;
191 Ok(())
192 });
193 tagged.add(1, payload);
194 }
195 tagged.write(buf, &self.unknown_tagged_fields);
196 }
197 }
198 fn decode_field_0(
199 out: &mut Self,
200 buf: &mut &'a [u8],
201 version: i16,
202 _flex: bool,
203 ) -> Result<(), ProtocolError> {
204 if (0..=14).contains(&version) {
205 out.replica_id = get_i32(buf)?;
206 }
207 Ok(())
208 }
209 fn decode_field_1(
210 out: &mut Self,
211 buf: &mut &'a [u8],
212 version: i16,
213 _flex: bool,
214 ) -> Result<(), ProtocolError> {
215 if version >= 0 {
216 out.max_wait_ms = get_i32(buf)?;
217 }
218 Ok(())
219 }
220 fn decode_field_2(
221 out: &mut Self,
222 buf: &mut &'a [u8],
223 version: i16,
224 _flex: bool,
225 ) -> Result<(), ProtocolError> {
226 if version >= 0 {
227 out.min_bytes = get_i32(buf)?;
228 }
229 Ok(())
230 }
231 fn decode_field_3(
232 out: &mut Self,
233 buf: &mut &'a [u8],
234 version: i16,
235 _flex: bool,
236 ) -> Result<(), ProtocolError> {
237 if version >= 3 {
238 out.max_bytes = get_i32(buf)?;
239 }
240 Ok(())
241 }
242 fn decode_field_4(
243 out: &mut Self,
244 buf: &mut &'a [u8],
245 version: i16,
246 _flex: bool,
247 ) -> Result<(), ProtocolError> {
248 if version >= 4 {
249 out.isolation_level = get_i8(buf)?;
250 }
251 Ok(())
252 }
253 fn decode_field_5(
254 out: &mut Self,
255 buf: &mut &'a [u8],
256 version: i16,
257 _flex: bool,
258 ) -> Result<(), ProtocolError> {
259 if version >= 7 {
260 out.session_id = get_i32(buf)?;
261 }
262 Ok(())
263 }
264 fn decode_field_6(
265 out: &mut Self,
266 buf: &mut &'a [u8],
267 version: i16,
268 _flex: bool,
269 ) -> Result<(), ProtocolError> {
270 if version >= 7 {
271 out.session_epoch = get_i32(buf)?;
272 }
273 Ok(())
274 }
275 fn decode_field_7(
276 out: &mut Self,
277 buf: &mut &'a [u8],
278 version: i16,
279 flex: bool,
280 ) -> Result<(), ProtocolError> {
281 if version >= 0 {
282 out.topics = {
283 let n = crate::primitives::array::get_array_len(buf, flex)?;
284 let mut v = Vec::with_capacity(n);
285 for _ in 0..n {
286 v.push(FetchTopic::decode_borrow(buf, version)?);
287 }
288 v
289 };
290 }
291 Ok(())
292 }
293 fn decode_field_8(
294 out: &mut Self,
295 buf: &mut &'a [u8],
296 version: i16,
297 flex: bool,
298 ) -> Result<(), ProtocolError> {
299 if version >= 7 {
300 out.forgotten_topics_data = {
301 let n = crate::primitives::array::get_array_len(buf, flex)?;
302 let mut v = Vec::with_capacity(n);
303 for _ in 0..n {
304 v.push(ForgottenTopic::decode_borrow(buf, version)?);
305 }
306 v
307 };
308 }
309 Ok(())
310 }
311 fn decode_field_9(
312 out: &mut Self,
313 buf: &mut &'a [u8],
314 version: i16,
315 flex: bool,
316 ) -> Result<(), ProtocolError> {
317 if version >= 11 {
318 out.rack_id = if flex {
319 get_compact_string_borrowed(buf)?
320 } else {
321 get_string_borrowed(buf)?
322 };
323 }
324 Ok(())
325 }
326 fn decode_tagged_fields(
327 out: &mut Self,
328 buf: &mut &'a [u8],
329 version: i16,
330 flex: bool,
331 ) -> Result<(), ProtocolError> {
332 if flex {
333 let mut tag_cluster_id = None;
334 let mut tag_replica_state = None;
335 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
336 0 => {
337 tag_cluster_id = Some({
338 let b: &mut &[u8] = payload;
339 if flex {
340 crate::primitives::string_bytes::get_compact_nullable_string_owned(b)?
341 } else {
342 crate::primitives::string_bytes::get_nullable_string_owned(b)?
343 }
344 });
345 Ok(true)
346 }
347 1 if version >= 15 => {
348 tag_replica_state = Some({
349 let b: &mut &[u8] = payload;
350 ReplicaState::decode_borrow(b, version)?
351 });
352 Ok(true)
353 }
354 1 => Err(ProtocolError::TagNotValidForVersion { tag: 1, version }),
355 _ => Ok(false),
356 })?;
357 if let Some(v) = tag_cluster_id {
358 out.cluster_id = v;
359 }
360 if let Some(v) = tag_replica_state {
361 out.replica_state = v;
362 }
363 }
364 Ok(())
365 }
366}
367impl Encode for FetchRequest<'_> {
368 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
369 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
370 return Err(ProtocolError::UnsupportedVersion {
371 api_key: API_KEY,
372 version,
373 });
374 }
375 let flex = is_flexible(version);
376 self.encode_field_0(buf, version, flex);
377 self.encode_field_1(buf, version, flex);
378 self.encode_field_2(buf, version, flex);
379 self.encode_field_3(buf, version, flex);
380 self.encode_field_4(buf, version, flex);
381 self.encode_field_5(buf, version, flex);
382 self.encode_field_6(buf, version, flex);
383 self.encode_field_7(buf, version, flex)?;
384 self.encode_field_8(buf, version, flex)?;
385 self.encode_field_9(buf, version, flex);
386 self.encode_tagged_fields(buf, version, flex);
387 Ok(())
388 }
389 fn encoded_len(&self, version: i16) -> usize {
390 let flex = is_flexible(version);
391 let mut n: usize = 0;
392 if (0..=14).contains(&version) {
393 n += 4;
394 }
395 if version >= 0 {
396 n += 4;
397 }
398 if version >= 0 {
399 n += 4;
400 }
401 if version >= 3 {
402 n += 4;
403 }
404 if version >= 4 {
405 n += 1;
406 }
407 if version >= 7 {
408 n += 4;
409 }
410 if version >= 7 {
411 n += 4;
412 }
413 if version >= 0 {
414 n += {
415 let prefix =
416 crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
417 let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
418 prefix + body
419 };
420 }
421 if version >= 7 {
422 n += {
423 let prefix = crate::primitives::array::array_len_prefix_len(
424 (self.forgotten_topics_data).len(),
425 flex,
426 );
427 let body: usize = (self.forgotten_topics_data)
428 .iter()
429 .map(|it| it.encoded_len(version))
430 .sum();
431 prefix + body
432 };
433 }
434 if version >= 11 {
435 n += if flex {
436 compact_string_len(self.rack_id)
437 } else {
438 string_len(self.rack_id)
439 };
440 }
441 if flex {
442 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
443 if self.cluster_id.is_some() {
444 known_pairs.push((
445 0,
446 if flex {
447 compact_nullable_string_len(self.cluster_id.as_deref())
448 } else {
449 nullable_string_len(self.cluster_id.as_deref())
450 },
451 ));
452 }
453 if version >= 15 && !(crate::codegen_helpers::is_default(&self.replica_state)) {
454 known_pairs.push((1, self.replica_state.encoded_len(version)));
455 }
456 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
457 }
458 n
459 }
460}
461impl<'de> DecodeBorrow<'de> for FetchRequest<'de> {
462 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
463 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
464 return Err(ProtocolError::UnsupportedVersion {
465 api_key: API_KEY,
466 version,
467 });
468 }
469 let flex = is_flexible(version);
470 let mut out = Self::default();
471 Self::decode_field_0(&mut out, buf, version, flex)?;
472 Self::decode_field_1(&mut out, buf, version, flex)?;
473 Self::decode_field_2(&mut out, buf, version, flex)?;
474 Self::decode_field_3(&mut out, buf, version, flex)?;
475 Self::decode_field_4(&mut out, buf, version, flex)?;
476 Self::decode_field_5(&mut out, buf, version, flex)?;
477 Self::decode_field_6(&mut out, buf, version, flex)?;
478 Self::decode_field_7(&mut out, buf, version, flex)?;
479 Self::decode_field_8(&mut out, buf, version, flex)?;
480 Self::decode_field_9(&mut out, buf, version, flex)?;
481 Self::decode_tagged_fields(&mut out, buf, version, flex)?;
482 Ok(out)
483 }
484}
485#[cfg(test)]
486impl FetchRequest<'_> {
487 #[must_use]
488 pub fn populated(version: i16) -> Self {
489 let mut m = Self::default();
490 if (0..=14).contains(&version) {
491 m.replica_id = 1i32;
492 }
493 if version >= 0 {
494 m.max_wait_ms = 1i32;
495 }
496 if version >= 0 {
497 m.min_bytes = 1i32;
498 }
499 if version >= 3 {
500 m.max_bytes = 1i32;
501 }
502 if version >= 4 {
503 m.isolation_level = 1i8;
504 }
505 if version >= 7 {
506 m.session_id = 1i32;
507 }
508 if version >= 7 {
509 m.session_epoch = 1i32;
510 }
511 if version >= 0 {
512 m.topics = vec![FetchTopic::populated(version)];
513 }
514 if version >= 7 {
515 m.forgotten_topics_data = vec![ForgottenTopic::populated(version)];
516 }
517 if version >= 11 {
518 m.rack_id = "x";
519 }
520 if version >= 12 {
521 m.cluster_id = Some("x".to_string());
522 }
523 if version >= 15 {
524 m.replica_state = ReplicaState::populated(version);
525 }
526 m
527 }
528}
529#[derive(Debug, Clone, PartialEq, Eq)]
530pub struct ReplicaState {
531 pub replica_id: i32,
532 pub replica_epoch: i64,
533 pub unknown_tagged_fields: UnknownTaggedFields,
534}
535impl Default for ReplicaState {
536 fn default() -> Self {
537 Self {
538 replica_id: -1i32,
539 replica_epoch: -1i64,
540 unknown_tagged_fields: UnknownTaggedFields::default(),
541 }
542 }
543}
544impl ReplicaState {
545 #[must_use]
549 pub fn to_owned(&self) -> crate::owned::fetch_request::ReplicaState {
550 crate::owned::fetch_request::ReplicaState {
551 replica_id: (self.replica_id),
552 replica_epoch: (self.replica_epoch),
553 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
554 }
555 }
556}
557impl Encode for ReplicaState {
558 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
559 let flex = version >= 12;
560 if version >= 15 {
561 put_i32(buf, self.replica_id);
562 }
563 if version >= 15 {
564 put_i64(buf, self.replica_epoch);
565 }
566 if flex {
567 let tagged = WriteTaggedFields::new();
568 tagged.write(buf, &self.unknown_tagged_fields);
569 }
570 Ok(())
571 }
572 fn encoded_len(&self, version: i16) -> usize {
573 let flex = version >= 12;
574 let mut n: usize = 0;
575 if version >= 15 {
576 n += 4;
577 }
578 if version >= 15 {
579 n += 8;
580 }
581 if flex {
582 let known_pairs: Vec<(u32, usize)> = Vec::new();
583 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
584 }
585 n
586 }
587}
588impl<'de> DecodeBorrow<'de> for ReplicaState {
589 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
590 let flex = version >= 12;
591 let mut out = Self::default();
592 if version >= 15 {
593 out.replica_id = get_i32(buf)?;
594 }
595 if version >= 15 {
596 out.replica_epoch = get_i64(buf)?;
597 }
598 if flex {
599 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
600 }
601 Ok(out)
602 }
603}
604#[cfg(test)]
605impl ReplicaState {
606 #[must_use]
607 pub fn populated(version: i16) -> Self {
608 let mut m = Self::default();
609 if version >= 15 {
610 m.replica_id = 1i32;
611 }
612 if version >= 15 {
613 m.replica_epoch = 1i64;
614 }
615 m
616 }
617}
618#[derive(Debug, Clone, PartialEq, Eq, Default)]
619pub struct FetchTopic<'a> {
620 pub topic: &'a str,
621 pub topic_id: crate::primitives::uuid::Uuid,
622 pub partitions: Vec<FetchPartition>,
623 pub unknown_tagged_fields: UnknownTaggedFields,
624}
625impl FetchTopic<'_> {
626 #[must_use]
630 pub fn to_owned(&self) -> crate::owned::fetch_request::FetchTopic {
631 crate::owned::fetch_request::FetchTopic {
632 topic: (self.topic).to_string(),
633 topic_id: (self.topic_id),
634 partitions: (self.partitions)
635 .iter()
636 .map(FetchPartition::to_owned)
637 .collect(),
638 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
639 }
640 }
641}
642impl Encode for FetchTopic<'_> {
643 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
644 let flex = version >= 12;
645 if (0..=12).contains(&version) {
646 if flex {
647 let () = put_compact_string(buf, self.topic);
648 } else {
649 let () = put_string(buf, self.topic);
650 }
651 }
652 if version >= 13 {
653 crate::primitives::uuid::put_uuid(buf, self.topic_id);
654 }
655 if version >= 0 {
656 {
657 crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
658 for it in &self.partitions {
659 it.encode(buf, version)?;
660 }
661 };
662 }
663 if flex {
664 let tagged = WriteTaggedFields::new();
665 tagged.write(buf, &self.unknown_tagged_fields);
666 }
667 Ok(())
668 }
669 fn encoded_len(&self, version: i16) -> usize {
670 let flex = version >= 12;
671 let mut n: usize = 0;
672 if (0..=12).contains(&version) {
673 n += if flex {
674 compact_string_len(self.topic)
675 } else {
676 string_len(self.topic)
677 };
678 }
679 if version >= 13 {
680 n += 16;
681 }
682 if version >= 0 {
683 n += {
684 let prefix =
685 crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
686 let body: usize = (self.partitions)
687 .iter()
688 .map(|it| it.encoded_len(version))
689 .sum();
690 prefix + body
691 };
692 }
693 if flex {
694 let known_pairs: Vec<(u32, usize)> = Vec::new();
695 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
696 }
697 n
698 }
699}
700impl<'de> DecodeBorrow<'de> for FetchTopic<'de> {
701 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
702 let flex = version >= 12;
703 let mut out = Self::default();
704 if (0..=12).contains(&version) {
705 out.topic = if flex {
706 get_compact_string_borrowed(buf)?
707 } else {
708 get_string_borrowed(buf)?
709 };
710 }
711 if version >= 13 {
712 out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
713 }
714 if version >= 0 {
715 out.partitions = {
716 let n = crate::primitives::array::get_array_len(buf, flex)?;
717 let mut v = Vec::with_capacity(n);
718 for _ in 0..n {
719 v.push(FetchPartition::decode_borrow(buf, version)?);
720 }
721 v
722 };
723 }
724 if flex {
725 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
726 }
727 Ok(out)
728 }
729}
730#[cfg(test)]
731impl FetchTopic<'_> {
732 #[must_use]
733 pub fn populated(version: i16) -> Self {
734 let mut m = Self::default();
735 if (0..=12).contains(&version) {
736 m.topic = "x";
737 }
738 if version >= 13 {
739 m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
740 }
741 if version >= 0 {
742 m.partitions = vec![FetchPartition::populated(version)];
743 }
744 m
745 }
746}
747#[derive(Debug, Clone, PartialEq, Eq)]
748pub struct FetchPartition {
749 pub partition: i32,
750 pub current_leader_epoch: i32,
751 pub fetch_offset: i64,
752 pub last_fetched_epoch: i32,
753 pub log_start_offset: i64,
754 pub partition_max_bytes: i32,
755 pub replica_directory_id: crate::primitives::uuid::Uuid,
756 pub high_watermark: i64,
757 pub unknown_tagged_fields: UnknownTaggedFields,
758}
759impl Default for FetchPartition {
760 fn default() -> Self {
761 Self {
762 partition: 0i32,
763 current_leader_epoch: -1i32,
764 fetch_offset: 0i64,
765 last_fetched_epoch: -1i32,
766 log_start_offset: -1i64,
767 partition_max_bytes: 0i32,
768 replica_directory_id: crate::primitives::uuid::Uuid::default(),
769 high_watermark: 9_223_372_036_854_775_807i64,
770 unknown_tagged_fields: UnknownTaggedFields::default(),
771 }
772 }
773}
774impl FetchPartition {
775 #[must_use]
779 pub fn to_owned(&self) -> crate::owned::fetch_request::FetchPartition {
780 crate::owned::fetch_request::FetchPartition {
781 partition: (self.partition),
782 current_leader_epoch: (self.current_leader_epoch),
783 fetch_offset: (self.fetch_offset),
784 last_fetched_epoch: (self.last_fetched_epoch),
785 log_start_offset: (self.log_start_offset),
786 partition_max_bytes: (self.partition_max_bytes),
787 replica_directory_id: (self.replica_directory_id),
788 high_watermark: (self.high_watermark),
789 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
790 }
791 }
792 fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
793 if version >= 0 {
794 put_i32(buf, self.partition);
795 }
796 }
797 fn encode_field_1<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
798 if version >= 9 {
799 put_i32(buf, self.current_leader_epoch);
800 }
801 }
802 fn encode_field_2<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
803 if version >= 0 {
804 put_i64(buf, self.fetch_offset);
805 }
806 }
807 fn encode_field_3<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
808 if version >= 12 {
809 put_i32(buf, self.last_fetched_epoch);
810 }
811 }
812 fn encode_field_4<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
813 if version >= 5 {
814 put_i64(buf, self.log_start_offset);
815 }
816 }
817 fn encode_field_5<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
818 if version >= 0 {
819 put_i32(buf, self.partition_max_bytes);
820 }
821 }
822 fn encode_tagged_fields<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
823 if flex {
824 let mut tagged = WriteTaggedFields::new();
825 if version >= 17 && !(crate::codegen_helpers::is_default(&self.replica_directory_id)) {
826 let payload = encode_to_bytes(16, |b| {
827 crate::primitives::uuid::put_uuid(b, self.replica_directory_id);
828 Ok(())
829 });
830 tagged.add(0, payload);
831 }
832 if version >= 18 && self.high_watermark != 9_223_372_036_854_775_807i64 {
833 let payload = encode_to_bytes(8, |b| {
834 put_i64(b, self.high_watermark);
835 Ok(())
836 });
837 tagged.add(1, payload);
838 }
839 tagged.write(buf, &self.unknown_tagged_fields);
840 }
841 }
842 fn decode_field_0(
843 out: &mut Self,
844 buf: &mut &[u8],
845 version: i16,
846 _flex: bool,
847 ) -> Result<(), ProtocolError> {
848 if version >= 0 {
849 out.partition = get_i32(buf)?;
850 }
851 Ok(())
852 }
853 fn decode_field_1(
854 out: &mut Self,
855 buf: &mut &[u8],
856 version: i16,
857 _flex: bool,
858 ) -> Result<(), ProtocolError> {
859 if version >= 9 {
860 out.current_leader_epoch = get_i32(buf)?;
861 }
862 Ok(())
863 }
864 fn decode_field_2(
865 out: &mut Self,
866 buf: &mut &[u8],
867 version: i16,
868 _flex: bool,
869 ) -> Result<(), ProtocolError> {
870 if version >= 0 {
871 out.fetch_offset = get_i64(buf)?;
872 }
873 Ok(())
874 }
875 fn decode_field_3(
876 out: &mut Self,
877 buf: &mut &[u8],
878 version: i16,
879 _flex: bool,
880 ) -> Result<(), ProtocolError> {
881 if version >= 12 {
882 out.last_fetched_epoch = get_i32(buf)?;
883 }
884 Ok(())
885 }
886 fn decode_field_4(
887 out: &mut Self,
888 buf: &mut &[u8],
889 version: i16,
890 _flex: bool,
891 ) -> Result<(), ProtocolError> {
892 if version >= 5 {
893 out.log_start_offset = get_i64(buf)?;
894 }
895 Ok(())
896 }
897 fn decode_field_5(
898 out: &mut Self,
899 buf: &mut &[u8],
900 version: i16,
901 _flex: bool,
902 ) -> Result<(), ProtocolError> {
903 if version >= 0 {
904 out.partition_max_bytes = get_i32(buf)?;
905 }
906 Ok(())
907 }
908 fn decode_tagged_fields(
909 out: &mut Self,
910 buf: &mut &[u8],
911 version: i16,
912 flex: bool,
913 ) -> Result<(), ProtocolError> {
914 if flex {
915 let mut tag_replica_directory_id = None;
916 let mut tag_high_watermark = None;
917 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
918 0 if version >= 17 => {
919 tag_replica_directory_id = Some({
920 let b: &mut &[u8] = payload;
921 crate::primitives::uuid::get_uuid(b)?
922 });
923 Ok(true)
924 }
925 0 => Err(ProtocolError::TagNotValidForVersion { tag: 0, version }),
926 1 if version >= 18 => {
927 tag_high_watermark = Some({
928 let b: &mut &[u8] = payload;
929 get_i64(b)?
930 });
931 Ok(true)
932 }
933 1 => Err(ProtocolError::TagNotValidForVersion { tag: 1, version }),
934 _ => Ok(false),
935 })?;
936 if let Some(v) = tag_replica_directory_id {
937 out.replica_directory_id = v;
938 }
939 if let Some(v) = tag_high_watermark {
940 out.high_watermark = v;
941 }
942 }
943 Ok(())
944 }
945}
946impl Encode for FetchPartition {
947 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
948 let flex = version >= 12;
949 self.encode_field_0(buf, version, flex);
950 self.encode_field_1(buf, version, flex);
951 self.encode_field_2(buf, version, flex);
952 self.encode_field_3(buf, version, flex);
953 self.encode_field_4(buf, version, flex);
954 self.encode_field_5(buf, version, flex);
955 self.encode_tagged_fields(buf, version, flex);
956 Ok(())
957 }
958 fn encoded_len(&self, version: i16) -> usize {
959 let flex = version >= 12;
960 let mut n: usize = 0;
961 if version >= 0 {
962 n += 4;
963 }
964 if version >= 9 {
965 n += 4;
966 }
967 if version >= 0 {
968 n += 8;
969 }
970 if version >= 12 {
971 n += 4;
972 }
973 if version >= 5 {
974 n += 8;
975 }
976 if version >= 0 {
977 n += 4;
978 }
979 if flex {
980 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
981 if version >= 17 && !(crate::codegen_helpers::is_default(&self.replica_directory_id)) {
982 known_pairs.push((0, 16));
983 }
984 if version >= 18 && self.high_watermark != 9_223_372_036_854_775_807i64 {
985 known_pairs.push((1, 8));
986 }
987 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
988 }
989 n
990 }
991}
992impl<'de> DecodeBorrow<'de> for FetchPartition {
993 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
994 let flex = version >= 12;
995 let mut out = Self::default();
996 Self::decode_field_0(&mut out, buf, version, flex)?;
997 Self::decode_field_1(&mut out, buf, version, flex)?;
998 Self::decode_field_2(&mut out, buf, version, flex)?;
999 Self::decode_field_3(&mut out, buf, version, flex)?;
1000 Self::decode_field_4(&mut out, buf, version, flex)?;
1001 Self::decode_field_5(&mut out, buf, version, flex)?;
1002 Self::decode_tagged_fields(&mut out, buf, version, flex)?;
1003 Ok(out)
1004 }
1005}
1006#[cfg(test)]
1007impl FetchPartition {
1008 #[must_use]
1009 pub fn populated(version: i16) -> Self {
1010 let mut m = Self::default();
1011 if version >= 0 {
1012 m.partition = 1i32;
1013 }
1014 if version >= 9 {
1015 m.current_leader_epoch = 1i32;
1016 }
1017 if version >= 0 {
1018 m.fetch_offset = 1i64;
1019 }
1020 if version >= 12 {
1021 m.last_fetched_epoch = 1i32;
1022 }
1023 if version >= 5 {
1024 m.log_start_offset = 1i64;
1025 }
1026 if version >= 0 {
1027 m.partition_max_bytes = 1i32;
1028 }
1029 if version >= 17 {
1030 m.replica_directory_id = crate::primitives::uuid::Uuid([1u8; 16]);
1031 }
1032 if version >= 18 {
1033 m.high_watermark = 1i64;
1034 }
1035 m
1036 }
1037}
1038#[derive(Debug, Clone, PartialEq, Eq, Default)]
1039pub struct ForgottenTopic<'a> {
1040 pub topic: &'a str,
1041 pub topic_id: crate::primitives::uuid::Uuid,
1042 pub partitions: Vec<i32>,
1043 pub unknown_tagged_fields: UnknownTaggedFields,
1044}
1045impl ForgottenTopic<'_> {
1046 #[must_use]
1050 pub fn to_owned(&self) -> crate::owned::fetch_request::ForgottenTopic {
1051 crate::owned::fetch_request::ForgottenTopic {
1052 topic: (self.topic).to_string(),
1053 topic_id: (self.topic_id),
1054 partitions: (self.partitions).clone(),
1055 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
1056 }
1057 }
1058}
1059impl Encode for ForgottenTopic<'_> {
1060 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
1061 let flex = version >= 12;
1062 if (7..=12).contains(&version) {
1063 if flex {
1064 let () = put_compact_string(buf, self.topic);
1065 } else {
1066 let () = put_string(buf, self.topic);
1067 }
1068 }
1069 if version >= 13 {
1070 crate::primitives::uuid::put_uuid(buf, self.topic_id);
1071 }
1072 if version >= 7 {
1073 {
1074 crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
1075 for it in &self.partitions {
1076 put_i32(buf, *it);
1077 }
1078 };
1079 }
1080 if flex {
1081 let tagged = WriteTaggedFields::new();
1082 tagged.write(buf, &self.unknown_tagged_fields);
1083 }
1084 Ok(())
1085 }
1086 fn encoded_len(&self, version: i16) -> usize {
1087 let flex = version >= 12;
1088 let mut n: usize = 0;
1089 if (7..=12).contains(&version) {
1090 n += if flex {
1091 compact_string_len(self.topic)
1092 } else {
1093 string_len(self.topic)
1094 };
1095 }
1096 if version >= 13 {
1097 n += 16;
1098 }
1099 if version >= 7 {
1100 n += {
1101 let prefix =
1102 crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
1103 let body: usize = (self.partitions).iter().map(|_| 4).sum();
1104 prefix + body
1105 };
1106 }
1107 if flex {
1108 let known_pairs: Vec<(u32, usize)> = Vec::new();
1109 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
1110 }
1111 n
1112 }
1113}
1114impl<'de> DecodeBorrow<'de> for ForgottenTopic<'de> {
1115 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
1116 let flex = version >= 12;
1117 let mut out = Self::default();
1118 if (7..=12).contains(&version) {
1119 out.topic = if flex {
1120 get_compact_string_borrowed(buf)?
1121 } else {
1122 get_string_borrowed(buf)?
1123 };
1124 }
1125 if version >= 13 {
1126 out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
1127 }
1128 if version >= 7 {
1129 out.partitions = {
1130 let n = crate::primitives::array::get_array_len(buf, flex)?;
1131 let mut v = Vec::with_capacity(n);
1132 for _ in 0..n {
1133 v.push(get_i32(buf)?);
1134 }
1135 v
1136 };
1137 }
1138 if flex {
1139 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
1140 }
1141 Ok(out)
1142 }
1143}
1144#[cfg(test)]
1145impl ForgottenTopic<'_> {
1146 #[must_use]
1147 pub fn populated(version: i16) -> Self {
1148 let mut m = Self::default();
1149 if (7..=12).contains(&version) {
1150 m.topic = "x";
1151 }
1152 if version >= 13 {
1153 m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
1154 }
1155 if version >= 7 {
1156 m.partitions = vec![1i32];
1157 }
1158 m
1159 }
1160}