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 AclDescription {
24 pub principal: StrBytes,
28
29 pub host: StrBytes,
33
34 pub operation: i8,
38
39 pub permission_type: i8,
43
44 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl AclDescription {
49 pub fn with_principal(mut self, value: StrBytes) -> Self {
55 self.principal = 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_operation(mut self, value: i8) -> Self {
73 self.operation = value;
74 self
75 }
76 pub fn with_permission_type(mut self, value: i8) -> Self {
82 self.permission_type = 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 AclDescription {
99 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100 if version < 0 || version > 3 {
101 bail!("specified version not supported by this message type");
102 }
103 if version >= 2 {
104 types::CompactString.encode(buf, &self.principal)?;
105 } else {
106 types::String.encode(buf, &self.principal)?;
107 }
108 if version >= 2 {
109 types::CompactString.encode(buf, &self.host)?;
110 } else {
111 types::String.encode(buf, &self.host)?;
112 }
113 types::Int8.encode(buf, &self.operation)?;
114 types::Int8.encode(buf, &self.permission_type)?;
115 if version >= 2 {
116 let num_tagged_fields = self.unknown_tagged_fields.len();
117 if num_tagged_fields > std::u32::MAX as usize {
118 bail!(
119 "Too many tagged fields to encode ({} fields)",
120 num_tagged_fields
121 );
122 }
123 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
124
125 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
126 }
127 Ok(())
128 }
129 fn compute_size(&self, version: i16) -> Result<usize> {
130 let mut total_size = 0;
131 if version >= 2 {
132 total_size += types::CompactString.compute_size(&self.principal)?;
133 } else {
134 total_size += types::String.compute_size(&self.principal)?;
135 }
136 if version >= 2 {
137 total_size += types::CompactString.compute_size(&self.host)?;
138 } else {
139 total_size += types::String.compute_size(&self.host)?;
140 }
141 total_size += types::Int8.compute_size(&self.operation)?;
142 total_size += types::Int8.compute_size(&self.permission_type)?;
143 if version >= 2 {
144 let num_tagged_fields = self.unknown_tagged_fields.len();
145 if num_tagged_fields > std::u32::MAX as usize {
146 bail!(
147 "Too many tagged fields to encode ({} fields)",
148 num_tagged_fields
149 );
150 }
151 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
152
153 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
154 }
155 Ok(total_size)
156 }
157}
158
159#[cfg(feature = "client")]
160impl Decodable for AclDescription {
161 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
162 if version < 0 || version > 3 {
163 bail!("specified version not supported by this message type");
164 }
165 let principal = if version >= 2 {
166 types::CompactString.decode(buf)?
167 } else {
168 types::String.decode(buf)?
169 };
170 let host = if version >= 2 {
171 types::CompactString.decode(buf)?
172 } else {
173 types::String.decode(buf)?
174 };
175 let operation = types::Int8.decode(buf)?;
176 let permission_type = types::Int8.decode(buf)?;
177 let mut unknown_tagged_fields = BTreeMap::new();
178 if version >= 2 {
179 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
180 for _ in 0..num_tagged_fields {
181 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
182 let size: u32 = types::UnsignedVarInt.decode(buf)?;
183 let unknown_value = buf.try_get_bytes(size as usize)?;
184 unknown_tagged_fields.insert(tag as i32, unknown_value);
185 }
186 }
187 Ok(Self {
188 principal,
189 host,
190 operation,
191 permission_type,
192 unknown_tagged_fields,
193 })
194 }
195}
196
197impl Default for AclDescription {
198 fn default() -> Self {
199 Self {
200 principal: Default::default(),
201 host: Default::default(),
202 operation: 0,
203 permission_type: 0,
204 unknown_tagged_fields: BTreeMap::new(),
205 }
206 }
207}
208
209impl Message for AclDescription {
210 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
211 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
212}
213
214#[non_exhaustive]
216#[derive(Debug, Clone, PartialEq)]
217pub struct DescribeAclsResource {
218 pub resource_type: i8,
222
223 pub resource_name: StrBytes,
227
228 pub pattern_type: i8,
232
233 pub acls: Vec<AclDescription>,
237
238 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
240}
241
242impl DescribeAclsResource {
243 pub fn with_resource_type(mut self, value: i8) -> Self {
249 self.resource_type = value;
250 self
251 }
252 pub fn with_resource_name(mut self, value: StrBytes) -> Self {
258 self.resource_name = value;
259 self
260 }
261 pub fn with_pattern_type(mut self, value: i8) -> Self {
267 self.pattern_type = value;
268 self
269 }
270 pub fn with_acls(mut self, value: Vec<AclDescription>) -> Self {
276 self.acls = value;
277 self
278 }
279 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
281 self.unknown_tagged_fields = value;
282 self
283 }
284 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
286 self.unknown_tagged_fields.insert(key, value);
287 self
288 }
289}
290
291#[cfg(feature = "broker")]
292impl Encodable for DescribeAclsResource {
293 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
294 if version < 0 || version > 3 {
295 bail!("specified version not supported by this message type");
296 }
297 types::Int8.encode(buf, &self.resource_type)?;
298 if version >= 2 {
299 types::CompactString.encode(buf, &self.resource_name)?;
300 } else {
301 types::String.encode(buf, &self.resource_name)?;
302 }
303 if version >= 1 {
304 types::Int8.encode(buf, &self.pattern_type)?;
305 } else {
306 if self.pattern_type != 3 {
307 bail!("A field is set that is not available on the selected protocol version");
308 }
309 }
310 if version >= 2 {
311 types::CompactArray(types::Struct { version }).encode(buf, &self.acls)?;
312 } else {
313 types::Array(types::Struct { version }).encode(buf, &self.acls)?;
314 }
315 if version >= 2 {
316 let num_tagged_fields = self.unknown_tagged_fields.len();
317 if num_tagged_fields > std::u32::MAX as usize {
318 bail!(
319 "Too many tagged fields to encode ({} fields)",
320 num_tagged_fields
321 );
322 }
323 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
324
325 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
326 }
327 Ok(())
328 }
329 fn compute_size(&self, version: i16) -> Result<usize> {
330 let mut total_size = 0;
331 total_size += types::Int8.compute_size(&self.resource_type)?;
332 if version >= 2 {
333 total_size += types::CompactString.compute_size(&self.resource_name)?;
334 } else {
335 total_size += types::String.compute_size(&self.resource_name)?;
336 }
337 if version >= 1 {
338 total_size += types::Int8.compute_size(&self.pattern_type)?;
339 } else {
340 if self.pattern_type != 3 {
341 bail!("A field is set that is not available on the selected protocol version");
342 }
343 }
344 if version >= 2 {
345 total_size +=
346 types::CompactArray(types::Struct { version }).compute_size(&self.acls)?;
347 } else {
348 total_size += types::Array(types::Struct { version }).compute_size(&self.acls)?;
349 }
350 if version >= 2 {
351 let num_tagged_fields = self.unknown_tagged_fields.len();
352 if num_tagged_fields > std::u32::MAX as usize {
353 bail!(
354 "Too many tagged fields to encode ({} fields)",
355 num_tagged_fields
356 );
357 }
358 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
359
360 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
361 }
362 Ok(total_size)
363 }
364}
365
366#[cfg(feature = "client")]
367impl Decodable for DescribeAclsResource {
368 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
369 if version < 0 || version > 3 {
370 bail!("specified version not supported by this message type");
371 }
372 let resource_type = types::Int8.decode(buf)?;
373 let resource_name = if version >= 2 {
374 types::CompactString.decode(buf)?
375 } else {
376 types::String.decode(buf)?
377 };
378 let pattern_type = if version >= 1 {
379 types::Int8.decode(buf)?
380 } else {
381 3
382 };
383 let acls = if version >= 2 {
384 types::CompactArray(types::Struct { version }).decode(buf)?
385 } else {
386 types::Array(types::Struct { version }).decode(buf)?
387 };
388 let mut unknown_tagged_fields = BTreeMap::new();
389 if version >= 2 {
390 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
391 for _ in 0..num_tagged_fields {
392 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
393 let size: u32 = types::UnsignedVarInt.decode(buf)?;
394 let unknown_value = buf.try_get_bytes(size as usize)?;
395 unknown_tagged_fields.insert(tag as i32, unknown_value);
396 }
397 }
398 Ok(Self {
399 resource_type,
400 resource_name,
401 pattern_type,
402 acls,
403 unknown_tagged_fields,
404 })
405 }
406}
407
408impl Default for DescribeAclsResource {
409 fn default() -> Self {
410 Self {
411 resource_type: 0,
412 resource_name: Default::default(),
413 pattern_type: 3,
414 acls: Default::default(),
415 unknown_tagged_fields: BTreeMap::new(),
416 }
417 }
418}
419
420impl Message for DescribeAclsResource {
421 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
422 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
423}
424
425#[non_exhaustive]
427#[derive(Debug, Clone, PartialEq)]
428pub struct DescribeAclsResponse {
429 pub throttle_time_ms: i32,
433
434 pub error_code: i16,
438
439 pub error_message: Option<StrBytes>,
443
444 pub resources: Vec<DescribeAclsResource>,
448
449 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
451}
452
453impl DescribeAclsResponse {
454 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
460 self.throttle_time_ms = value;
461 self
462 }
463 pub fn with_error_code(mut self, value: i16) -> Self {
469 self.error_code = value;
470 self
471 }
472 pub fn with_error_message(mut self, value: Option<StrBytes>) -> Self {
478 self.error_message = value;
479 self
480 }
481 pub fn with_resources(mut self, value: Vec<DescribeAclsResource>) -> Self {
487 self.resources = value;
488 self
489 }
490 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
492 self.unknown_tagged_fields = value;
493 self
494 }
495 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
497 self.unknown_tagged_fields.insert(key, value);
498 self
499 }
500}
501
502#[cfg(feature = "broker")]
503impl Encodable for DescribeAclsResponse {
504 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
505 if version < 0 || version > 3 {
506 bail!("specified version not supported by this message type");
507 }
508 types::Int32.encode(buf, &self.throttle_time_ms)?;
509 types::Int16.encode(buf, &self.error_code)?;
510 if version >= 2 {
511 types::CompactString.encode(buf, &self.error_message)?;
512 } else {
513 types::String.encode(buf, &self.error_message)?;
514 }
515 if version >= 2 {
516 types::CompactArray(types::Struct { version }).encode(buf, &self.resources)?;
517 } else {
518 types::Array(types::Struct { version }).encode(buf, &self.resources)?;
519 }
520 if version >= 2 {
521 let num_tagged_fields = self.unknown_tagged_fields.len();
522 if num_tagged_fields > std::u32::MAX as usize {
523 bail!(
524 "Too many tagged fields to encode ({} fields)",
525 num_tagged_fields
526 );
527 }
528 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
529
530 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
531 }
532 Ok(())
533 }
534 fn compute_size(&self, version: i16) -> Result<usize> {
535 let mut total_size = 0;
536 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
537 total_size += types::Int16.compute_size(&self.error_code)?;
538 if version >= 2 {
539 total_size += types::CompactString.compute_size(&self.error_message)?;
540 } else {
541 total_size += types::String.compute_size(&self.error_message)?;
542 }
543 if version >= 2 {
544 total_size +=
545 types::CompactArray(types::Struct { version }).compute_size(&self.resources)?;
546 } else {
547 total_size += types::Array(types::Struct { version }).compute_size(&self.resources)?;
548 }
549 if version >= 2 {
550 let num_tagged_fields = self.unknown_tagged_fields.len();
551 if num_tagged_fields > std::u32::MAX as usize {
552 bail!(
553 "Too many tagged fields to encode ({} fields)",
554 num_tagged_fields
555 );
556 }
557 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
558
559 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
560 }
561 Ok(total_size)
562 }
563}
564
565#[cfg(feature = "client")]
566impl Decodable for DescribeAclsResponse {
567 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
568 if version < 0 || version > 3 {
569 bail!("specified version not supported by this message type");
570 }
571 let throttle_time_ms = types::Int32.decode(buf)?;
572 let error_code = types::Int16.decode(buf)?;
573 let error_message = if version >= 2 {
574 types::CompactString.decode(buf)?
575 } else {
576 types::String.decode(buf)?
577 };
578 let resources = if version >= 2 {
579 types::CompactArray(types::Struct { version }).decode(buf)?
580 } else {
581 types::Array(types::Struct { version }).decode(buf)?
582 };
583 let mut unknown_tagged_fields = BTreeMap::new();
584 if version >= 2 {
585 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
586 for _ in 0..num_tagged_fields {
587 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
588 let size: u32 = types::UnsignedVarInt.decode(buf)?;
589 let unknown_value = buf.try_get_bytes(size as usize)?;
590 unknown_tagged_fields.insert(tag as i32, unknown_value);
591 }
592 }
593 Ok(Self {
594 throttle_time_ms,
595 error_code,
596 error_message,
597 resources,
598 unknown_tagged_fields,
599 })
600 }
601}
602
603impl Default for DescribeAclsResponse {
604 fn default() -> Self {
605 Self {
606 throttle_time_ms: 0,
607 error_code: 0,
608 error_message: Some(Default::default()),
609 resources: Default::default(),
610 unknown_tagged_fields: BTreeMap::new(),
611 }
612 }
613}
614
615impl Message for DescribeAclsResponse {
616 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
617 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
618}
619
620impl HeaderVersion for DescribeAclsResponse {
621 fn header_version(version: i16) -> i16 {
622 if version >= 2 {
623 1
624 } else {
625 0
626 }
627 }
628}