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 AssignReplicasToDirsRequest {
24 pub broker_id: super::BrokerId,
28
29 pub broker_epoch: i64,
33
34 pub directories: Vec<DirectoryData>,
38
39 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl AssignReplicasToDirsRequest {
44 pub fn with_broker_id(mut self, value: super::BrokerId) -> Self {
50 self.broker_id = value;
51 self
52 }
53 pub fn with_broker_epoch(mut self, value: i64) -> Self {
59 self.broker_epoch = value;
60 self
61 }
62 pub fn with_directories(mut self, value: Vec<DirectoryData>) -> Self {
68 self.directories = 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 = "client")]
84impl Encodable for AssignReplicasToDirsRequest {
85 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
86 if version != 0 {
87 bail!("specified version not supported by this message type");
88 }
89 types::Int32.encode(buf, &self.broker_id)?;
90 types::Int64.encode(buf, &self.broker_epoch)?;
91 types::CompactArray(types::Struct { version }).encode(buf, &self.directories)?;
92 let num_tagged_fields = self.unknown_tagged_fields.len();
93 if num_tagged_fields > std::u32::MAX as usize {
94 bail!(
95 "Too many tagged fields to encode ({} fields)",
96 num_tagged_fields
97 );
98 }
99 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
100
101 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
102 Ok(())
103 }
104 fn compute_size(&self, version: i16) -> Result<usize> {
105 let mut total_size = 0;
106 total_size += types::Int32.compute_size(&self.broker_id)?;
107 total_size += types::Int64.compute_size(&self.broker_epoch)?;
108 total_size +=
109 types::CompactArray(types::Struct { version }).compute_size(&self.directories)?;
110 let num_tagged_fields = self.unknown_tagged_fields.len();
111 if num_tagged_fields > std::u32::MAX as usize {
112 bail!(
113 "Too many tagged fields to encode ({} fields)",
114 num_tagged_fields
115 );
116 }
117 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
118
119 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
120 Ok(total_size)
121 }
122}
123
124#[cfg(feature = "broker")]
125impl Decodable for AssignReplicasToDirsRequest {
126 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
127 if version != 0 {
128 bail!("specified version not supported by this message type");
129 }
130 let broker_id = types::Int32.decode(buf)?;
131 let broker_epoch = types::Int64.decode(buf)?;
132 let directories = types::CompactArray(types::Struct { version }).decode(buf)?;
133 let mut unknown_tagged_fields = BTreeMap::new();
134 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
135 for _ in 0..num_tagged_fields {
136 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
137 let size: u32 = types::UnsignedVarInt.decode(buf)?;
138 let unknown_value = buf.try_get_bytes(size as usize)?;
139 unknown_tagged_fields.insert(tag as i32, unknown_value);
140 }
141 Ok(Self {
142 broker_id,
143 broker_epoch,
144 directories,
145 unknown_tagged_fields,
146 })
147 }
148}
149
150impl Default for AssignReplicasToDirsRequest {
151 fn default() -> Self {
152 Self {
153 broker_id: (0).into(),
154 broker_epoch: -1,
155 directories: Default::default(),
156 unknown_tagged_fields: BTreeMap::new(),
157 }
158 }
159}
160
161impl Message for AssignReplicasToDirsRequest {
162 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
163 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
164}
165
166#[non_exhaustive]
168#[derive(Debug, Clone, PartialEq)]
169pub struct DirectoryData {
170 pub id: Uuid,
174
175 pub topics: Vec<TopicData>,
179
180 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
182}
183
184impl DirectoryData {
185 pub fn with_id(mut self, value: Uuid) -> Self {
191 self.id = value;
192 self
193 }
194 pub fn with_topics(mut self, value: Vec<TopicData>) -> Self {
200 self.topics = value;
201 self
202 }
203 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
205 self.unknown_tagged_fields = value;
206 self
207 }
208 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
210 self.unknown_tagged_fields.insert(key, value);
211 self
212 }
213}
214
215#[cfg(feature = "client")]
216impl Encodable for DirectoryData {
217 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
218 if version != 0 {
219 bail!("specified version not supported by this message type");
220 }
221 types::Uuid.encode(buf, &self.id)?;
222 types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
223 let num_tagged_fields = self.unknown_tagged_fields.len();
224 if num_tagged_fields > std::u32::MAX as usize {
225 bail!(
226 "Too many tagged fields to encode ({} fields)",
227 num_tagged_fields
228 );
229 }
230 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
231
232 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
233 Ok(())
234 }
235 fn compute_size(&self, version: i16) -> Result<usize> {
236 let mut total_size = 0;
237 total_size += types::Uuid.compute_size(&self.id)?;
238 total_size += types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
239 let num_tagged_fields = self.unknown_tagged_fields.len();
240 if num_tagged_fields > std::u32::MAX as usize {
241 bail!(
242 "Too many tagged fields to encode ({} fields)",
243 num_tagged_fields
244 );
245 }
246 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
247
248 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
249 Ok(total_size)
250 }
251}
252
253#[cfg(feature = "broker")]
254impl Decodable for DirectoryData {
255 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
256 if version != 0 {
257 bail!("specified version not supported by this message type");
258 }
259 let id = types::Uuid.decode(buf)?;
260 let topics = types::CompactArray(types::Struct { version }).decode(buf)?;
261 let mut unknown_tagged_fields = BTreeMap::new();
262 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
263 for _ in 0..num_tagged_fields {
264 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
265 let size: u32 = types::UnsignedVarInt.decode(buf)?;
266 let unknown_value = buf.try_get_bytes(size as usize)?;
267 unknown_tagged_fields.insert(tag as i32, unknown_value);
268 }
269 Ok(Self {
270 id,
271 topics,
272 unknown_tagged_fields,
273 })
274 }
275}
276
277impl Default for DirectoryData {
278 fn default() -> Self {
279 Self {
280 id: Uuid::nil(),
281 topics: Default::default(),
282 unknown_tagged_fields: BTreeMap::new(),
283 }
284 }
285}
286
287impl Message for DirectoryData {
288 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
289 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
290}
291
292#[non_exhaustive]
294#[derive(Debug, Clone, PartialEq)]
295pub struct PartitionData {
296 pub partition_index: i32,
300
301 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
303}
304
305impl PartitionData {
306 pub fn with_partition_index(mut self, value: i32) -> Self {
312 self.partition_index = value;
313 self
314 }
315 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
317 self.unknown_tagged_fields = value;
318 self
319 }
320 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
322 self.unknown_tagged_fields.insert(key, value);
323 self
324 }
325}
326
327#[cfg(feature = "client")]
328impl Encodable for PartitionData {
329 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
330 if version != 0 {
331 bail!("specified version not supported by this message type");
332 }
333 types::Int32.encode(buf, &self.partition_index)?;
334 let num_tagged_fields = self.unknown_tagged_fields.len();
335 if num_tagged_fields > std::u32::MAX as usize {
336 bail!(
337 "Too many tagged fields to encode ({} fields)",
338 num_tagged_fields
339 );
340 }
341 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
342
343 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
344 Ok(())
345 }
346 fn compute_size(&self, version: i16) -> Result<usize> {
347 let mut total_size = 0;
348 total_size += types::Int32.compute_size(&self.partition_index)?;
349 let num_tagged_fields = self.unknown_tagged_fields.len();
350 if num_tagged_fields > std::u32::MAX as usize {
351 bail!(
352 "Too many tagged fields to encode ({} fields)",
353 num_tagged_fields
354 );
355 }
356 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
357
358 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
359 Ok(total_size)
360 }
361}
362
363#[cfg(feature = "broker")]
364impl Decodable for PartitionData {
365 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
366 if version != 0 {
367 bail!("specified version not supported by this message type");
368 }
369 let partition_index = types::Int32.decode(buf)?;
370 let mut unknown_tagged_fields = BTreeMap::new();
371 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
372 for _ in 0..num_tagged_fields {
373 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
374 let size: u32 = types::UnsignedVarInt.decode(buf)?;
375 let unknown_value = buf.try_get_bytes(size as usize)?;
376 unknown_tagged_fields.insert(tag as i32, unknown_value);
377 }
378 Ok(Self {
379 partition_index,
380 unknown_tagged_fields,
381 })
382 }
383}
384
385impl Default for PartitionData {
386 fn default() -> Self {
387 Self {
388 partition_index: 0,
389 unknown_tagged_fields: BTreeMap::new(),
390 }
391 }
392}
393
394impl Message for PartitionData {
395 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
396 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
397}
398
399#[non_exhaustive]
401#[derive(Debug, Clone, PartialEq)]
402pub struct TopicData {
403 pub topic_id: Uuid,
407
408 pub partitions: Vec<PartitionData>,
412
413 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
415}
416
417impl TopicData {
418 pub fn with_topic_id(mut self, value: Uuid) -> Self {
424 self.topic_id = value;
425 self
426 }
427 pub fn with_partitions(mut self, value: Vec<PartitionData>) -> Self {
433 self.partitions = value;
434 self
435 }
436 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
438 self.unknown_tagged_fields = value;
439 self
440 }
441 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
443 self.unknown_tagged_fields.insert(key, value);
444 self
445 }
446}
447
448#[cfg(feature = "client")]
449impl Encodable for TopicData {
450 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
451 if version != 0 {
452 bail!("specified version not supported by this message type");
453 }
454 types::Uuid.encode(buf, &self.topic_id)?;
455 types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
456 let num_tagged_fields = self.unknown_tagged_fields.len();
457 if num_tagged_fields > std::u32::MAX as usize {
458 bail!(
459 "Too many tagged fields to encode ({} fields)",
460 num_tagged_fields
461 );
462 }
463 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
464
465 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
466 Ok(())
467 }
468 fn compute_size(&self, version: i16) -> Result<usize> {
469 let mut total_size = 0;
470 total_size += types::Uuid.compute_size(&self.topic_id)?;
471 total_size +=
472 types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
473 let num_tagged_fields = self.unknown_tagged_fields.len();
474 if num_tagged_fields > std::u32::MAX as usize {
475 bail!(
476 "Too many tagged fields to encode ({} fields)",
477 num_tagged_fields
478 );
479 }
480 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
481
482 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
483 Ok(total_size)
484 }
485}
486
487#[cfg(feature = "broker")]
488impl Decodable for TopicData {
489 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
490 if version != 0 {
491 bail!("specified version not supported by this message type");
492 }
493 let topic_id = types::Uuid.decode(buf)?;
494 let partitions = types::CompactArray(types::Struct { version }).decode(buf)?;
495 let mut unknown_tagged_fields = BTreeMap::new();
496 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
497 for _ in 0..num_tagged_fields {
498 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
499 let size: u32 = types::UnsignedVarInt.decode(buf)?;
500 let unknown_value = buf.try_get_bytes(size as usize)?;
501 unknown_tagged_fields.insert(tag as i32, unknown_value);
502 }
503 Ok(Self {
504 topic_id,
505 partitions,
506 unknown_tagged_fields,
507 })
508 }
509}
510
511impl Default for TopicData {
512 fn default() -> Self {
513 Self {
514 topic_id: Uuid::nil(),
515 partitions: Default::default(),
516 unknown_tagged_fields: BTreeMap::new(),
517 }
518 }
519}
520
521impl Message for TopicData {
522 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
523 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
524}
525
526impl HeaderVersion for AssignReplicasToDirsRequest {
527 fn header_version(version: i16) -> i16 {
528 2
529 }
530}