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