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 DescribeClusterBroker {
24 pub broker_id: super::BrokerId,
28
29 pub host: StrBytes,
33
34 pub port: i32,
38
39 pub rack: Option<StrBytes>,
43
44 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl DescribeClusterBroker {
49 pub fn with_broker_id(mut self, value: super::BrokerId) -> Self {
55 self.broker_id = value;
56 self
57 }
58 pub fn with_host(mut self, value: StrBytes) -> Self {
64 self.host = value;
65 self
66 }
67 pub fn with_port(mut self, value: i32) -> Self {
73 self.port = value;
74 self
75 }
76 pub fn with_rack(mut self, value: Option<StrBytes>) -> Self {
82 self.rack = value;
83 self
84 }
85 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
87 self.unknown_tagged_fields = value;
88 self
89 }
90 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
92 self.unknown_tagged_fields.insert(key, value);
93 self
94 }
95}
96
97#[cfg(feature = "broker")]
98impl Encodable for DescribeClusterBroker {
99 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100 if version < 0 || version > 1 {
101 bail!("specified version not supported by this message type");
102 }
103 types::Int32.encode(buf, &self.broker_id)?;
104 types::CompactString.encode(buf, &self.host)?;
105 types::Int32.encode(buf, &self.port)?;
106 types::CompactString.encode(buf, &self.rack)?;
107 let num_tagged_fields = self.unknown_tagged_fields.len();
108 if num_tagged_fields > std::u32::MAX as usize {
109 bail!(
110 "Too many tagged fields to encode ({} fields)",
111 num_tagged_fields
112 );
113 }
114 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
115
116 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
117 Ok(())
118 }
119 fn compute_size(&self, version: i16) -> Result<usize> {
120 let mut total_size = 0;
121 total_size += types::Int32.compute_size(&self.broker_id)?;
122 total_size += types::CompactString.compute_size(&self.host)?;
123 total_size += types::Int32.compute_size(&self.port)?;
124 total_size += types::CompactString.compute_size(&self.rack)?;
125 let num_tagged_fields = self.unknown_tagged_fields.len();
126 if num_tagged_fields > std::u32::MAX as usize {
127 bail!(
128 "Too many tagged fields to encode ({} fields)",
129 num_tagged_fields
130 );
131 }
132 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
133
134 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
135 Ok(total_size)
136 }
137}
138
139#[cfg(feature = "client")]
140impl Decodable for DescribeClusterBroker {
141 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
142 if version < 0 || version > 1 {
143 bail!("specified version not supported by this message type");
144 }
145 let broker_id = types::Int32.decode(buf)?;
146 let host = types::CompactString.decode(buf)?;
147 let port = types::Int32.decode(buf)?;
148 let rack = types::CompactString.decode(buf)?;
149 let mut unknown_tagged_fields = BTreeMap::new();
150 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
151 for _ in 0..num_tagged_fields {
152 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
153 let size: u32 = types::UnsignedVarInt.decode(buf)?;
154 let unknown_value = buf.try_get_bytes(size as usize)?;
155 unknown_tagged_fields.insert(tag as i32, unknown_value);
156 }
157 Ok(Self {
158 broker_id,
159 host,
160 port,
161 rack,
162 unknown_tagged_fields,
163 })
164 }
165}
166
167impl Default for DescribeClusterBroker {
168 fn default() -> Self {
169 Self {
170 broker_id: (0).into(),
171 host: Default::default(),
172 port: 0,
173 rack: None,
174 unknown_tagged_fields: BTreeMap::new(),
175 }
176 }
177}
178
179impl Message for DescribeClusterBroker {
180 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
181 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
182}
183
184#[non_exhaustive]
186#[derive(Debug, Clone, PartialEq)]
187pub struct DescribeClusterResponse {
188 pub throttle_time_ms: i32,
192
193 pub error_code: i16,
197
198 pub error_message: Option<StrBytes>,
202
203 pub endpoint_type: i8,
207
208 pub cluster_id: StrBytes,
212
213 pub controller_id: super::BrokerId,
217
218 pub brokers: Vec<DescribeClusterBroker>,
222
223 pub cluster_authorized_operations: i32,
227
228 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
230}
231
232impl DescribeClusterResponse {
233 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
239 self.throttle_time_ms = value;
240 self
241 }
242 pub fn with_error_code(mut self, value: i16) -> Self {
248 self.error_code = value;
249 self
250 }
251 pub fn with_error_message(mut self, value: Option<StrBytes>) -> Self {
257 self.error_message = value;
258 self
259 }
260 pub fn with_endpoint_type(mut self, value: i8) -> Self {
266 self.endpoint_type = value;
267 self
268 }
269 pub fn with_cluster_id(mut self, value: StrBytes) -> Self {
275 self.cluster_id = value;
276 self
277 }
278 pub fn with_controller_id(mut self, value: super::BrokerId) -> Self {
284 self.controller_id = value;
285 self
286 }
287 pub fn with_brokers(mut self, value: Vec<DescribeClusterBroker>) -> Self {
293 self.brokers = value;
294 self
295 }
296 pub fn with_cluster_authorized_operations(mut self, value: i32) -> Self {
302 self.cluster_authorized_operations = value;
303 self
304 }
305 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
307 self.unknown_tagged_fields = value;
308 self
309 }
310 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
312 self.unknown_tagged_fields.insert(key, value);
313 self
314 }
315}
316
317#[cfg(feature = "broker")]
318impl Encodable for DescribeClusterResponse {
319 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
320 if version < 0 || version > 1 {
321 bail!("specified version not supported by this message type");
322 }
323 types::Int32.encode(buf, &self.throttle_time_ms)?;
324 types::Int16.encode(buf, &self.error_code)?;
325 types::CompactString.encode(buf, &self.error_message)?;
326 if version >= 1 {
327 types::Int8.encode(buf, &self.endpoint_type)?;
328 } else {
329 if self.endpoint_type != 1 {
330 bail!("A field is set that is not available on the selected protocol version");
331 }
332 }
333 types::CompactString.encode(buf, &self.cluster_id)?;
334 types::Int32.encode(buf, &self.controller_id)?;
335 types::CompactArray(types::Struct { version }).encode(buf, &self.brokers)?;
336 types::Int32.encode(buf, &self.cluster_authorized_operations)?;
337 let num_tagged_fields = self.unknown_tagged_fields.len();
338 if num_tagged_fields > std::u32::MAX as usize {
339 bail!(
340 "Too many tagged fields to encode ({} fields)",
341 num_tagged_fields
342 );
343 }
344 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
345
346 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
347 Ok(())
348 }
349 fn compute_size(&self, version: i16) -> Result<usize> {
350 let mut total_size = 0;
351 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
352 total_size += types::Int16.compute_size(&self.error_code)?;
353 total_size += types::CompactString.compute_size(&self.error_message)?;
354 if version >= 1 {
355 total_size += types::Int8.compute_size(&self.endpoint_type)?;
356 } else {
357 if self.endpoint_type != 1 {
358 bail!("A field is set that is not available on the selected protocol version");
359 }
360 }
361 total_size += types::CompactString.compute_size(&self.cluster_id)?;
362 total_size += types::Int32.compute_size(&self.controller_id)?;
363 total_size += types::CompactArray(types::Struct { version }).compute_size(&self.brokers)?;
364 total_size += types::Int32.compute_size(&self.cluster_authorized_operations)?;
365 let num_tagged_fields = self.unknown_tagged_fields.len();
366 if num_tagged_fields > std::u32::MAX as usize {
367 bail!(
368 "Too many tagged fields to encode ({} fields)",
369 num_tagged_fields
370 );
371 }
372 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
373
374 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
375 Ok(total_size)
376 }
377}
378
379#[cfg(feature = "client")]
380impl Decodable for DescribeClusterResponse {
381 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
382 if version < 0 || version > 1 {
383 bail!("specified version not supported by this message type");
384 }
385 let throttle_time_ms = types::Int32.decode(buf)?;
386 let error_code = types::Int16.decode(buf)?;
387 let error_message = types::CompactString.decode(buf)?;
388 let endpoint_type = if version >= 1 {
389 types::Int8.decode(buf)?
390 } else {
391 1
392 };
393 let cluster_id = types::CompactString.decode(buf)?;
394 let controller_id = types::Int32.decode(buf)?;
395 let brokers = types::CompactArray(types::Struct { version }).decode(buf)?;
396 let cluster_authorized_operations = types::Int32.decode(buf)?;
397 let mut unknown_tagged_fields = BTreeMap::new();
398 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
399 for _ in 0..num_tagged_fields {
400 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
401 let size: u32 = types::UnsignedVarInt.decode(buf)?;
402 let unknown_value = buf.try_get_bytes(size as usize)?;
403 unknown_tagged_fields.insert(tag as i32, unknown_value);
404 }
405 Ok(Self {
406 throttle_time_ms,
407 error_code,
408 error_message,
409 endpoint_type,
410 cluster_id,
411 controller_id,
412 brokers,
413 cluster_authorized_operations,
414 unknown_tagged_fields,
415 })
416 }
417}
418
419impl Default for DescribeClusterResponse {
420 fn default() -> Self {
421 Self {
422 throttle_time_ms: 0,
423 error_code: 0,
424 error_message: None,
425 endpoint_type: 1,
426 cluster_id: Default::default(),
427 controller_id: (-1).into(),
428 brokers: Default::default(),
429 cluster_authorized_operations: -2147483648,
430 unknown_tagged_fields: BTreeMap::new(),
431 }
432 }
433}
434
435impl Message for DescribeClusterResponse {
436 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
437 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
438}
439
440impl HeaderVersion for DescribeClusterResponse {
441 fn header_version(version: i16) -> i16 {
442 1
443 }
444}