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 AlterPartitionReassignmentsRequest {
24 pub timeout_ms: i32,
28
29 pub topics: Vec<ReassignableTopic>,
33
34 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl AlterPartitionReassignmentsRequest {
39 pub fn with_timeout_ms(mut self, value: i32) -> Self {
45 self.timeout_ms = value;
46 self
47 }
48 pub fn with_topics(mut self, value: Vec<ReassignableTopic>) -> Self {
54 self.topics = value;
55 self
56 }
57 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
59 self.unknown_tagged_fields = value;
60 self
61 }
62 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
64 self.unknown_tagged_fields.insert(key, value);
65 self
66 }
67}
68
69#[cfg(feature = "client")]
70impl Encodable for AlterPartitionReassignmentsRequest {
71 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
72 types::Int32.encode(buf, &self.timeout_ms)?;
73 types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
74 let num_tagged_fields = self.unknown_tagged_fields.len();
75 if num_tagged_fields > std::u32::MAX as usize {
76 bail!(
77 "Too many tagged fields to encode ({} fields)",
78 num_tagged_fields
79 );
80 }
81 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
82
83 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
84 Ok(())
85 }
86 fn compute_size(&self, version: i16) -> Result<usize> {
87 let mut total_size = 0;
88 total_size += types::Int32.compute_size(&self.timeout_ms)?;
89 total_size += types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
90 let num_tagged_fields = self.unknown_tagged_fields.len();
91 if num_tagged_fields > std::u32::MAX as usize {
92 bail!(
93 "Too many tagged fields to encode ({} fields)",
94 num_tagged_fields
95 );
96 }
97 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
98
99 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
100 Ok(total_size)
101 }
102}
103
104#[cfg(feature = "broker")]
105impl Decodable for AlterPartitionReassignmentsRequest {
106 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
107 let timeout_ms = types::Int32.decode(buf)?;
108 let topics = types::CompactArray(types::Struct { version }).decode(buf)?;
109 let mut unknown_tagged_fields = BTreeMap::new();
110 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
111 for _ in 0..num_tagged_fields {
112 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
113 let size: u32 = types::UnsignedVarInt.decode(buf)?;
114 let unknown_value = buf.try_get_bytes(size as usize)?;
115 unknown_tagged_fields.insert(tag as i32, unknown_value);
116 }
117 Ok(Self {
118 timeout_ms,
119 topics,
120 unknown_tagged_fields,
121 })
122 }
123}
124
125impl Default for AlterPartitionReassignmentsRequest {
126 fn default() -> Self {
127 Self {
128 timeout_ms: 60000,
129 topics: Default::default(),
130 unknown_tagged_fields: BTreeMap::new(),
131 }
132 }
133}
134
135impl Message for AlterPartitionReassignmentsRequest {
136 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
137 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
138}
139
140#[non_exhaustive]
142#[derive(Debug, Clone, PartialEq)]
143pub struct ReassignablePartition {
144 pub partition_index: i32,
148
149 pub replicas: Option<Vec<super::BrokerId>>,
153
154 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
156}
157
158impl ReassignablePartition {
159 pub fn with_partition_index(mut self, value: i32) -> Self {
165 self.partition_index = value;
166 self
167 }
168 pub fn with_replicas(mut self, value: Option<Vec<super::BrokerId>>) -> Self {
174 self.replicas = value;
175 self
176 }
177 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
179 self.unknown_tagged_fields = value;
180 self
181 }
182 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
184 self.unknown_tagged_fields.insert(key, value);
185 self
186 }
187}
188
189#[cfg(feature = "client")]
190impl Encodable for ReassignablePartition {
191 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
192 types::Int32.encode(buf, &self.partition_index)?;
193 types::CompactArray(types::Int32).encode(buf, &self.replicas)?;
194 let num_tagged_fields = self.unknown_tagged_fields.len();
195 if num_tagged_fields > std::u32::MAX as usize {
196 bail!(
197 "Too many tagged fields to encode ({} fields)",
198 num_tagged_fields
199 );
200 }
201 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
202
203 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
204 Ok(())
205 }
206 fn compute_size(&self, version: i16) -> Result<usize> {
207 let mut total_size = 0;
208 total_size += types::Int32.compute_size(&self.partition_index)?;
209 total_size += types::CompactArray(types::Int32).compute_size(&self.replicas)?;
210 let num_tagged_fields = self.unknown_tagged_fields.len();
211 if num_tagged_fields > std::u32::MAX as usize {
212 bail!(
213 "Too many tagged fields to encode ({} fields)",
214 num_tagged_fields
215 );
216 }
217 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
218
219 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
220 Ok(total_size)
221 }
222}
223
224#[cfg(feature = "broker")]
225impl Decodable for ReassignablePartition {
226 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
227 let partition_index = types::Int32.decode(buf)?;
228 let replicas = types::CompactArray(types::Int32).decode(buf)?;
229 let mut unknown_tagged_fields = BTreeMap::new();
230 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
231 for _ in 0..num_tagged_fields {
232 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
233 let size: u32 = types::UnsignedVarInt.decode(buf)?;
234 let unknown_value = buf.try_get_bytes(size as usize)?;
235 unknown_tagged_fields.insert(tag as i32, unknown_value);
236 }
237 Ok(Self {
238 partition_index,
239 replicas,
240 unknown_tagged_fields,
241 })
242 }
243}
244
245impl Default for ReassignablePartition {
246 fn default() -> Self {
247 Self {
248 partition_index: 0,
249 replicas: None,
250 unknown_tagged_fields: BTreeMap::new(),
251 }
252 }
253}
254
255impl Message for ReassignablePartition {
256 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
257 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
258}
259
260#[non_exhaustive]
262#[derive(Debug, Clone, PartialEq)]
263pub struct ReassignableTopic {
264 pub name: super::TopicName,
268
269 pub partitions: Vec<ReassignablePartition>,
273
274 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
276}
277
278impl ReassignableTopic {
279 pub fn with_name(mut self, value: super::TopicName) -> Self {
285 self.name = value;
286 self
287 }
288 pub fn with_partitions(mut self, value: Vec<ReassignablePartition>) -> Self {
294 self.partitions = value;
295 self
296 }
297 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
299 self.unknown_tagged_fields = value;
300 self
301 }
302 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
304 self.unknown_tagged_fields.insert(key, value);
305 self
306 }
307}
308
309#[cfg(feature = "client")]
310impl Encodable for ReassignableTopic {
311 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
312 types::CompactString.encode(buf, &self.name)?;
313 types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
314 let num_tagged_fields = self.unknown_tagged_fields.len();
315 if num_tagged_fields > std::u32::MAX as usize {
316 bail!(
317 "Too many tagged fields to encode ({} fields)",
318 num_tagged_fields
319 );
320 }
321 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
322
323 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
324 Ok(())
325 }
326 fn compute_size(&self, version: i16) -> Result<usize> {
327 let mut total_size = 0;
328 total_size += types::CompactString.compute_size(&self.name)?;
329 total_size +=
330 types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
331 let num_tagged_fields = self.unknown_tagged_fields.len();
332 if num_tagged_fields > std::u32::MAX as usize {
333 bail!(
334 "Too many tagged fields to encode ({} fields)",
335 num_tagged_fields
336 );
337 }
338 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
339
340 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
341 Ok(total_size)
342 }
343}
344
345#[cfg(feature = "broker")]
346impl Decodable for ReassignableTopic {
347 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
348 let name = types::CompactString.decode(buf)?;
349 let partitions = types::CompactArray(types::Struct { version }).decode(buf)?;
350 let mut unknown_tagged_fields = BTreeMap::new();
351 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
352 for _ in 0..num_tagged_fields {
353 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
354 let size: u32 = types::UnsignedVarInt.decode(buf)?;
355 let unknown_value = buf.try_get_bytes(size as usize)?;
356 unknown_tagged_fields.insert(tag as i32, unknown_value);
357 }
358 Ok(Self {
359 name,
360 partitions,
361 unknown_tagged_fields,
362 })
363 }
364}
365
366impl Default for ReassignableTopic {
367 fn default() -> Self {
368 Self {
369 name: Default::default(),
370 partitions: Default::default(),
371 unknown_tagged_fields: BTreeMap::new(),
372 }
373 }
374}
375
376impl Message for ReassignableTopic {
377 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
378 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
379}
380
381impl HeaderVersion for AlterPartitionReassignmentsRequest {
382 fn header_version(version: i16) -> i16 {
383 2
384 }
385}