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 ElectLeadersResponse {
24 pub throttle_time_ms: i32,
28
29 pub error_code: i16,
33
34 pub replica_election_results: Vec<ReplicaElectionResult>,
38
39 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl ElectLeadersResponse {
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_replica_election_results(mut self, value: Vec<ReplicaElectionResult>) -> Self {
68 self.replica_election_results = 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 ElectLeadersResponse {
85 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
86 if version < 0 || version > 2 {
87 bail!("specified version not supported by this message type");
88 }
89 types::Int32.encode(buf, &self.throttle_time_ms)?;
90 if version >= 1 {
91 types::Int16.encode(buf, &self.error_code)?;
92 } else {
93 if self.error_code != 0 {
94 bail!("A field is set that is not available on the selected protocol version");
95 }
96 }
97 if version >= 2 {
98 types::CompactArray(types::Struct { version })
99 .encode(buf, &self.replica_election_results)?;
100 } else {
101 types::Array(types::Struct { version }).encode(buf, &self.replica_election_results)?;
102 }
103 if version >= 2 {
104 let num_tagged_fields = self.unknown_tagged_fields.len();
105 if num_tagged_fields > std::u32::MAX as usize {
106 bail!(
107 "Too many tagged fields to encode ({} fields)",
108 num_tagged_fields
109 );
110 }
111 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
112
113 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
114 }
115 Ok(())
116 }
117 fn compute_size(&self, version: i16) -> Result<usize> {
118 let mut total_size = 0;
119 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
120 if version >= 1 {
121 total_size += types::Int16.compute_size(&self.error_code)?;
122 } else {
123 if self.error_code != 0 {
124 bail!("A field is set that is not available on the selected protocol version");
125 }
126 }
127 if version >= 2 {
128 total_size += types::CompactArray(types::Struct { version })
129 .compute_size(&self.replica_election_results)?;
130 } else {
131 total_size += types::Array(types::Struct { version })
132 .compute_size(&self.replica_election_results)?;
133 }
134 if version >= 2 {
135 let num_tagged_fields = self.unknown_tagged_fields.len();
136 if num_tagged_fields > std::u32::MAX as usize {
137 bail!(
138 "Too many tagged fields to encode ({} fields)",
139 num_tagged_fields
140 );
141 }
142 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
143
144 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
145 }
146 Ok(total_size)
147 }
148}
149
150#[cfg(feature = "client")]
151impl Decodable for ElectLeadersResponse {
152 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
153 if version < 0 || version > 2 {
154 bail!("specified version not supported by this message type");
155 }
156 let throttle_time_ms = types::Int32.decode(buf)?;
157 let error_code = if version >= 1 {
158 types::Int16.decode(buf)?
159 } else {
160 0
161 };
162 let replica_election_results = if version >= 2 {
163 types::CompactArray(types::Struct { version }).decode(buf)?
164 } else {
165 types::Array(types::Struct { version }).decode(buf)?
166 };
167 let mut unknown_tagged_fields = BTreeMap::new();
168 if version >= 2 {
169 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
170 for _ in 0..num_tagged_fields {
171 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
172 let size: u32 = types::UnsignedVarInt.decode(buf)?;
173 let unknown_value = buf.try_get_bytes(size as usize)?;
174 unknown_tagged_fields.insert(tag as i32, unknown_value);
175 }
176 }
177 Ok(Self {
178 throttle_time_ms,
179 error_code,
180 replica_election_results,
181 unknown_tagged_fields,
182 })
183 }
184}
185
186impl Default for ElectLeadersResponse {
187 fn default() -> Self {
188 Self {
189 throttle_time_ms: 0,
190 error_code: 0,
191 replica_election_results: Default::default(),
192 unknown_tagged_fields: BTreeMap::new(),
193 }
194 }
195}
196
197impl Message for ElectLeadersResponse {
198 const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
199 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
200}
201
202#[non_exhaustive]
204#[derive(Debug, Clone, PartialEq)]
205pub struct PartitionResult {
206 pub partition_id: i32,
210
211 pub error_code: i16,
215
216 pub error_message: Option<StrBytes>,
220
221 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
223}
224
225impl PartitionResult {
226 pub fn with_partition_id(mut self, value: i32) -> Self {
232 self.partition_id = value;
233 self
234 }
235 pub fn with_error_code(mut self, value: i16) -> Self {
241 self.error_code = value;
242 self
243 }
244 pub fn with_error_message(mut self, value: Option<StrBytes>) -> Self {
250 self.error_message = value;
251 self
252 }
253 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
255 self.unknown_tagged_fields = value;
256 self
257 }
258 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
260 self.unknown_tagged_fields.insert(key, value);
261 self
262 }
263}
264
265#[cfg(feature = "broker")]
266impl Encodable for PartitionResult {
267 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
268 if version < 0 || version > 2 {
269 bail!("specified version not supported by this message type");
270 }
271 types::Int32.encode(buf, &self.partition_id)?;
272 types::Int16.encode(buf, &self.error_code)?;
273 if version >= 2 {
274 types::CompactString.encode(buf, &self.error_message)?;
275 } else {
276 types::String.encode(buf, &self.error_message)?;
277 }
278 if version >= 2 {
279 let num_tagged_fields = self.unknown_tagged_fields.len();
280 if num_tagged_fields > std::u32::MAX as usize {
281 bail!(
282 "Too many tagged fields to encode ({} fields)",
283 num_tagged_fields
284 );
285 }
286 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
287
288 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
289 }
290 Ok(())
291 }
292 fn compute_size(&self, version: i16) -> Result<usize> {
293 let mut total_size = 0;
294 total_size += types::Int32.compute_size(&self.partition_id)?;
295 total_size += types::Int16.compute_size(&self.error_code)?;
296 if version >= 2 {
297 total_size += types::CompactString.compute_size(&self.error_message)?;
298 } else {
299 total_size += types::String.compute_size(&self.error_message)?;
300 }
301 if version >= 2 {
302 let num_tagged_fields = self.unknown_tagged_fields.len();
303 if num_tagged_fields > std::u32::MAX as usize {
304 bail!(
305 "Too many tagged fields to encode ({} fields)",
306 num_tagged_fields
307 );
308 }
309 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
310
311 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
312 }
313 Ok(total_size)
314 }
315}
316
317#[cfg(feature = "client")]
318impl Decodable for PartitionResult {
319 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
320 if version < 0 || version > 2 {
321 bail!("specified version not supported by this message type");
322 }
323 let partition_id = types::Int32.decode(buf)?;
324 let error_code = types::Int16.decode(buf)?;
325 let error_message = if version >= 2 {
326 types::CompactString.decode(buf)?
327 } else {
328 types::String.decode(buf)?
329 };
330 let mut unknown_tagged_fields = BTreeMap::new();
331 if version >= 2 {
332 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
333 for _ in 0..num_tagged_fields {
334 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
335 let size: u32 = types::UnsignedVarInt.decode(buf)?;
336 let unknown_value = buf.try_get_bytes(size as usize)?;
337 unknown_tagged_fields.insert(tag as i32, unknown_value);
338 }
339 }
340 Ok(Self {
341 partition_id,
342 error_code,
343 error_message,
344 unknown_tagged_fields,
345 })
346 }
347}
348
349impl Default for PartitionResult {
350 fn default() -> Self {
351 Self {
352 partition_id: 0,
353 error_code: 0,
354 error_message: Some(Default::default()),
355 unknown_tagged_fields: BTreeMap::new(),
356 }
357 }
358}
359
360impl Message for PartitionResult {
361 const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
362 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
363}
364
365#[non_exhaustive]
367#[derive(Debug, Clone, PartialEq)]
368pub struct ReplicaElectionResult {
369 pub topic: super::TopicName,
373
374 pub partition_result: Vec<PartitionResult>,
378
379 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
381}
382
383impl ReplicaElectionResult {
384 pub fn with_topic(mut self, value: super::TopicName) -> Self {
390 self.topic = value;
391 self
392 }
393 pub fn with_partition_result(mut self, value: Vec<PartitionResult>) -> Self {
399 self.partition_result = value;
400 self
401 }
402 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
404 self.unknown_tagged_fields = value;
405 self
406 }
407 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
409 self.unknown_tagged_fields.insert(key, value);
410 self
411 }
412}
413
414#[cfg(feature = "broker")]
415impl Encodable for ReplicaElectionResult {
416 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
417 if version < 0 || version > 2 {
418 bail!("specified version not supported by this message type");
419 }
420 if version >= 2 {
421 types::CompactString.encode(buf, &self.topic)?;
422 } else {
423 types::String.encode(buf, &self.topic)?;
424 }
425 if version >= 2 {
426 types::CompactArray(types::Struct { version }).encode(buf, &self.partition_result)?;
427 } else {
428 types::Array(types::Struct { version }).encode(buf, &self.partition_result)?;
429 }
430 if version >= 2 {
431 let num_tagged_fields = self.unknown_tagged_fields.len();
432 if num_tagged_fields > std::u32::MAX as usize {
433 bail!(
434 "Too many tagged fields to encode ({} fields)",
435 num_tagged_fields
436 );
437 }
438 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
439
440 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
441 }
442 Ok(())
443 }
444 fn compute_size(&self, version: i16) -> Result<usize> {
445 let mut total_size = 0;
446 if version >= 2 {
447 total_size += types::CompactString.compute_size(&self.topic)?;
448 } else {
449 total_size += types::String.compute_size(&self.topic)?;
450 }
451 if version >= 2 {
452 total_size += types::CompactArray(types::Struct { version })
453 .compute_size(&self.partition_result)?;
454 } else {
455 total_size +=
456 types::Array(types::Struct { version }).compute_size(&self.partition_result)?;
457 }
458 if version >= 2 {
459 let num_tagged_fields = self.unknown_tagged_fields.len();
460 if num_tagged_fields > std::u32::MAX as usize {
461 bail!(
462 "Too many tagged fields to encode ({} fields)",
463 num_tagged_fields
464 );
465 }
466 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
467
468 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
469 }
470 Ok(total_size)
471 }
472}
473
474#[cfg(feature = "client")]
475impl Decodable for ReplicaElectionResult {
476 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
477 if version < 0 || version > 2 {
478 bail!("specified version not supported by this message type");
479 }
480 let topic = if version >= 2 {
481 types::CompactString.decode(buf)?
482 } else {
483 types::String.decode(buf)?
484 };
485 let partition_result = if version >= 2 {
486 types::CompactArray(types::Struct { version }).decode(buf)?
487 } else {
488 types::Array(types::Struct { version }).decode(buf)?
489 };
490 let mut unknown_tagged_fields = BTreeMap::new();
491 if version >= 2 {
492 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
493 for _ in 0..num_tagged_fields {
494 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
495 let size: u32 = types::UnsignedVarInt.decode(buf)?;
496 let unknown_value = buf.try_get_bytes(size as usize)?;
497 unknown_tagged_fields.insert(tag as i32, unknown_value);
498 }
499 }
500 Ok(Self {
501 topic,
502 partition_result,
503 unknown_tagged_fields,
504 })
505 }
506}
507
508impl Default for ReplicaElectionResult {
509 fn default() -> Self {
510 Self {
511 topic: Default::default(),
512 partition_result: Default::default(),
513 unknown_tagged_fields: BTreeMap::new(),
514 }
515 }
516}
517
518impl Message for ReplicaElectionResult {
519 const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
520 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
521}
522
523impl HeaderVersion for ElectLeadersResponse {
524 fn header_version(version: i16) -> i16 {
525 if version >= 2 {
526 1
527 } else {
528 0
529 }
530 }
531}