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