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