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