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