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