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