1use bytes::BufMut;
3
4use crate::{
5 DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6 primitives::{
7 fixed::{get_i16, get_i32, get_i64, put_i16, 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::{
14 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
15 get_nullable_string_borrowed, get_string_borrowed,
16 },
17 },
18 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
19};
20pub const API_KEY: i16 = 9;
21pub const MIN_VERSION: i16 = 1;
22pub const MAX_VERSION: i16 = 10;
23pub const FLEXIBLE_MIN: i16 = 6;
24#[inline]
25#[must_use]
26pub fn is_flexible(version: i16) -> bool {
27 version >= FLEXIBLE_MIN
28}
29#[derive(Debug, Clone, PartialEq, Eq, Default)]
30pub struct OffsetFetchResponse<'a> {
31 pub throttle_time_ms: i32,
32 pub topics: Vec<OffsetFetchResponseTopic<'a>>,
33 pub error_code: i16,
34 pub groups: Vec<OffsetFetchResponseGroup<'a>>,
35 pub unknown_tagged_fields: UnknownTaggedFields,
36}
37impl OffsetFetchResponse<'_> {
38 #[must_use]
42 pub fn to_owned(&self) -> crate::owned::offset_fetch_response::OffsetFetchResponse {
43 crate::owned::offset_fetch_response::OffsetFetchResponse {
44 throttle_time_ms: (self.throttle_time_ms),
45 topics: (self.topics)
46 .iter()
47 .map(OffsetFetchResponseTopic::to_owned)
48 .collect(),
49 error_code: (self.error_code),
50 groups: (self.groups)
51 .iter()
52 .map(OffsetFetchResponseGroup::to_owned)
53 .collect(),
54 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
55 }
56 }
57}
58impl Encode for OffsetFetchResponse<'_> {
59 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
60 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
61 return Err(ProtocolError::UnsupportedVersion {
62 api_key: API_KEY,
63 version,
64 });
65 }
66 let flex = is_flexible(version);
67 if version >= 3 {
68 put_i32(buf, self.throttle_time_ms);
69 }
70 if (0..=7).contains(&version) {
71 {
72 crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
73 for it in &self.topics {
74 it.encode(buf, version)?;
75 }
76 };
77 }
78 if (2..=7).contains(&version) {
79 put_i16(buf, self.error_code);
80 }
81 if version >= 8 {
82 {
83 crate::primitives::array::put_array_len(buf, (self.groups).len(), flex);
84 for it in &self.groups {
85 it.encode(buf, version)?;
86 }
87 };
88 }
89 if flex {
90 let tagged = WriteTaggedFields::new();
91 tagged.write(buf, &self.unknown_tagged_fields);
92 }
93 Ok(())
94 }
95 fn encoded_len(&self, version: i16) -> usize {
96 let flex = is_flexible(version);
97 let mut n: usize = 0;
98 if version >= 3 {
99 n += 4;
100 }
101 if (0..=7).contains(&version) {
102 n += {
103 let prefix =
104 crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
105 let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
106 prefix + body
107 };
108 }
109 if (2..=7).contains(&version) {
110 n += 2;
111 }
112 if version >= 8 {
113 n += {
114 let prefix =
115 crate::primitives::array::array_len_prefix_len((self.groups).len(), flex);
116 let body: usize = (self.groups).iter().map(|it| it.encoded_len(version)).sum();
117 prefix + body
118 };
119 }
120 if flex {
121 let known_pairs: Vec<(u32, usize)> = Vec::new();
122 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
123 }
124 n
125 }
126}
127impl<'de> DecodeBorrow<'de> for OffsetFetchResponse<'de> {
128 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
129 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
130 return Err(ProtocolError::UnsupportedVersion {
131 api_key: API_KEY,
132 version,
133 });
134 }
135 let flex = is_flexible(version);
136 let mut out = Self::default();
137 if version >= 3 {
138 out.throttle_time_ms = get_i32(buf)?;
139 }
140 if (0..=7).contains(&version) {
141 out.topics = {
142 let n = crate::primitives::array::get_array_len(buf, flex)?;
143 let mut v = Vec::with_capacity(n);
144 for _ in 0..n {
145 v.push(OffsetFetchResponseTopic::decode_borrow(buf, version)?);
146 }
147 v
148 };
149 }
150 if (2..=7).contains(&version) {
151 out.error_code = get_i16(buf)?;
152 }
153 if version >= 8 {
154 out.groups = {
155 let n = crate::primitives::array::get_array_len(buf, flex)?;
156 let mut v = Vec::with_capacity(n);
157 for _ in 0..n {
158 v.push(OffsetFetchResponseGroup::decode_borrow(buf, version)?);
159 }
160 v
161 };
162 }
163 if flex {
164 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
165 }
166 Ok(out)
167 }
168}
169#[cfg(test)]
170impl OffsetFetchResponse<'_> {
171 #[must_use]
172 pub fn populated(version: i16) -> Self {
173 let mut m = Self::default();
174 if version >= 3 {
175 m.throttle_time_ms = 1i32;
176 }
177 if (0..=7).contains(&version) {
178 m.topics = vec![OffsetFetchResponseTopic::populated(version)];
179 }
180 if (2..=7).contains(&version) {
181 m.error_code = 1i16;
182 }
183 if version >= 8 {
184 m.groups = vec![OffsetFetchResponseGroup::populated(version)];
185 }
186 m
187 }
188}
189#[derive(Debug, Clone, PartialEq, Eq, Default)]
190pub struct OffsetFetchResponseTopic<'a> {
191 pub name: &'a str,
192 pub partitions: Vec<OffsetFetchResponsePartition<'a>>,
193 pub unknown_tagged_fields: UnknownTaggedFields,
194}
195impl OffsetFetchResponseTopic<'_> {
196 #[must_use]
200 pub fn to_owned(&self) -> crate::owned::offset_fetch_response::OffsetFetchResponseTopic {
201 crate::owned::offset_fetch_response::OffsetFetchResponseTopic {
202 name: (self.name).to_string(),
203 partitions: (self.partitions)
204 .iter()
205 .map(OffsetFetchResponsePartition::to_owned)
206 .collect(),
207 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
208 }
209 }
210}
211impl Encode for OffsetFetchResponseTopic<'_> {
212 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
213 let flex = version >= 6;
214 if (0..=7).contains(&version) {
215 if flex {
216 let () = put_compact_string(buf, self.name);
217 } else {
218 let () = put_string(buf, self.name);
219 }
220 }
221 if (0..=7).contains(&version) {
222 {
223 crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
224 for it in &self.partitions {
225 it.encode(buf, version)?;
226 }
227 };
228 }
229 if flex {
230 let tagged = WriteTaggedFields::new();
231 tagged.write(buf, &self.unknown_tagged_fields);
232 }
233 Ok(())
234 }
235 fn encoded_len(&self, version: i16) -> usize {
236 let flex = version >= 6;
237 let mut n: usize = 0;
238 if (0..=7).contains(&version) {
239 n += if flex {
240 compact_string_len(self.name)
241 } else {
242 string_len(self.name)
243 };
244 }
245 if (0..=7).contains(&version) {
246 n += {
247 let prefix =
248 crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
249 let body: usize = (self.partitions)
250 .iter()
251 .map(|it| it.encoded_len(version))
252 .sum();
253 prefix + body
254 };
255 }
256 if flex {
257 let known_pairs: Vec<(u32, usize)> = Vec::new();
258 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
259 }
260 n
261 }
262}
263impl<'de> DecodeBorrow<'de> for OffsetFetchResponseTopic<'de> {
264 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
265 let flex = version >= 6;
266 let mut out = Self::default();
267 if (0..=7).contains(&version) {
268 out.name = if flex {
269 get_compact_string_borrowed(buf)?
270 } else {
271 get_string_borrowed(buf)?
272 };
273 }
274 if (0..=7).contains(&version) {
275 out.partitions = {
276 let n = crate::primitives::array::get_array_len(buf, flex)?;
277 let mut v = Vec::with_capacity(n);
278 for _ in 0..n {
279 v.push(OffsetFetchResponsePartition::decode_borrow(buf, version)?);
280 }
281 v
282 };
283 }
284 if flex {
285 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
286 }
287 Ok(out)
288 }
289}
290#[cfg(test)]
291impl OffsetFetchResponseTopic<'_> {
292 #[must_use]
293 pub fn populated(version: i16) -> Self {
294 let mut m = Self::default();
295 if (0..=7).contains(&version) {
296 m.name = "x";
297 }
298 if (0..=7).contains(&version) {
299 m.partitions = vec![OffsetFetchResponsePartition::populated(version)];
300 }
301 m
302 }
303}
304#[derive(Debug, Clone, PartialEq, Eq)]
305pub struct OffsetFetchResponsePartition<'a> {
306 pub partition_index: i32,
307 pub committed_offset: i64,
308 pub committed_leader_epoch: i32,
309 pub metadata: Option<&'a str>,
310 pub error_code: i16,
311 pub unknown_tagged_fields: UnknownTaggedFields,
312}
313impl Default for OffsetFetchResponsePartition<'_> {
314 fn default() -> Self {
315 Self {
316 partition_index: 0i32,
317 committed_offset: 0i64,
318 committed_leader_epoch: -1i32,
319 metadata: Some(""),
320 error_code: 0i16,
321 unknown_tagged_fields: UnknownTaggedFields::default(),
322 }
323 }
324}
325impl OffsetFetchResponsePartition<'_> {
326 #[must_use]
330 pub fn to_owned(&self) -> crate::owned::offset_fetch_response::OffsetFetchResponsePartition {
331 crate::owned::offset_fetch_response::OffsetFetchResponsePartition {
332 partition_index: (self.partition_index),
333 committed_offset: (self.committed_offset),
334 committed_leader_epoch: (self.committed_leader_epoch),
335 metadata: (self.metadata).map(std::string::ToString::to_string),
336 error_code: (self.error_code),
337 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
338 }
339 }
340}
341impl Encode for OffsetFetchResponsePartition<'_> {
342 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
343 let flex = version >= 6;
344 if (0..=7).contains(&version) {
345 put_i32(buf, self.partition_index);
346 }
347 if (0..=7).contains(&version) {
348 put_i64(buf, self.committed_offset);
349 }
350 if (5..=7).contains(&version) {
351 put_i32(buf, self.committed_leader_epoch);
352 }
353 if (0..=7).contains(&version) {
354 if flex {
355 let () = put_compact_nullable_string(buf, self.metadata);
356 } else {
357 let () = put_nullable_string(buf, self.metadata);
358 }
359 }
360 if (0..=7).contains(&version) {
361 put_i16(buf, self.error_code);
362 }
363 if flex {
364 let tagged = WriteTaggedFields::new();
365 tagged.write(buf, &self.unknown_tagged_fields);
366 }
367 Ok(())
368 }
369 fn encoded_len(&self, version: i16) -> usize {
370 let flex = version >= 6;
371 let mut n: usize = 0;
372 if (0..=7).contains(&version) {
373 n += 4;
374 }
375 if (0..=7).contains(&version) {
376 n += 8;
377 }
378 if (5..=7).contains(&version) {
379 n += 4;
380 }
381 if (0..=7).contains(&version) {
382 n += if flex {
383 compact_nullable_string_len(self.metadata)
384 } else {
385 nullable_string_len(self.metadata)
386 };
387 }
388 if (0..=7).contains(&version) {
389 n += 2;
390 }
391 if flex {
392 let known_pairs: Vec<(u32, usize)> = Vec::new();
393 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
394 }
395 n
396 }
397}
398impl<'de> DecodeBorrow<'de> for OffsetFetchResponsePartition<'de> {
399 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
400 let flex = version >= 6;
401 let mut out = Self::default();
402 if (0..=7).contains(&version) {
403 out.partition_index = get_i32(buf)?;
404 }
405 if (0..=7).contains(&version) {
406 out.committed_offset = get_i64(buf)?;
407 }
408 if (5..=7).contains(&version) {
409 out.committed_leader_epoch = get_i32(buf)?;
410 }
411 if (0..=7).contains(&version) {
412 out.metadata = if flex {
413 get_compact_nullable_string_borrowed(buf)?
414 } else {
415 get_nullable_string_borrowed(buf)?
416 };
417 }
418 if (0..=7).contains(&version) {
419 out.error_code = get_i16(buf)?;
420 }
421 if flex {
422 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
423 }
424 Ok(out)
425 }
426}
427#[cfg(test)]
428impl OffsetFetchResponsePartition<'_> {
429 #[must_use]
430 pub fn populated(version: i16) -> Self {
431 let mut m = Self::default();
432 if (0..=7).contains(&version) {
433 m.partition_index = 1i32;
434 }
435 if (0..=7).contains(&version) {
436 m.committed_offset = 1i64;
437 }
438 if (5..=7).contains(&version) {
439 m.committed_leader_epoch = 1i32;
440 }
441 if (0..=7).contains(&version) {
442 m.metadata = Some("x");
443 }
444 if (0..=7).contains(&version) {
445 m.error_code = 1i16;
446 }
447 m
448 }
449}
450#[derive(Debug, Clone, PartialEq, Eq, Default)]
451pub struct OffsetFetchResponseGroup<'a> {
452 pub group_id: &'a str,
453 pub topics: Vec<OffsetFetchResponseTopics<'a>>,
454 pub error_code: i16,
455 pub unknown_tagged_fields: UnknownTaggedFields,
456}
457impl OffsetFetchResponseGroup<'_> {
458 #[must_use]
462 pub fn to_owned(&self) -> crate::owned::offset_fetch_response::OffsetFetchResponseGroup {
463 crate::owned::offset_fetch_response::OffsetFetchResponseGroup {
464 group_id: (self.group_id).to_string(),
465 topics: (self.topics)
466 .iter()
467 .map(OffsetFetchResponseTopics::to_owned)
468 .collect(),
469 error_code: (self.error_code),
470 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
471 }
472 }
473}
474impl Encode for OffsetFetchResponseGroup<'_> {
475 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
476 let flex = version >= 6;
477 if version >= 8 {
478 if flex {
479 let () = put_compact_string(buf, self.group_id);
480 } else {
481 let () = put_string(buf, self.group_id);
482 }
483 }
484 if version >= 8 {
485 {
486 crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
487 for it in &self.topics {
488 it.encode(buf, version)?;
489 }
490 };
491 }
492 if version >= 8 {
493 put_i16(buf, self.error_code);
494 }
495 if flex {
496 let tagged = WriteTaggedFields::new();
497 tagged.write(buf, &self.unknown_tagged_fields);
498 }
499 Ok(())
500 }
501 fn encoded_len(&self, version: i16) -> usize {
502 let flex = version >= 6;
503 let mut n: usize = 0;
504 if version >= 8 {
505 n += if flex {
506 compact_string_len(self.group_id)
507 } else {
508 string_len(self.group_id)
509 };
510 }
511 if version >= 8 {
512 n += {
513 let prefix =
514 crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
515 let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
516 prefix + body
517 };
518 }
519 if version >= 8 {
520 n += 2;
521 }
522 if flex {
523 let known_pairs: Vec<(u32, usize)> = Vec::new();
524 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
525 }
526 n
527 }
528}
529impl<'de> DecodeBorrow<'de> for OffsetFetchResponseGroup<'de> {
530 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
531 let flex = version >= 6;
532 let mut out = Self::default();
533 if version >= 8 {
534 out.group_id = if flex {
535 get_compact_string_borrowed(buf)?
536 } else {
537 get_string_borrowed(buf)?
538 };
539 }
540 if version >= 8 {
541 out.topics = {
542 let n = crate::primitives::array::get_array_len(buf, flex)?;
543 let mut v = Vec::with_capacity(n);
544 for _ in 0..n {
545 v.push(OffsetFetchResponseTopics::decode_borrow(buf, version)?);
546 }
547 v
548 };
549 }
550 if version >= 8 {
551 out.error_code = get_i16(buf)?;
552 }
553 if flex {
554 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
555 }
556 Ok(out)
557 }
558}
559#[cfg(test)]
560impl OffsetFetchResponseGroup<'_> {
561 #[must_use]
562 pub fn populated(version: i16) -> Self {
563 let mut m = Self::default();
564 if version >= 8 {
565 m.group_id = "x";
566 }
567 if version >= 8 {
568 m.topics = vec![OffsetFetchResponseTopics::populated(version)];
569 }
570 if version >= 8 {
571 m.error_code = 1i16;
572 }
573 m
574 }
575}
576#[derive(Debug, Clone, PartialEq, Eq, Default)]
577pub struct OffsetFetchResponseTopics<'a> {
578 pub name: &'a str,
579 pub topic_id: crate::primitives::uuid::Uuid,
580 pub partitions: Vec<OffsetFetchResponsePartitions<'a>>,
581 pub unknown_tagged_fields: UnknownTaggedFields,
582}
583impl OffsetFetchResponseTopics<'_> {
584 #[must_use]
588 pub fn to_owned(&self) -> crate::owned::offset_fetch_response::OffsetFetchResponseTopics {
589 crate::owned::offset_fetch_response::OffsetFetchResponseTopics {
590 name: (self.name).to_string(),
591 topic_id: (self.topic_id),
592 partitions: (self.partitions)
593 .iter()
594 .map(OffsetFetchResponsePartitions::to_owned)
595 .collect(),
596 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
597 }
598 }
599}
600impl Encode for OffsetFetchResponseTopics<'_> {
601 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
602 let flex = version >= 6;
603 if (8..=9).contains(&version) {
604 if flex {
605 let () = put_compact_string(buf, self.name);
606 } else {
607 let () = put_string(buf, self.name);
608 }
609 }
610 if version >= 10 {
611 crate::primitives::uuid::put_uuid(buf, self.topic_id);
612 }
613 if version >= 8 {
614 {
615 crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
616 for it in &self.partitions {
617 it.encode(buf, version)?;
618 }
619 };
620 }
621 if flex {
622 let tagged = WriteTaggedFields::new();
623 tagged.write(buf, &self.unknown_tagged_fields);
624 }
625 Ok(())
626 }
627 fn encoded_len(&self, version: i16) -> usize {
628 let flex = version >= 6;
629 let mut n: usize = 0;
630 if (8..=9).contains(&version) {
631 n += if flex {
632 compact_string_len(self.name)
633 } else {
634 string_len(self.name)
635 };
636 }
637 if version >= 10 {
638 n += 16;
639 }
640 if version >= 8 {
641 n += {
642 let prefix =
643 crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
644 let body: usize = (self.partitions)
645 .iter()
646 .map(|it| it.encoded_len(version))
647 .sum();
648 prefix + body
649 };
650 }
651 if flex {
652 let known_pairs: Vec<(u32, usize)> = Vec::new();
653 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
654 }
655 n
656 }
657}
658impl<'de> DecodeBorrow<'de> for OffsetFetchResponseTopics<'de> {
659 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
660 let flex = version >= 6;
661 let mut out = Self::default();
662 if (8..=9).contains(&version) {
663 out.name = if flex {
664 get_compact_string_borrowed(buf)?
665 } else {
666 get_string_borrowed(buf)?
667 };
668 }
669 if version >= 10 {
670 out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
671 }
672 if version >= 8 {
673 out.partitions = {
674 let n = crate::primitives::array::get_array_len(buf, flex)?;
675 let mut v = Vec::with_capacity(n);
676 for _ in 0..n {
677 v.push(OffsetFetchResponsePartitions::decode_borrow(buf, version)?);
678 }
679 v
680 };
681 }
682 if flex {
683 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
684 }
685 Ok(out)
686 }
687}
688#[cfg(test)]
689impl OffsetFetchResponseTopics<'_> {
690 #[must_use]
691 pub fn populated(version: i16) -> Self {
692 let mut m = Self::default();
693 if (8..=9).contains(&version) {
694 m.name = "x";
695 }
696 if version >= 10 {
697 m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
698 }
699 if version >= 8 {
700 m.partitions = vec![OffsetFetchResponsePartitions::populated(version)];
701 }
702 m
703 }
704}
705#[derive(Debug, Clone, PartialEq, Eq)]
706pub struct OffsetFetchResponsePartitions<'a> {
707 pub partition_index: i32,
708 pub committed_offset: i64,
709 pub committed_leader_epoch: i32,
710 pub metadata: Option<&'a str>,
711 pub error_code: i16,
712 pub unknown_tagged_fields: UnknownTaggedFields,
713}
714impl Default for OffsetFetchResponsePartitions<'_> {
715 fn default() -> Self {
716 Self {
717 partition_index: 0i32,
718 committed_offset: 0i64,
719 committed_leader_epoch: -1i32,
720 metadata: Some(""),
721 error_code: 0i16,
722 unknown_tagged_fields: UnknownTaggedFields::default(),
723 }
724 }
725}
726impl OffsetFetchResponsePartitions<'_> {
727 #[must_use]
731 pub fn to_owned(&self) -> crate::owned::offset_fetch_response::OffsetFetchResponsePartitions {
732 crate::owned::offset_fetch_response::OffsetFetchResponsePartitions {
733 partition_index: (self.partition_index),
734 committed_offset: (self.committed_offset),
735 committed_leader_epoch: (self.committed_leader_epoch),
736 metadata: (self.metadata).map(std::string::ToString::to_string),
737 error_code: (self.error_code),
738 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
739 }
740 }
741}
742impl Encode for OffsetFetchResponsePartitions<'_> {
743 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
744 let flex = version >= 6;
745 if version >= 8 {
746 put_i32(buf, self.partition_index);
747 }
748 if version >= 8 {
749 put_i64(buf, self.committed_offset);
750 }
751 if version >= 8 {
752 put_i32(buf, self.committed_leader_epoch);
753 }
754 if version >= 8 {
755 if flex {
756 let () = put_compact_nullable_string(buf, self.metadata);
757 } else {
758 let () = put_nullable_string(buf, self.metadata);
759 }
760 }
761 if version >= 8 {
762 put_i16(buf, self.error_code);
763 }
764 if flex {
765 let tagged = WriteTaggedFields::new();
766 tagged.write(buf, &self.unknown_tagged_fields);
767 }
768 Ok(())
769 }
770 fn encoded_len(&self, version: i16) -> usize {
771 let flex = version >= 6;
772 let mut n: usize = 0;
773 if version >= 8 {
774 n += 4;
775 }
776 if version >= 8 {
777 n += 8;
778 }
779 if version >= 8 {
780 n += 4;
781 }
782 if version >= 8 {
783 n += if flex {
784 compact_nullable_string_len(self.metadata)
785 } else {
786 nullable_string_len(self.metadata)
787 };
788 }
789 if version >= 8 {
790 n += 2;
791 }
792 if flex {
793 let known_pairs: Vec<(u32, usize)> = Vec::new();
794 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
795 }
796 n
797 }
798}
799impl<'de> DecodeBorrow<'de> for OffsetFetchResponsePartitions<'de> {
800 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
801 let flex = version >= 6;
802 let mut out = Self::default();
803 if version >= 8 {
804 out.partition_index = get_i32(buf)?;
805 }
806 if version >= 8 {
807 out.committed_offset = get_i64(buf)?;
808 }
809 if version >= 8 {
810 out.committed_leader_epoch = get_i32(buf)?;
811 }
812 if version >= 8 {
813 out.metadata = if flex {
814 get_compact_nullable_string_borrowed(buf)?
815 } else {
816 get_nullable_string_borrowed(buf)?
817 };
818 }
819 if version >= 8 {
820 out.error_code = get_i16(buf)?;
821 }
822 if flex {
823 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
824 }
825 Ok(out)
826 }
827}
828#[cfg(test)]
829impl OffsetFetchResponsePartitions<'_> {
830 #[must_use]
831 pub fn populated(version: i16) -> Self {
832 let mut m = Self::default();
833 if version >= 8 {
834 m.partition_index = 1i32;
835 }
836 if version >= 8 {
837 m.committed_offset = 1i64;
838 }
839 if version >= 8 {
840 m.committed_leader_epoch = 1i32;
841 }
842 if version >= 8 {
843 m.metadata = Some("x");
844 }
845 if version >= 8 {
846 m.error_code = 1i16;
847 }
848 m
849 }
850}