1#![allow(
6 clippy::field_reassign_with_default,
7 clippy::nonminimal_bool,
8 clippy::derivable_impls,
9 clippy::borrow_deref_ref,
10 clippy::double_parens,
11 unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21pub fn is_flexible(version: i16) -> bool {
23 let _ = version;
24 version >= 9
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct MetadataResponse {
31 pub throttle_time_ms: i32,
33 pub brokers: Vec<MetadataResponseBroker>,
35 pub cluster_id: Option<String>,
37 pub controller_id: i32,
39 pub topics: Vec<MetadataResponseTopic>,
41 pub cluster_authorized_operations: i32,
43 pub error_code: i16,
45 pub unknown_tagged_fields: Vec<RawTaggedField>,
47}
48
49impl Default for MetadataResponse {
50 fn default() -> Self {
51 Self {
52 throttle_time_ms: 0,
53 brokers: Vec::new(),
54 cluster_id: None,
55 controller_id: -1,
56 topics: Vec::new(),
57 cluster_authorized_operations: -2147483648,
58 error_code: 0,
59 unknown_tagged_fields: Vec::new(),
60 }
61 }
62}
63
64impl MetadataResponse {
65 pub const API_KEY: i16 = 3;
67 pub const MIN_VERSION: i16 = 0;
69 pub const MAX_VERSION: i16 = 13;
71
72 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
74 if version >= 3 {
75 buf.put_i32(self.throttle_time_ms);
76 }
77 if is_flexible(version) {
78 wire::put_compact_array_len(buf, Some(self.brokers.len()));
79 } else {
80 wire::put_array_len(buf, Some(self.brokers.len()))?;
81 }
82 for item in &self.brokers {
83 item.encode(buf, version)?;
84 }
85 if version >= 2 {
86 if is_flexible(version) {
87 wire::put_compact_nullable_string(buf, self.cluster_id.as_deref());
88 } else {
89 wire::put_nullable_string(buf, self.cluster_id.as_deref())?;
90 }
91 }
92 if version >= 1 {
93 buf.put_i32(self.controller_id);
94 }
95 if is_flexible(version) {
96 wire::put_compact_array_len(buf, Some(self.topics.len()));
97 } else {
98 wire::put_array_len(buf, Some(self.topics.len()))?;
99 }
100 for item in &self.topics {
101 item.encode(buf, version)?;
102 }
103 if (8..=10).contains(&version) {
104 buf.put_i32(self.cluster_authorized_operations);
105 }
106 if version >= 13 {
107 buf.put_i16(self.error_code);
108 }
109 if is_flexible(version) {
110 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
111 }
112 Ok(())
113 }
114
115 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
118 Self::decode_with_limits(buf, version, Limits::default())
119 }
120
121 pub fn decode_with_limits(
123 buf: &mut impl Buf,
124 version: i16,
125 limits: Limits,
126 ) -> Result<Self, DecodeError> {
127 let mut budget = limits.budget(buf.remaining());
128 Self::decode_with_budget(buf, version, &mut budget)
129 }
130
131 pub fn decode_with_budget(
133 buf: &mut impl Buf,
134 version: i16,
135 budget: &mut Budget,
136 ) -> Result<Self, DecodeError> {
137 let mut this = Self::default();
138 if version >= 3 {
139 this.throttle_time_ms = wire::get_i32(buf)?;
140 }
141 this.brokers = {
142 let len = if is_flexible(version) {
143 wire::get_compact_array_len(buf)?
144 } else {
145 wire::get_array_len(buf)?
146 };
147 match len {
148 None => return Err(DecodeError::InvalidLength(-1)),
149 Some(n) => {
150 let mut items = Vec::new();
151 for _ in 0..n {
152 let mark = buf.remaining();
153 let item =
154 MetadataResponseBroker::decode_with_budget(buf, version, budget)?;
155 budget.progress(mark, buf.remaining())?;
156 budget.push(&mut items, item)?;
157 }
158 items
159 }
160 }
161 };
162 if version >= 2 {
163 this.cluster_id = if is_flexible(version) {
164 wire::get_compact_nullable_string(buf)?
165 } else {
166 wire::get_nullable_string(buf)?
167 };
168 }
169 if version >= 1 {
170 this.controller_id = wire::get_i32(buf)?;
171 }
172 this.topics = {
173 let len = if is_flexible(version) {
174 wire::get_compact_array_len(buf)?
175 } else {
176 wire::get_array_len(buf)?
177 };
178 match len {
179 None => return Err(DecodeError::InvalidLength(-1)),
180 Some(n) => {
181 let mut items = Vec::new();
182 for _ in 0..n {
183 let mark = buf.remaining();
184 let item = MetadataResponseTopic::decode_with_budget(buf, version, budget)?;
185 budget.progress(mark, buf.remaining())?;
186 budget.push(&mut items, item)?;
187 }
188 items
189 }
190 }
191 };
192 if (8..=10).contains(&version) {
193 this.cluster_authorized_operations = wire::get_i32(buf)?;
194 }
195 if version >= 13 {
196 this.error_code = wire::get_i16(buf)?;
197 }
198 if is_flexible(version) {
199 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
200 }
201 Ok(this)
202 }
203}
204
205impl crate::Message for MetadataResponse {
206 const API_KEY: i16 = 3;
207 const MIN_VERSION: i16 = 0;
208 const MAX_VERSION: i16 = 13;
209
210 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
211 MetadataResponse::encode(self, buf, version)
212 }
213 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
214 MetadataResponse::decode(buf, version)
215 }
216 fn decode_with_limits(
217 buf: &mut impl Buf,
218 version: i16,
219 limits: Limits,
220 ) -> Result<Self, DecodeError> {
221 MetadataResponse::decode_with_limits(buf, version, limits)
222 }
223 fn throttle_time_ms(&self) -> Option<i32> {
224 Some(self.throttle_time_ms)
225 }
226}
227
228#[derive(Debug, Clone, PartialEq)]
230#[non_exhaustive]
231pub struct MetadataResponseBroker {
232 pub node_id: i32,
234 pub host: String,
236 pub port: i32,
238 pub rack: Option<String>,
240 pub unknown_tagged_fields: Vec<RawTaggedField>,
242}
243
244impl Default for MetadataResponseBroker {
245 fn default() -> Self {
246 Self {
247 node_id: 0,
248 host: String::new(),
249 port: 0,
250 rack: None,
251 unknown_tagged_fields: Vec::new(),
252 }
253 }
254}
255
256impl MetadataResponseBroker {
257 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
259 buf.put_i32(self.node_id);
260 if is_flexible(version) {
261 wire::put_compact_string(buf, &self.host);
262 } else {
263 wire::put_string(buf, &self.host)?;
264 }
265 buf.put_i32(self.port);
266 if version >= 1 {
267 if is_flexible(version) {
268 wire::put_compact_nullable_string(buf, self.rack.as_deref());
269 } else {
270 wire::put_nullable_string(buf, self.rack.as_deref())?;
271 }
272 }
273 if is_flexible(version) {
274 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
275 }
276 Ok(())
277 }
278
279 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
282 Self::decode_with_limits(buf, version, Limits::default())
283 }
284
285 pub fn decode_with_limits(
287 buf: &mut impl Buf,
288 version: i16,
289 limits: Limits,
290 ) -> Result<Self, DecodeError> {
291 let mut budget = limits.budget(buf.remaining());
292 Self::decode_with_budget(buf, version, &mut budget)
293 }
294
295 pub fn decode_with_budget(
297 buf: &mut impl Buf,
298 version: i16,
299 budget: &mut Budget,
300 ) -> Result<Self, DecodeError> {
301 let mut this = Self::default();
302 this.node_id = wire::get_i32(buf)?;
303 this.host = if is_flexible(version) {
304 wire::get_compact_string(buf)?
305 } else {
306 wire::get_string(buf)?
307 };
308 this.port = wire::get_i32(buf)?;
309 if version >= 1 {
310 this.rack = if is_flexible(version) {
311 wire::get_compact_nullable_string(buf)?
312 } else {
313 wire::get_nullable_string(buf)?
314 };
315 }
316 if is_flexible(version) {
317 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
318 }
319 Ok(this)
320 }
321}
322
323#[derive(Debug, Clone, PartialEq)]
325#[non_exhaustive]
326pub struct MetadataResponseTopic {
327 pub error_code: i16,
329 pub name: Option<String>,
331 pub topic_id: [u8; 16],
333 pub is_internal: bool,
335 pub partitions: Vec<MetadataResponsePartition>,
337 pub topic_authorized_operations: i32,
339 pub unknown_tagged_fields: Vec<RawTaggedField>,
341}
342
343impl Default for MetadataResponseTopic {
344 fn default() -> Self {
345 Self {
346 error_code: 0,
347 name: Some(String::new()),
348 topic_id: [0u8; 16],
349 is_internal: false,
350 partitions: Vec::new(),
351 topic_authorized_operations: -2147483648,
352 unknown_tagged_fields: Vec::new(),
353 }
354 }
355}
356
357impl MetadataResponseTopic {
358 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
360 buf.put_i16(self.error_code);
361 if self.name.is_none() && !(version >= 12) {
362 return Err(EncodeError::NullField("Name"));
363 }
364 if is_flexible(version) {
365 wire::put_compact_nullable_string(buf, self.name.as_deref());
366 } else {
367 wire::put_nullable_string(buf, self.name.as_deref())?;
368 }
369 if version >= 10 {
370 wire::put_uuid(buf, self.topic_id);
371 }
372 if version >= 1 {
373 buf.put_u8(u8::from(self.is_internal));
374 }
375 if is_flexible(version) {
376 wire::put_compact_array_len(buf, Some(self.partitions.len()));
377 } else {
378 wire::put_array_len(buf, Some(self.partitions.len()))?;
379 }
380 for item in &self.partitions {
381 item.encode(buf, version)?;
382 }
383 if version >= 8 {
384 buf.put_i32(self.topic_authorized_operations);
385 }
386 if is_flexible(version) {
387 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
388 }
389 Ok(())
390 }
391
392 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
395 Self::decode_with_limits(buf, version, Limits::default())
396 }
397
398 pub fn decode_with_limits(
400 buf: &mut impl Buf,
401 version: i16,
402 limits: Limits,
403 ) -> Result<Self, DecodeError> {
404 let mut budget = limits.budget(buf.remaining());
405 Self::decode_with_budget(buf, version, &mut budget)
406 }
407
408 pub fn decode_with_budget(
410 buf: &mut impl Buf,
411 version: i16,
412 budget: &mut Budget,
413 ) -> Result<Self, DecodeError> {
414 let mut this = Self::default();
415 this.error_code = wire::get_i16(buf)?;
416 this.name = {
417 let raw = if is_flexible(version) {
418 wire::get_compact_nullable_string(buf)?
419 } else {
420 wire::get_nullable_string(buf)?
421 };
422 if raw.is_none() && !(version >= 12) {
423 return Err(DecodeError::InvalidLength(-1));
424 }
425 raw
426 };
427 if version >= 10 {
428 this.topic_id = wire::get_uuid(buf)?;
429 }
430 if version >= 1 {
431 this.is_internal = wire::get_bool(buf)?;
432 }
433 this.partitions = {
434 let len = if is_flexible(version) {
435 wire::get_compact_array_len(buf)?
436 } else {
437 wire::get_array_len(buf)?
438 };
439 match len {
440 None => return Err(DecodeError::InvalidLength(-1)),
441 Some(n) => {
442 let mut items = Vec::new();
443 for _ in 0..n {
444 let mark = buf.remaining();
445 let item =
446 MetadataResponsePartition::decode_with_budget(buf, version, budget)?;
447 budget.progress(mark, buf.remaining())?;
448 budget.push(&mut items, item)?;
449 }
450 items
451 }
452 }
453 };
454 if version >= 8 {
455 this.topic_authorized_operations = wire::get_i32(buf)?;
456 }
457 if is_flexible(version) {
458 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
459 }
460 Ok(this)
461 }
462}
463
464#[derive(Debug, Clone, PartialEq)]
466#[non_exhaustive]
467pub struct MetadataResponsePartition {
468 pub error_code: i16,
470 pub partition_index: i32,
472 pub leader_id: i32,
474 pub leader_epoch: i32,
476 pub replica_nodes: Vec<i32>,
478 pub isr_nodes: Vec<i32>,
480 pub offline_replicas: Vec<i32>,
482 pub unknown_tagged_fields: Vec<RawTaggedField>,
484}
485
486impl Default for MetadataResponsePartition {
487 fn default() -> Self {
488 Self {
489 error_code: 0,
490 partition_index: 0,
491 leader_id: 0,
492 leader_epoch: -1,
493 replica_nodes: Vec::new(),
494 isr_nodes: Vec::new(),
495 offline_replicas: Vec::new(),
496 unknown_tagged_fields: Vec::new(),
497 }
498 }
499}
500
501impl MetadataResponsePartition {
502 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
504 buf.put_i16(self.error_code);
505 buf.put_i32(self.partition_index);
506 buf.put_i32(self.leader_id);
507 if version >= 7 {
508 buf.put_i32(self.leader_epoch);
509 }
510 if is_flexible(version) {
511 wire::put_compact_array_len(buf, Some(self.replica_nodes.len()));
512 } else {
513 wire::put_array_len(buf, Some(self.replica_nodes.len()))?;
514 }
515 for item in &self.replica_nodes {
516 buf.put_i32(*item);
517 }
518 if is_flexible(version) {
519 wire::put_compact_array_len(buf, Some(self.isr_nodes.len()));
520 } else {
521 wire::put_array_len(buf, Some(self.isr_nodes.len()))?;
522 }
523 for item in &self.isr_nodes {
524 buf.put_i32(*item);
525 }
526 if version >= 5 {
527 if is_flexible(version) {
528 wire::put_compact_array_len(buf, Some(self.offline_replicas.len()));
529 } else {
530 wire::put_array_len(buf, Some(self.offline_replicas.len()))?;
531 }
532 for item in &self.offline_replicas {
533 buf.put_i32(*item);
534 }
535 }
536 if is_flexible(version) {
537 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
538 }
539 Ok(())
540 }
541
542 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
545 Self::decode_with_limits(buf, version, Limits::default())
546 }
547
548 pub fn decode_with_limits(
550 buf: &mut impl Buf,
551 version: i16,
552 limits: Limits,
553 ) -> Result<Self, DecodeError> {
554 let mut budget = limits.budget(buf.remaining());
555 Self::decode_with_budget(buf, version, &mut budget)
556 }
557
558 pub fn decode_with_budget(
560 buf: &mut impl Buf,
561 version: i16,
562 budget: &mut Budget,
563 ) -> Result<Self, DecodeError> {
564 let mut this = Self::default();
565 this.error_code = wire::get_i16(buf)?;
566 this.partition_index = wire::get_i32(buf)?;
567 this.leader_id = wire::get_i32(buf)?;
568 if version >= 7 {
569 this.leader_epoch = wire::get_i32(buf)?;
570 }
571 this.replica_nodes = {
572 let len = if is_flexible(version) {
573 wire::get_compact_array_len(buf)?
574 } else {
575 wire::get_array_len(buf)?
576 };
577 match len {
578 None => return Err(DecodeError::InvalidLength(-1)),
579 Some(n) => {
580 let mut items = Vec::new();
581 for _ in 0..n {
582 budget.push(&mut items, wire::get_i32(buf)?)?;
583 }
584 items
585 }
586 }
587 };
588 this.isr_nodes = {
589 let len = if is_flexible(version) {
590 wire::get_compact_array_len(buf)?
591 } else {
592 wire::get_array_len(buf)?
593 };
594 match len {
595 None => return Err(DecodeError::InvalidLength(-1)),
596 Some(n) => {
597 let mut items = Vec::new();
598 for _ in 0..n {
599 budget.push(&mut items, wire::get_i32(buf)?)?;
600 }
601 items
602 }
603 }
604 };
605 if version >= 5 {
606 this.offline_replicas = {
607 let len = if is_flexible(version) {
608 wire::get_compact_array_len(buf)?
609 } else {
610 wire::get_array_len(buf)?
611 };
612 match len {
613 None => return Err(DecodeError::InvalidLength(-1)),
614 Some(n) => {
615 let mut items = Vec::new();
616 for _ in 0..n {
617 budget.push(&mut items, wire::get_i32(buf)?)?;
618 }
619 items
620 }
621 }
622 };
623 }
624 if is_flexible(version) {
625 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
626 }
627 Ok(this)
628 }
629}