1#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15 buf::{ByteBuf, ByteBufMut},
16 compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17 Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct AlterPartitionResponse {
24 pub throttle_time_ms: i32,
28
29 pub error_code: i16,
33
34 pub topics: Vec<TopicData>,
38
39 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl AlterPartitionResponse {
44 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
50 self.throttle_time_ms = value;
51 self
52 }
53 pub fn with_error_code(mut self, value: i16) -> Self {
59 self.error_code = value;
60 self
61 }
62 pub fn with_topics(mut self, value: Vec<TopicData>) -> Self {
68 self.topics = value;
69 self
70 }
71 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
73 self.unknown_tagged_fields = value;
74 self
75 }
76 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
78 self.unknown_tagged_fields.insert(key, value);
79 self
80 }
81}
82
83#[cfg(feature = "broker")]
84impl Encodable for AlterPartitionResponse {
85 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
86 types::Int32.encode(buf, &self.throttle_time_ms)?;
87 types::Int16.encode(buf, &self.error_code)?;
88 types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
89 let num_tagged_fields = self.unknown_tagged_fields.len();
90 if num_tagged_fields > std::u32::MAX as usize {
91 bail!(
92 "Too many tagged fields to encode ({} fields)",
93 num_tagged_fields
94 );
95 }
96 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
97
98 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
99 Ok(())
100 }
101 fn compute_size(&self, version: i16) -> Result<usize> {
102 let mut total_size = 0;
103 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
104 total_size += types::Int16.compute_size(&self.error_code)?;
105 total_size += types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
106 let num_tagged_fields = self.unknown_tagged_fields.len();
107 if num_tagged_fields > std::u32::MAX as usize {
108 bail!(
109 "Too many tagged fields to encode ({} fields)",
110 num_tagged_fields
111 );
112 }
113 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
114
115 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
116 Ok(total_size)
117 }
118}
119
120#[cfg(feature = "client")]
121impl Decodable for AlterPartitionResponse {
122 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
123 let throttle_time_ms = types::Int32.decode(buf)?;
124 let error_code = types::Int16.decode(buf)?;
125 let topics = types::CompactArray(types::Struct { version }).decode(buf)?;
126 let mut unknown_tagged_fields = BTreeMap::new();
127 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
128 for _ in 0..num_tagged_fields {
129 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
130 let size: u32 = types::UnsignedVarInt.decode(buf)?;
131 let unknown_value = buf.try_get_bytes(size as usize)?;
132 unknown_tagged_fields.insert(tag as i32, unknown_value);
133 }
134 Ok(Self {
135 throttle_time_ms,
136 error_code,
137 topics,
138 unknown_tagged_fields,
139 })
140 }
141}
142
143impl Default for AlterPartitionResponse {
144 fn default() -> Self {
145 Self {
146 throttle_time_ms: 0,
147 error_code: 0,
148 topics: Default::default(),
149 unknown_tagged_fields: BTreeMap::new(),
150 }
151 }
152}
153
154impl Message for AlterPartitionResponse {
155 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
156 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
157}
158
159#[non_exhaustive]
161#[derive(Debug, Clone, PartialEq)]
162pub struct PartitionData {
163 pub partition_index: i32,
167
168 pub error_code: i16,
172
173 pub leader_id: super::BrokerId,
177
178 pub leader_epoch: i32,
182
183 pub isr: Vec<super::BrokerId>,
187
188 pub leader_recovery_state: i8,
192
193 pub partition_epoch: i32,
197
198 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
200}
201
202impl PartitionData {
203 pub fn with_partition_index(mut self, value: i32) -> Self {
209 self.partition_index = value;
210 self
211 }
212 pub fn with_error_code(mut self, value: i16) -> Self {
218 self.error_code = value;
219 self
220 }
221 pub fn with_leader_id(mut self, value: super::BrokerId) -> Self {
227 self.leader_id = value;
228 self
229 }
230 pub fn with_leader_epoch(mut self, value: i32) -> Self {
236 self.leader_epoch = value;
237 self
238 }
239 pub fn with_isr(mut self, value: Vec<super::BrokerId>) -> Self {
245 self.isr = value;
246 self
247 }
248 pub fn with_leader_recovery_state(mut self, value: i8) -> Self {
254 self.leader_recovery_state = value;
255 self
256 }
257 pub fn with_partition_epoch(mut self, value: i32) -> Self {
263 self.partition_epoch = value;
264 self
265 }
266 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
268 self.unknown_tagged_fields = value;
269 self
270 }
271 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
273 self.unknown_tagged_fields.insert(key, value);
274 self
275 }
276}
277
278#[cfg(feature = "broker")]
279impl Encodable for PartitionData {
280 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
281 types::Int32.encode(buf, &self.partition_index)?;
282 types::Int16.encode(buf, &self.error_code)?;
283 types::Int32.encode(buf, &self.leader_id)?;
284 types::Int32.encode(buf, &self.leader_epoch)?;
285 types::CompactArray(types::Int32).encode(buf, &self.isr)?;
286 if version >= 1 {
287 types::Int8.encode(buf, &self.leader_recovery_state)?;
288 }
289 types::Int32.encode(buf, &self.partition_epoch)?;
290 let num_tagged_fields = self.unknown_tagged_fields.len();
291 if num_tagged_fields > std::u32::MAX as usize {
292 bail!(
293 "Too many tagged fields to encode ({} fields)",
294 num_tagged_fields
295 );
296 }
297 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
298
299 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
300 Ok(())
301 }
302 fn compute_size(&self, version: i16) -> Result<usize> {
303 let mut total_size = 0;
304 total_size += types::Int32.compute_size(&self.partition_index)?;
305 total_size += types::Int16.compute_size(&self.error_code)?;
306 total_size += types::Int32.compute_size(&self.leader_id)?;
307 total_size += types::Int32.compute_size(&self.leader_epoch)?;
308 total_size += types::CompactArray(types::Int32).compute_size(&self.isr)?;
309 if version >= 1 {
310 total_size += types::Int8.compute_size(&self.leader_recovery_state)?;
311 }
312 total_size += types::Int32.compute_size(&self.partition_epoch)?;
313 let num_tagged_fields = self.unknown_tagged_fields.len();
314 if num_tagged_fields > std::u32::MAX as usize {
315 bail!(
316 "Too many tagged fields to encode ({} fields)",
317 num_tagged_fields
318 );
319 }
320 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
321
322 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
323 Ok(total_size)
324 }
325}
326
327#[cfg(feature = "client")]
328impl Decodable for PartitionData {
329 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
330 let partition_index = types::Int32.decode(buf)?;
331 let error_code = types::Int16.decode(buf)?;
332 let leader_id = types::Int32.decode(buf)?;
333 let leader_epoch = types::Int32.decode(buf)?;
334 let isr = types::CompactArray(types::Int32).decode(buf)?;
335 let leader_recovery_state = if version >= 1 {
336 types::Int8.decode(buf)?
337 } else {
338 0
339 };
340 let partition_epoch = types::Int32.decode(buf)?;
341 let mut unknown_tagged_fields = BTreeMap::new();
342 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
343 for _ in 0..num_tagged_fields {
344 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
345 let size: u32 = types::UnsignedVarInt.decode(buf)?;
346 let unknown_value = buf.try_get_bytes(size as usize)?;
347 unknown_tagged_fields.insert(tag as i32, unknown_value);
348 }
349 Ok(Self {
350 partition_index,
351 error_code,
352 leader_id,
353 leader_epoch,
354 isr,
355 leader_recovery_state,
356 partition_epoch,
357 unknown_tagged_fields,
358 })
359 }
360}
361
362impl Default for PartitionData {
363 fn default() -> Self {
364 Self {
365 partition_index: 0,
366 error_code: 0,
367 leader_id: (0).into(),
368 leader_epoch: 0,
369 isr: Default::default(),
370 leader_recovery_state: 0,
371 partition_epoch: 0,
372 unknown_tagged_fields: BTreeMap::new(),
373 }
374 }
375}
376
377impl Message for PartitionData {
378 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
379 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
380}
381
382#[non_exhaustive]
384#[derive(Debug, Clone, PartialEq)]
385pub struct TopicData {
386 pub topic_name: super::TopicName,
390
391 pub topic_id: Uuid,
395
396 pub partitions: Vec<PartitionData>,
400
401 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
403}
404
405impl TopicData {
406 pub fn with_topic_name(mut self, value: super::TopicName) -> Self {
412 self.topic_name = value;
413 self
414 }
415 pub fn with_topic_id(mut self, value: Uuid) -> Self {
421 self.topic_id = value;
422 self
423 }
424 pub fn with_partitions(mut self, value: Vec<PartitionData>) -> Self {
430 self.partitions = value;
431 self
432 }
433 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
435 self.unknown_tagged_fields = value;
436 self
437 }
438 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
440 self.unknown_tagged_fields.insert(key, value);
441 self
442 }
443}
444
445#[cfg(feature = "broker")]
446impl Encodable for TopicData {
447 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
448 if version <= 1 {
449 types::CompactString.encode(buf, &self.topic_name)?;
450 }
451 if version >= 2 {
452 types::Uuid.encode(buf, &self.topic_id)?;
453 }
454 types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
455 let num_tagged_fields = self.unknown_tagged_fields.len();
456 if num_tagged_fields > std::u32::MAX as usize {
457 bail!(
458 "Too many tagged fields to encode ({} fields)",
459 num_tagged_fields
460 );
461 }
462 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
463
464 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
465 Ok(())
466 }
467 fn compute_size(&self, version: i16) -> Result<usize> {
468 let mut total_size = 0;
469 if version <= 1 {
470 total_size += types::CompactString.compute_size(&self.topic_name)?;
471 }
472 if version >= 2 {
473 total_size += types::Uuid.compute_size(&self.topic_id)?;
474 }
475 total_size +=
476 types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
477 let num_tagged_fields = self.unknown_tagged_fields.len();
478 if num_tagged_fields > std::u32::MAX as usize {
479 bail!(
480 "Too many tagged fields to encode ({} fields)",
481 num_tagged_fields
482 );
483 }
484 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
485
486 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
487 Ok(total_size)
488 }
489}
490
491#[cfg(feature = "client")]
492impl Decodable for TopicData {
493 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
494 let topic_name = if version <= 1 {
495 types::CompactString.decode(buf)?
496 } else {
497 Default::default()
498 };
499 let topic_id = if version >= 2 {
500 types::Uuid.decode(buf)?
501 } else {
502 Uuid::nil()
503 };
504 let partitions = types::CompactArray(types::Struct { version }).decode(buf)?;
505 let mut unknown_tagged_fields = BTreeMap::new();
506 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
507 for _ in 0..num_tagged_fields {
508 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
509 let size: u32 = types::UnsignedVarInt.decode(buf)?;
510 let unknown_value = buf.try_get_bytes(size as usize)?;
511 unknown_tagged_fields.insert(tag as i32, unknown_value);
512 }
513 Ok(Self {
514 topic_name,
515 topic_id,
516 partitions,
517 unknown_tagged_fields,
518 })
519 }
520}
521
522impl Default for TopicData {
523 fn default() -> Self {
524 Self {
525 topic_name: Default::default(),
526 topic_id: Uuid::nil(),
527 partitions: Default::default(),
528 unknown_tagged_fields: BTreeMap::new(),
529 }
530 }
531}
532
533impl Message for TopicData {
534 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
535 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
536}
537
538impl HeaderVersion for AlterPartitionResponse {
539 fn header_version(version: i16) -> i16 {
540 1
541 }
542}