1#![allow(
3 missing_docs,
4 clippy::all,
5 clippy::pedantic,
6 clippy::nursery,
7 clippy::arithmetic_side_effects,
8 reason = "Generated protocol modules mirror Kafka's schema shape and intentionally trade \
9 hand-written lint style for reproducible wire-code output."
10)]
11use bytes::{Bytes, BytesMut};
12
13use crate::*;
14
15#[derive(Debug, Clone, PartialEq)]
16pub struct FetchRequestData {
17 pub cluster_id: Option<KafkaString>,
20 pub replica_id: i32,
22 pub replica_state: ReplicaState,
24 pub max_wait_ms: i32,
26 pub min_bytes: i32,
28 pub max_bytes: i32,
30 pub isolation_level: i8,
37 pub session_id: i32,
39 pub session_epoch: i32,
41 pub topics: Vec<FetchTopic>,
43 pub forgotten_topics_data: Vec<ForgottenTopic>,
45 pub rack_id: KafkaString,
47 pub _unknown_tagged_fields: Vec<RawTaggedField>,
48}
49impl Default for FetchRequestData {
50 fn default() -> Self {
51 Self {
52 cluster_id: None,
53 replica_id: -1i32,
54 replica_state: ReplicaState::default(),
55 max_wait_ms: 0_i32,
56 min_bytes: 0_i32,
57 max_bytes: i32::MAX,
58 isolation_level: 0i8,
59 session_id: 0i32,
60 session_epoch: -1i32,
61 topics: Vec::new(),
62 forgotten_topics_data: Vec::new(),
63 rack_id: KafkaString::default(),
64 _unknown_tagged_fields: Vec::new(),
65 }
66 }
67}
68impl FetchRequestData {
69 pub fn with_cluster_id(mut self, value: Option<KafkaString>) -> Self {
70 self.cluster_id = value;
71 self
72 }
73 pub fn with_replica_id(mut self, value: i32) -> Self {
74 self.replica_id = value;
75 self
76 }
77 pub fn with_replica_state(mut self, value: ReplicaState) -> Self {
78 self.replica_state = value;
79 self
80 }
81 pub fn with_max_wait_ms(mut self, value: i32) -> Self {
82 self.max_wait_ms = value;
83 self
84 }
85 pub fn with_min_bytes(mut self, value: i32) -> Self {
86 self.min_bytes = value;
87 self
88 }
89 pub fn with_max_bytes(mut self, value: i32) -> Self {
90 self.max_bytes = value;
91 self
92 }
93 pub fn with_isolation_level(mut self, value: i8) -> Self {
94 self.isolation_level = value;
95 self
96 }
97 pub fn with_session_id(mut self, value: i32) -> Self {
98 self.session_id = value;
99 self
100 }
101 pub fn with_session_epoch(mut self, value: i32) -> Self {
102 self.session_epoch = value;
103 self
104 }
105 pub fn with_topics(mut self, value: Vec<FetchTopic>) -> Self {
106 self.topics = value;
107 self
108 }
109 pub fn with_forgotten_topics_data(mut self, value: Vec<ForgottenTopic>) -> Self {
110 self.forgotten_topics_data = value;
111 self
112 }
113 pub fn with_rack_id(mut self, value: KafkaString) -> Self {
114 self.rack_id = value;
115 self
116 }
117 pub fn read(buf: &mut Bytes, version: i16) -> Result<Self> {
118 if version < 4 || version > 18 {
119 return Err(UnsupportedVersion::new(1, version).into());
120 }
121 let mut cluster_id = None;
122 let mut replica_id = -1i32;
123 let mut replica_state = ReplicaState::default();
124 let max_wait_ms;
125 let min_bytes;
126 let max_bytes;
127 let isolation_level;
128 let mut session_id = 0i32;
129 let mut session_epoch = -1i32;
130 let topics;
131 let mut forgotten_topics_data = Vec::new();
132 let mut rack_id = KafkaString::default();
133 let mut _unknown_tagged_fields: Vec<RawTaggedField> = Vec::new();
134 if version <= 14 {
135 replica_id = read_i32(buf)?;
136 }
137 max_wait_ms = read_i32(buf)?;
138 min_bytes = read_i32(buf)?;
139 max_bytes = read_i32(buf)?;
140 isolation_level = read_i8(buf)?;
141 if version >= 7 {
142 session_id = read_i32(buf)?;
143 }
144 if version >= 7 {
145 session_epoch = read_i32(buf)?;
146 }
147 if version >= 12 {
148 topics = {
149 let len = read_compact_array_length(buf)?;
150 let mut arr = Vec::with_capacity(array_read_capacity(len, (buf).len()));
151 for _ in 0..len {
152 arr.push(FetchTopic::read(buf, version)?);
153 }
154 arr
155 };
156 } else {
157 topics = {
158 let len = read_array_length(buf)?;
159 let mut arr = Vec::with_capacity(array_read_capacity(len, (buf).len()));
160 for _ in 0..len {
161 arr.push(FetchTopic::read(buf, version)?);
162 }
163 arr
164 };
165 }
166 if version >= 7 {
167 if version >= 12 {
168 forgotten_topics_data = {
169 let len = read_compact_array_length(buf)?;
170 let mut arr = Vec::with_capacity(array_read_capacity(len, (buf).len()));
171 for _ in 0..len {
172 arr.push(ForgottenTopic::read(buf, version)?);
173 }
174 arr
175 };
176 } else {
177 forgotten_topics_data = {
178 let len = read_array_length(buf)?;
179 let mut arr = Vec::with_capacity(array_read_capacity(len, (buf).len()));
180 for _ in 0..len {
181 arr.push(ForgottenTopic::read(buf, version)?);
182 }
183 arr
184 };
185 }
186 }
187 if version >= 11 {
188 if version >= 12 {
189 rack_id = read_compact_string(buf)?;
190 } else {
191 rack_id = read_string(buf)?;
192 }
193 }
194 if version >= 12 {
195 let tagged_fields = read_tagged_fields(buf)?;
196 for field in &tagged_fields {
197 match field.tag {
198 0 => {
199 let mut tag_buf = field.data.clone();
200 cluster_id = read_compact_nullable_string(&mut tag_buf)?;
201 },
202 1 => {
203 if version >= 15 {
204 let mut tag_buf = field.data.clone();
205 replica_state = ReplicaState::read(&mut tag_buf, version)?;
206 }
207 },
208 _ => {
209 _unknown_tagged_fields.push(field.clone());
210 },
211 }
212 }
213 }
214 Ok(Self {
215 cluster_id,
216 replica_id,
217 replica_state,
218 max_wait_ms,
219 min_bytes,
220 max_bytes,
221 isolation_level,
222 session_id,
223 session_epoch,
224 topics,
225 forgotten_topics_data,
226 rack_id,
227 _unknown_tagged_fields,
228 })
229 }
230 pub fn write(&self, buf: &mut BytesMut, version: i16) -> Result<()> {
231 if version < 4 || version > 18 {
232 return Err(UnsupportedVersion::new(1, version).into());
233 }
234 if version <= 14 {
235 write_i32(buf, self.replica_id);
236 } else if self.replica_id != -1i32 {
237 return Err(UnsupportedFieldVersion::new(1, "replica_id", version).into());
238 }
239 write_i32(buf, self.max_wait_ms);
240 write_i32(buf, self.min_bytes);
241 write_i32(buf, self.max_bytes);
242 write_i8(buf, self.isolation_level);
243 if version >= 7 {
244 write_i32(buf, self.session_id);
245 } else if self.session_id != 0i32 {
246 return Err(UnsupportedFieldVersion::new(1, "session_id", version).into());
247 }
248 if version >= 7 {
249 write_i32(buf, self.session_epoch);
250 } else if self.session_epoch != -1i32 {
251 return Err(UnsupportedFieldVersion::new(1, "session_epoch", version).into());
252 }
253 if version >= 12 {
254 write_compact_array_length(buf, self.topics.len() as i32);
255 for el in &self.topics {
256 el.write(buf, version)?;
257 }
258 } else {
259 write_array_length(buf, self.topics.len() as i32);
260 for el in &self.topics {
261 el.write(buf, version)?;
262 }
263 }
264 if version >= 7 {
265 if version >= 12 {
266 write_compact_array_length(buf, self.forgotten_topics_data.len() as i32);
267 for el in &self.forgotten_topics_data {
268 el.write(buf, version)?;
269 }
270 } else {
271 write_array_length(buf, self.forgotten_topics_data.len() as i32);
272 for el in &self.forgotten_topics_data {
273 el.write(buf, version)?;
274 }
275 }
276 } else if self.forgotten_topics_data != Vec::new() {
277 return Err(UnsupportedFieldVersion::new(1, "forgotten_topics_data", version).into());
278 }
279 if version >= 11 {
280 if version >= 12 {
281 write_compact_string(buf, &self.rack_id)?;
282 } else {
283 write_string(buf, &self.rack_id)?;
284 }
285 } else if self.rack_id != KafkaString::default() {
286 return Err(UnsupportedFieldVersion::new(1, "rack_id", version).into());
287 }
288 if version >= 12 {
289 let mut known_tagged_fields: Vec<RawTaggedField> = Vec::new();
290 if self.cluster_id.is_some() {
291 let mut tag_buf = BytesMut::new();
292 write_compact_nullable_string(&mut tag_buf, self.cluster_id.as_ref())?;
293 known_tagged_fields.push(RawTaggedField {
294 tag: 0,
295 data: tag_buf.freeze(),
296 });
297 }
298 if version >= 15 && self.replica_state != ReplicaState::default() {
299 let mut tag_buf = BytesMut::new();
300 self.replica_state.write(&mut tag_buf, version)?;
301 known_tagged_fields.push(RawTaggedField {
302 tag: 1,
303 data: tag_buf.freeze(),
304 });
305 }
306 let mut all_tags = known_tagged_fields;
307 all_tags.extend(self._unknown_tagged_fields.iter().cloned());
308 all_tags.sort_by_key(|f| f.tag);
309 write_tagged_fields(buf, &all_tags)?;
310 }
311 Ok(())
312 }
313 pub fn encoded_len(&self, version: i16) -> Result<usize> {
314 if version < 4 || version > 18 {
315 return Err(UnsupportedVersion::new(1, version).into());
316 }
317 let mut len: usize = 0;
318 if version <= 14 {
319 len += 4;
320 } else if self.replica_id != -1i32 {
321 return Err(UnsupportedFieldVersion::new(1, "replica_id", version).into());
322 }
323 len += 4;
324 len += 4;
325 len += 4;
326 len += 1;
327 if version >= 7 {
328 len += 4;
329 } else if self.session_id != 0i32 {
330 return Err(UnsupportedFieldVersion::new(1, "session_id", version).into());
331 }
332 if version >= 7 {
333 len += 4;
334 } else if self.session_epoch != -1i32 {
335 return Err(UnsupportedFieldVersion::new(1, "session_epoch", version).into());
336 }
337 if version >= 12 {
338 len += compact_array_length_len(self.topics.len() as i32);
339 for el in &self.topics {
340 len += el.encoded_len(version)?;
341 }
342 } else {
343 len += array_length_len();
344 for el in &self.topics {
345 len += el.encoded_len(version)?;
346 }
347 }
348 if version >= 7 {
349 if version >= 12 {
350 len += compact_array_length_len(self.forgotten_topics_data.len() as i32);
351 for el in &self.forgotten_topics_data {
352 len += el.encoded_len(version)?;
353 }
354 } else {
355 len += array_length_len();
356 for el in &self.forgotten_topics_data {
357 len += el.encoded_len(version)?;
358 }
359 }
360 } else if self.forgotten_topics_data != Vec::new() {
361 return Err(UnsupportedFieldVersion::new(1, "forgotten_topics_data", version).into());
362 }
363 if version >= 11 {
364 if version >= 12 {
365 len += compact_string_len(&self.rack_id)?;
366 } else {
367 len += string_len(&self.rack_id)?;
368 }
369 } else if self.rack_id != KafkaString::default() {
370 return Err(UnsupportedFieldVersion::new(1, "rack_id", version).into());
371 }
372 if version >= 12 {
373 let mut known_tagged_fields: Vec<RawTaggedField> = Vec::new();
374 if self.cluster_id.is_some() {
375 let mut tag_buf = BytesMut::new();
376 write_compact_nullable_string(&mut tag_buf, self.cluster_id.as_ref())?;
377 known_tagged_fields.push(RawTaggedField {
378 tag: 0,
379 data: tag_buf.freeze(),
380 });
381 }
382 if version >= 15 && self.replica_state != ReplicaState::default() {
383 let mut tag_buf = BytesMut::new();
384 self.replica_state.write(&mut tag_buf, version)?;
385 known_tagged_fields.push(RawTaggedField {
386 tag: 1,
387 data: tag_buf.freeze(),
388 });
389 }
390 let mut all_tags = known_tagged_fields;
391 all_tags.extend(self._unknown_tagged_fields.iter().cloned());
392 all_tags.sort_by_key(|f| f.tag);
393 len += tagged_fields_len(&all_tags)?;
394 }
395 Ok(len)
396 }
397}
398#[derive(Debug, Clone, PartialEq)]
399pub struct ReplicaState {
400 pub replica_id: i32,
402 pub replica_epoch: i64,
404 pub _unknown_tagged_fields: Vec<RawTaggedField>,
405}
406impl Default for ReplicaState {
407 fn default() -> Self {
408 Self {
409 replica_id: -1i32,
410 replica_epoch: -1i64,
411 _unknown_tagged_fields: Vec::new(),
412 }
413 }
414}
415impl ReplicaState {
416 pub fn with_replica_id(mut self, value: i32) -> Self {
417 self.replica_id = value;
418 self
419 }
420 pub fn with_replica_epoch(mut self, value: i64) -> Self {
421 self.replica_epoch = value;
422 self
423 }
424 pub fn read(buf: &mut Bytes, _version: i16) -> Result<Self> {
425 let replica_id;
426 let replica_epoch;
427 let mut _unknown_tagged_fields: Vec<RawTaggedField> = Vec::new();
428 replica_id = read_i32(buf)?;
429 replica_epoch = read_i64(buf)?;
430 let tagged_fields = read_tagged_fields(buf)?;
431 for field in &tagged_fields {
432 match field.tag {
433 _ => {
434 _unknown_tagged_fields.push(field.clone());
435 },
436 }
437 }
438 Ok(Self {
439 replica_id,
440 replica_epoch,
441 _unknown_tagged_fields,
442 })
443 }
444 pub fn write(&self, buf: &mut BytesMut, _version: i16) -> Result<()> {
445 write_i32(buf, self.replica_id);
446 write_i64(buf, self.replica_epoch);
447 let mut all_tags: Vec<RawTaggedField> = self._unknown_tagged_fields.clone();
448 all_tags.sort_by_key(|f| f.tag);
449 write_tagged_fields(buf, &all_tags)?;
450 Ok(())
451 }
452 pub fn encoded_len(&self, _version: i16) -> Result<usize> {
453 let mut len: usize = 0;
454 len += 4;
455 len += 8;
456 let mut all_tags: Vec<RawTaggedField> = self._unknown_tagged_fields.clone();
457 all_tags.sort_by_key(|f| f.tag);
458 len += tagged_fields_len(&all_tags)?;
459 Ok(len)
460 }
461}
462#[derive(Debug, Clone, PartialEq)]
463pub struct FetchTopic {
464 pub topic: KafkaString,
466 pub topic_id: KafkaUuid,
468 pub partitions: Vec<FetchPartition>,
470 pub _unknown_tagged_fields: Vec<RawTaggedField>,
471}
472impl Default for FetchTopic {
473 fn default() -> Self {
474 Self {
475 topic: KafkaString::default(),
476 topic_id: KafkaUuid::ZERO,
477 partitions: Vec::new(),
478 _unknown_tagged_fields: Vec::new(),
479 }
480 }
481}
482impl FetchTopic {
483 pub fn with_topic(mut self, value: KafkaString) -> Self {
484 self.topic = value;
485 self
486 }
487 pub fn with_topic_id(mut self, value: KafkaUuid) -> Self {
488 self.topic_id = value;
489 self
490 }
491 pub fn with_partitions(mut self, value: Vec<FetchPartition>) -> Self {
492 self.partitions = value;
493 self
494 }
495 pub fn read(buf: &mut Bytes, version: i16) -> Result<Self> {
496 let mut topic = KafkaString::default();
497 let mut topic_id = KafkaUuid::ZERO;
498 let partitions;
499 let mut _unknown_tagged_fields: Vec<RawTaggedField> = Vec::new();
500 if version <= 12 {
501 if version >= 12 {
502 topic = read_compact_string(buf)?;
503 } else {
504 topic = read_string(buf)?;
505 }
506 }
507 if version >= 13 {
508 topic_id = read_uuid(buf)?;
509 }
510 if version >= 12 {
511 partitions = {
512 let len = read_compact_array_length(buf)?;
513 let mut arr = Vec::with_capacity(array_read_capacity(len, (buf).len()));
514 for _ in 0..len {
515 arr.push(FetchPartition::read(buf, version)?);
516 }
517 arr
518 };
519 } else {
520 partitions = {
521 let len = read_array_length(buf)?;
522 let mut arr = Vec::with_capacity(array_read_capacity(len, (buf).len()));
523 for _ in 0..len {
524 arr.push(FetchPartition::read(buf, version)?);
525 }
526 arr
527 };
528 }
529 if version >= 12 {
530 let tagged_fields = read_tagged_fields(buf)?;
531 for field in &tagged_fields {
532 match field.tag {
533 _ => {
534 _unknown_tagged_fields.push(field.clone());
535 },
536 }
537 }
538 }
539 Ok(Self {
540 topic,
541 topic_id,
542 partitions,
543 _unknown_tagged_fields,
544 })
545 }
546 pub fn write(&self, buf: &mut BytesMut, version: i16) -> Result<()> {
547 if version <= 12 {
548 if version >= 12 {
549 write_compact_string(buf, &self.topic)?;
550 } else {
551 write_string(buf, &self.topic)?;
552 }
553 } else if self.topic != KafkaString::default() {
554 return Err(UnsupportedFieldVersion::new(1, "topic", version).into());
555 }
556 if version >= 13 {
557 write_uuid(buf, &self.topic_id);
558 } else if self.topic_id != KafkaUuid::ZERO {
559 return Err(UnsupportedFieldVersion::new(1, "topic_id", version).into());
560 }
561 if version >= 12 {
562 write_compact_array_length(buf, self.partitions.len() as i32);
563 for el in &self.partitions {
564 el.write(buf, version)?;
565 }
566 } else {
567 write_array_length(buf, self.partitions.len() as i32);
568 for el in &self.partitions {
569 el.write(buf, version)?;
570 }
571 }
572 if version >= 12 {
573 let mut all_tags: Vec<RawTaggedField> = self._unknown_tagged_fields.clone();
574 all_tags.sort_by_key(|f| f.tag);
575 write_tagged_fields(buf, &all_tags)?;
576 }
577 Ok(())
578 }
579 pub fn encoded_len(&self, version: i16) -> Result<usize> {
580 let mut len: usize = 0;
581 if version <= 12 {
582 if version >= 12 {
583 len += compact_string_len(&self.topic)?;
584 } else {
585 len += string_len(&self.topic)?;
586 }
587 } else if self.topic != KafkaString::default() {
588 return Err(UnsupportedFieldVersion::new(1, "topic", version).into());
589 }
590 if version >= 13 {
591 len += 16;
592 } else if self.topic_id != KafkaUuid::ZERO {
593 return Err(UnsupportedFieldVersion::new(1, "topic_id", version).into());
594 }
595 if version >= 12 {
596 len += compact_array_length_len(self.partitions.len() as i32);
597 for el in &self.partitions {
598 len += el.encoded_len(version)?;
599 }
600 } else {
601 len += array_length_len();
602 for el in &self.partitions {
603 len += el.encoded_len(version)?;
604 }
605 }
606 if version >= 12 {
607 let mut all_tags: Vec<RawTaggedField> = self._unknown_tagged_fields.clone();
608 all_tags.sort_by_key(|f| f.tag);
609 len += tagged_fields_len(&all_tags)?;
610 }
611 Ok(len)
612 }
613}
614#[derive(Debug, Clone, PartialEq)]
615pub struct FetchPartition {
616 pub partition: i32,
618 pub current_leader_epoch: i32,
620 pub fetch_offset: i64,
622 pub last_fetched_epoch: i32,
624 pub log_start_offset: i64,
627 pub partition_max_bytes: i32,
630 pub replica_directory_id: KafkaUuid,
632 pub high_watermark: i64,
635 pub _unknown_tagged_fields: Vec<RawTaggedField>,
636}
637impl Default for FetchPartition {
638 fn default() -> Self {
639 Self {
640 partition: 0_i32,
641 current_leader_epoch: -1i32,
642 fetch_offset: 0_i64,
643 last_fetched_epoch: -1i32,
644 log_start_offset: -1i64,
645 partition_max_bytes: 0_i32,
646 replica_directory_id: KafkaUuid::ZERO,
647 high_watermark: i64::MAX,
648 _unknown_tagged_fields: Vec::new(),
649 }
650 }
651}
652impl FetchPartition {
653 pub fn with_partition(mut self, value: i32) -> Self {
654 self.partition = value;
655 self
656 }
657 pub fn with_current_leader_epoch(mut self, value: i32) -> Self {
658 self.current_leader_epoch = value;
659 self
660 }
661 pub fn with_fetch_offset(mut self, value: i64) -> Self {
662 self.fetch_offset = value;
663 self
664 }
665 pub fn with_last_fetched_epoch(mut self, value: i32) -> Self {
666 self.last_fetched_epoch = value;
667 self
668 }
669 pub fn with_log_start_offset(mut self, value: i64) -> Self {
670 self.log_start_offset = value;
671 self
672 }
673 pub fn with_partition_max_bytes(mut self, value: i32) -> Self {
674 self.partition_max_bytes = value;
675 self
676 }
677 pub fn with_replica_directory_id(mut self, value: KafkaUuid) -> Self {
678 self.replica_directory_id = value;
679 self
680 }
681 pub fn with_high_watermark(mut self, value: i64) -> Self {
682 self.high_watermark = value;
683 self
684 }
685 pub fn read(buf: &mut Bytes, version: i16) -> Result<Self> {
686 let partition;
687 let mut current_leader_epoch = -1i32;
688 let fetch_offset;
689 let mut last_fetched_epoch = -1i32;
690 let mut log_start_offset = -1i64;
691 let partition_max_bytes;
692 let mut replica_directory_id = KafkaUuid::ZERO;
693 let mut high_watermark = i64::MAX;
694 let mut _unknown_tagged_fields: Vec<RawTaggedField> = Vec::new();
695 partition = read_i32(buf)?;
696 if version >= 9 {
697 current_leader_epoch = read_i32(buf)?;
698 }
699 fetch_offset = read_i64(buf)?;
700 if version >= 12 {
701 last_fetched_epoch = read_i32(buf)?;
702 }
703 if version >= 5 {
704 log_start_offset = read_i64(buf)?;
705 }
706 partition_max_bytes = read_i32(buf)?;
707 if version >= 12 {
708 let tagged_fields = read_tagged_fields(buf)?;
709 for field in &tagged_fields {
710 match field.tag {
711 0 => {
712 if version >= 17 {
713 let mut tag_buf = field.data.clone();
714 replica_directory_id = read_uuid(&mut tag_buf)?;
715 }
716 },
717 1 => {
718 if version >= 18 {
719 let mut tag_buf = field.data.clone();
720 high_watermark = read_i64(&mut tag_buf)?;
721 }
722 },
723 _ => {
724 _unknown_tagged_fields.push(field.clone());
725 },
726 }
727 }
728 }
729 Ok(Self {
730 partition,
731 current_leader_epoch,
732 fetch_offset,
733 last_fetched_epoch,
734 log_start_offset,
735 partition_max_bytes,
736 replica_directory_id,
737 high_watermark,
738 _unknown_tagged_fields,
739 })
740 }
741 pub fn write(&self, buf: &mut BytesMut, version: i16) -> Result<()> {
742 write_i32(buf, self.partition);
743 if version >= 9 {
744 write_i32(buf, self.current_leader_epoch);
745 } else if self.current_leader_epoch != -1i32 {
746 return Err(UnsupportedFieldVersion::new(1, "current_leader_epoch", version).into());
747 }
748 write_i64(buf, self.fetch_offset);
749 if version >= 12 {
750 write_i32(buf, self.last_fetched_epoch);
751 } else if self.last_fetched_epoch != -1i32 {
752 return Err(UnsupportedFieldVersion::new(1, "last_fetched_epoch", version).into());
753 }
754 if version >= 5 {
755 write_i64(buf, self.log_start_offset);
756 } else if self.log_start_offset != -1i64 {
757 return Err(UnsupportedFieldVersion::new(1, "log_start_offset", version).into());
758 }
759 write_i32(buf, self.partition_max_bytes);
760 if version >= 12 {
761 let mut known_tagged_fields: Vec<RawTaggedField> = Vec::new();
762 if version >= 17 && !self.replica_directory_id.is_nil() {
763 let mut tag_buf = BytesMut::new();
764 write_uuid(&mut tag_buf, &self.replica_directory_id);
765 known_tagged_fields.push(RawTaggedField {
766 tag: 0,
767 data: tag_buf.freeze(),
768 });
769 }
770 if version >= 18 && self.high_watermark != 9223372036854775807_i64 {
771 let mut tag_buf = BytesMut::new();
772 write_i64(&mut tag_buf, self.high_watermark);
773 known_tagged_fields.push(RawTaggedField {
774 tag: 1,
775 data: tag_buf.freeze(),
776 });
777 }
778 let mut all_tags = known_tagged_fields;
779 all_tags.extend(self._unknown_tagged_fields.iter().cloned());
780 all_tags.sort_by_key(|f| f.tag);
781 write_tagged_fields(buf, &all_tags)?;
782 }
783 Ok(())
784 }
785 pub fn encoded_len(&self, version: i16) -> Result<usize> {
786 let mut len: usize = 0;
787 len += 4;
788 if version >= 9 {
789 len += 4;
790 } else if self.current_leader_epoch != -1i32 {
791 return Err(UnsupportedFieldVersion::new(1, "current_leader_epoch", version).into());
792 }
793 len += 8;
794 if version >= 12 {
795 len += 4;
796 } else if self.last_fetched_epoch != -1i32 {
797 return Err(UnsupportedFieldVersion::new(1, "last_fetched_epoch", version).into());
798 }
799 if version >= 5 {
800 len += 8;
801 } else if self.log_start_offset != -1i64 {
802 return Err(UnsupportedFieldVersion::new(1, "log_start_offset", version).into());
803 }
804 len += 4;
805 if version >= 12 {
806 let mut known_tagged_fields: Vec<RawTaggedField> = Vec::new();
807 if version >= 17 && !self.replica_directory_id.is_nil() {
808 let mut tag_buf = BytesMut::new();
809 write_uuid(&mut tag_buf, &self.replica_directory_id);
810 known_tagged_fields.push(RawTaggedField {
811 tag: 0,
812 data: tag_buf.freeze(),
813 });
814 }
815 if version >= 18 && self.high_watermark != 9223372036854775807_i64 {
816 let mut tag_buf = BytesMut::new();
817 write_i64(&mut tag_buf, self.high_watermark);
818 known_tagged_fields.push(RawTaggedField {
819 tag: 1,
820 data: tag_buf.freeze(),
821 });
822 }
823 let mut all_tags = known_tagged_fields;
824 all_tags.extend(self._unknown_tagged_fields.iter().cloned());
825 all_tags.sort_by_key(|f| f.tag);
826 len += tagged_fields_len(&all_tags)?;
827 }
828 Ok(len)
829 }
830}
831#[derive(Debug, Clone, PartialEq)]
832pub struct ForgottenTopic {
833 pub topic: KafkaString,
835 pub topic_id: KafkaUuid,
837 pub partitions: Vec<i32>,
839 pub _unknown_tagged_fields: Vec<RawTaggedField>,
840}
841impl Default for ForgottenTopic {
842 fn default() -> Self {
843 Self {
844 topic: KafkaString::default(),
845 topic_id: KafkaUuid::ZERO,
846 partitions: Vec::new(),
847 _unknown_tagged_fields: Vec::new(),
848 }
849 }
850}
851impl ForgottenTopic {
852 pub fn with_topic(mut self, value: KafkaString) -> Self {
853 self.topic = value;
854 self
855 }
856 pub fn with_topic_id(mut self, value: KafkaUuid) -> Self {
857 self.topic_id = value;
858 self
859 }
860 pub fn with_partitions(mut self, value: Vec<i32>) -> Self {
861 self.partitions = value;
862 self
863 }
864 pub fn read(buf: &mut Bytes, version: i16) -> Result<Self> {
865 let mut topic = KafkaString::default();
866 let mut topic_id = KafkaUuid::ZERO;
867 let partitions;
868 let mut _unknown_tagged_fields: Vec<RawTaggedField> = Vec::new();
869 if version <= 12 {
870 if version >= 12 {
871 topic = read_compact_string(buf)?;
872 } else {
873 topic = read_string(buf)?;
874 }
875 }
876 if version >= 13 {
877 topic_id = read_uuid(buf)?;
878 }
879 if version >= 12 {
880 partitions = {
881 let len = read_compact_array_length(buf)?;
882 let mut arr = Vec::with_capacity(array_read_capacity(len, (buf).len()));
883 for _ in 0..len {
884 arr.push(read_i32(buf)?);
885 }
886 arr
887 };
888 } else {
889 partitions = {
890 let len = read_array_length(buf)?;
891 let mut arr = Vec::with_capacity(array_read_capacity(len, (buf).len()));
892 for _ in 0..len {
893 arr.push(read_i32(buf)?);
894 }
895 arr
896 };
897 }
898 if version >= 12 {
899 let tagged_fields = read_tagged_fields(buf)?;
900 for field in &tagged_fields {
901 match field.tag {
902 _ => {
903 _unknown_tagged_fields.push(field.clone());
904 },
905 }
906 }
907 }
908 Ok(Self {
909 topic,
910 topic_id,
911 partitions,
912 _unknown_tagged_fields,
913 })
914 }
915 pub fn write(&self, buf: &mut BytesMut, version: i16) -> Result<()> {
916 if version <= 12 {
917 if version >= 12 {
918 write_compact_string(buf, &self.topic)?;
919 } else {
920 write_string(buf, &self.topic)?;
921 }
922 } else if self.topic != KafkaString::default() {
923 return Err(UnsupportedFieldVersion::new(1, "topic", version).into());
924 }
925 if version >= 13 {
926 write_uuid(buf, &self.topic_id);
927 } else if self.topic_id != KafkaUuid::ZERO {
928 return Err(UnsupportedFieldVersion::new(1, "topic_id", version).into());
929 }
930 if version >= 12 {
931 write_compact_array_length(buf, self.partitions.len() as i32);
932 for el in &self.partitions {
933 write_i32(buf, *el);
934 }
935 } else {
936 write_array_length(buf, self.partitions.len() as i32);
937 for el in &self.partitions {
938 write_i32(buf, *el);
939 }
940 }
941 if version >= 12 {
942 let mut all_tags: Vec<RawTaggedField> = self._unknown_tagged_fields.clone();
943 all_tags.sort_by_key(|f| f.tag);
944 write_tagged_fields(buf, &all_tags)?;
945 }
946 Ok(())
947 }
948 pub fn encoded_len(&self, version: i16) -> Result<usize> {
949 let mut len: usize = 0;
950 if version <= 12 {
951 if version >= 12 {
952 len += compact_string_len(&self.topic)?;
953 } else {
954 len += string_len(&self.topic)?;
955 }
956 } else if self.topic != KafkaString::default() {
957 return Err(UnsupportedFieldVersion::new(1, "topic", version).into());
958 }
959 if version >= 13 {
960 len += 16;
961 } else if self.topic_id != KafkaUuid::ZERO {
962 return Err(UnsupportedFieldVersion::new(1, "topic_id", version).into());
963 }
964 if version >= 12 {
965 len += compact_array_length_len(self.partitions.len() as i32);
966 len += self.partitions.len() * 4usize;
967 } else {
968 len += array_length_len();
969 len += self.partitions.len() * 4usize;
970 }
971 if version >= 12 {
972 let mut all_tags: Vec<RawTaggedField> = self._unknown_tagged_fields.clone();
973 all_tags.sort_by_key(|f| f.tag);
974 len += tagged_fields_len(&all_tags)?;
975 }
976 Ok(len)
977 }
978}