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 ApiVersion {
24 pub api_key: i16,
28
29 pub min_version: i16,
33
34 pub max_version: i16,
38
39 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl ApiVersion {
44 pub fn with_api_key(mut self, value: i16) -> Self {
50 self.api_key = value;
51 self
52 }
53 pub fn with_min_version(mut self, value: i16) -> Self {
59 self.min_version = value;
60 self
61 }
62 pub fn with_max_version(mut self, value: i16) -> Self {
68 self.max_version = 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 ApiVersion {
85 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
86 if version < 0 || version > 4 {
87 bail!("specified version not supported by this message type");
88 }
89 types::Int16.encode(buf, &self.api_key)?;
90 types::Int16.encode(buf, &self.min_version)?;
91 types::Int16.encode(buf, &self.max_version)?;
92 if version >= 3 {
93 let num_tagged_fields = self.unknown_tagged_fields.len();
94 if num_tagged_fields > std::u32::MAX as usize {
95 bail!(
96 "Too many tagged fields to encode ({} fields)",
97 num_tagged_fields
98 );
99 }
100 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
101
102 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
103 }
104 Ok(())
105 }
106 fn compute_size(&self, version: i16) -> Result<usize> {
107 let mut total_size = 0;
108 total_size += types::Int16.compute_size(&self.api_key)?;
109 total_size += types::Int16.compute_size(&self.min_version)?;
110 total_size += types::Int16.compute_size(&self.max_version)?;
111 if version >= 3 {
112 let num_tagged_fields = self.unknown_tagged_fields.len();
113 if num_tagged_fields > std::u32::MAX as usize {
114 bail!(
115 "Too many tagged fields to encode ({} fields)",
116 num_tagged_fields
117 );
118 }
119 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
120
121 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
122 }
123 Ok(total_size)
124 }
125}
126
127#[cfg(feature = "client")]
128impl Decodable for ApiVersion {
129 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
130 if version < 0 || version > 4 {
131 bail!("specified version not supported by this message type");
132 }
133 let api_key = types::Int16.decode(buf)?;
134 let min_version = types::Int16.decode(buf)?;
135 let max_version = types::Int16.decode(buf)?;
136 let mut unknown_tagged_fields = BTreeMap::new();
137 if version >= 3 {
138 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
139 for _ in 0..num_tagged_fields {
140 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
141 let size: u32 = types::UnsignedVarInt.decode(buf)?;
142 let unknown_value = buf.try_get_bytes(size as usize)?;
143 unknown_tagged_fields.insert(tag as i32, unknown_value);
144 }
145 }
146 Ok(Self {
147 api_key,
148 min_version,
149 max_version,
150 unknown_tagged_fields,
151 })
152 }
153}
154
155impl Default for ApiVersion {
156 fn default() -> Self {
157 Self {
158 api_key: 0,
159 min_version: 0,
160 max_version: 0,
161 unknown_tagged_fields: BTreeMap::new(),
162 }
163 }
164}
165
166impl Message for ApiVersion {
167 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
168 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
169}
170
171#[non_exhaustive]
173#[derive(Debug, Clone, PartialEq)]
174pub struct ApiVersionsResponse {
175 pub error_code: i16,
179
180 pub api_keys: Vec<ApiVersion>,
184
185 pub throttle_time_ms: i32,
189
190 pub supported_features: Vec<SupportedFeatureKey>,
194
195 pub finalized_features_epoch: i64,
199
200 pub finalized_features: Vec<FinalizedFeatureKey>,
204
205 pub zk_migration_ready: bool,
209
210 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
212}
213
214impl ApiVersionsResponse {
215 pub fn with_error_code(mut self, value: i16) -> Self {
221 self.error_code = value;
222 self
223 }
224 pub fn with_api_keys(mut self, value: Vec<ApiVersion>) -> Self {
230 self.api_keys = value;
231 self
232 }
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_supported_features(mut self, value: Vec<SupportedFeatureKey>) -> Self {
248 self.supported_features = value;
249 self
250 }
251 pub fn with_finalized_features_epoch(mut self, value: i64) -> Self {
257 self.finalized_features_epoch = value;
258 self
259 }
260 pub fn with_finalized_features(mut self, value: Vec<FinalizedFeatureKey>) -> Self {
266 self.finalized_features = value;
267 self
268 }
269 pub fn with_zk_migration_ready(mut self, value: bool) -> Self {
275 self.zk_migration_ready = value;
276 self
277 }
278 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
280 self.unknown_tagged_fields = value;
281 self
282 }
283 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
285 self.unknown_tagged_fields.insert(key, value);
286 self
287 }
288}
289
290#[cfg(feature = "broker")]
291impl Encodable for ApiVersionsResponse {
292 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
293 if version < 0 || version > 4 {
294 bail!("specified version not supported by this message type");
295 }
296 types::Int16.encode(buf, &self.error_code)?;
297 if version >= 3 {
298 types::CompactArray(types::Struct { version }).encode(buf, &self.api_keys)?;
299 } else {
300 types::Array(types::Struct { version }).encode(buf, &self.api_keys)?;
301 }
302 if version >= 1 {
303 types::Int32.encode(buf, &self.throttle_time_ms)?;
304 }
305 if version >= 3 {
306 let mut num_tagged_fields = self.unknown_tagged_fields.len();
307 if !self.supported_features.is_empty() {
308 num_tagged_fields += 1;
309 }
310 if self.finalized_features_epoch != -1 {
311 num_tagged_fields += 1;
312 }
313 if !self.finalized_features.is_empty() {
314 num_tagged_fields += 1;
315 }
316 if self.zk_migration_ready {
317 num_tagged_fields += 1;
318 }
319 if num_tagged_fields > std::u32::MAX as usize {
320 bail!(
321 "Too many tagged fields to encode ({} fields)",
322 num_tagged_fields
323 );
324 }
325 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
326 if !self.supported_features.is_empty() {
327 let computed_size = types::CompactArray(types::Struct { version })
328 .compute_size(&self.supported_features)?;
329 if computed_size > std::u32::MAX as usize {
330 bail!(
331 "Tagged field is too large to encode ({} bytes)",
332 computed_size
333 );
334 }
335 types::UnsignedVarInt.encode(buf, 0)?;
336 types::UnsignedVarInt.encode(buf, computed_size as u32)?;
337 types::CompactArray(types::Struct { version })
338 .encode(buf, &self.supported_features)?;
339 }
340 if self.finalized_features_epoch != -1 {
341 let computed_size = types::Int64.compute_size(&self.finalized_features_epoch)?;
342 if computed_size > std::u32::MAX as usize {
343 bail!(
344 "Tagged field is too large to encode ({} bytes)",
345 computed_size
346 );
347 }
348 types::UnsignedVarInt.encode(buf, 1)?;
349 types::UnsignedVarInt.encode(buf, computed_size as u32)?;
350 types::Int64.encode(buf, &self.finalized_features_epoch)?;
351 }
352 if !self.finalized_features.is_empty() {
353 let computed_size = types::CompactArray(types::Struct { version })
354 .compute_size(&self.finalized_features)?;
355 if computed_size > std::u32::MAX as usize {
356 bail!(
357 "Tagged field is too large to encode ({} bytes)",
358 computed_size
359 );
360 }
361 types::UnsignedVarInt.encode(buf, 2)?;
362 types::UnsignedVarInt.encode(buf, computed_size as u32)?;
363 types::CompactArray(types::Struct { version })
364 .encode(buf, &self.finalized_features)?;
365 }
366 if self.zk_migration_ready {
367 let computed_size = types::Boolean.compute_size(&self.zk_migration_ready)?;
368 if computed_size > std::u32::MAX as usize {
369 bail!(
370 "Tagged field is too large to encode ({} bytes)",
371 computed_size
372 );
373 }
374 types::UnsignedVarInt.encode(buf, 3)?;
375 types::UnsignedVarInt.encode(buf, computed_size as u32)?;
376 types::Boolean.encode(buf, &self.zk_migration_ready)?;
377 }
378
379 write_unknown_tagged_fields(buf, 4.., &self.unknown_tagged_fields)?;
380 }
381 Ok(())
382 }
383 fn compute_size(&self, version: i16) -> Result<usize> {
384 let mut total_size = 0;
385 total_size += types::Int16.compute_size(&self.error_code)?;
386 if version >= 3 {
387 total_size +=
388 types::CompactArray(types::Struct { version }).compute_size(&self.api_keys)?;
389 } else {
390 total_size += types::Array(types::Struct { version }).compute_size(&self.api_keys)?;
391 }
392 if version >= 1 {
393 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
394 }
395 if version >= 3 {
396 let mut num_tagged_fields = self.unknown_tagged_fields.len();
397 if !self.supported_features.is_empty() {
398 num_tagged_fields += 1;
399 }
400 if self.finalized_features_epoch != -1 {
401 num_tagged_fields += 1;
402 }
403 if !self.finalized_features.is_empty() {
404 num_tagged_fields += 1;
405 }
406 if self.zk_migration_ready {
407 num_tagged_fields += 1;
408 }
409 if num_tagged_fields > std::u32::MAX as usize {
410 bail!(
411 "Too many tagged fields to encode ({} fields)",
412 num_tagged_fields
413 );
414 }
415 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
416 if !self.supported_features.is_empty() {
417 let computed_size = types::CompactArray(types::Struct { version })
418 .compute_size(&self.supported_features)?;
419 if computed_size > std::u32::MAX as usize {
420 bail!(
421 "Tagged field is too large to encode ({} bytes)",
422 computed_size
423 );
424 }
425 total_size += types::UnsignedVarInt.compute_size(0)?;
426 total_size += types::UnsignedVarInt.compute_size(computed_size as u32)?;
427 total_size += computed_size;
428 }
429 if self.finalized_features_epoch != -1 {
430 let computed_size = types::Int64.compute_size(&self.finalized_features_epoch)?;
431 if computed_size > std::u32::MAX as usize {
432 bail!(
433 "Tagged field is too large to encode ({} bytes)",
434 computed_size
435 );
436 }
437 total_size += types::UnsignedVarInt.compute_size(1)?;
438 total_size += types::UnsignedVarInt.compute_size(computed_size as u32)?;
439 total_size += computed_size;
440 }
441 if !self.finalized_features.is_empty() {
442 let computed_size = types::CompactArray(types::Struct { version })
443 .compute_size(&self.finalized_features)?;
444 if computed_size > std::u32::MAX as usize {
445 bail!(
446 "Tagged field is too large to encode ({} bytes)",
447 computed_size
448 );
449 }
450 total_size += types::UnsignedVarInt.compute_size(2)?;
451 total_size += types::UnsignedVarInt.compute_size(computed_size as u32)?;
452 total_size += computed_size;
453 }
454 if self.zk_migration_ready {
455 let computed_size = types::Boolean.compute_size(&self.zk_migration_ready)?;
456 if computed_size > std::u32::MAX as usize {
457 bail!(
458 "Tagged field is too large to encode ({} bytes)",
459 computed_size
460 );
461 }
462 total_size += types::UnsignedVarInt.compute_size(3)?;
463 total_size += types::UnsignedVarInt.compute_size(computed_size as u32)?;
464 total_size += computed_size;
465 }
466
467 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
468 }
469 Ok(total_size)
470 }
471}
472
473#[cfg(feature = "client")]
474impl Decodable for ApiVersionsResponse {
475 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
476 if version < 0 || version > 4 {
477 bail!("specified version not supported by this message type");
478 }
479 let error_code = types::Int16.decode(buf)?;
480 let api_keys = if version >= 3 {
481 types::CompactArray(types::Struct { version }).decode(buf)?
482 } else {
483 types::Array(types::Struct { version }).decode(buf)?
484 };
485 let throttle_time_ms = if version >= 1 {
486 types::Int32.decode(buf)?
487 } else {
488 0
489 };
490 let mut supported_features = Default::default();
491 let mut finalized_features_epoch = -1;
492 let mut finalized_features = Default::default();
493 let mut zk_migration_ready = false;
494 let mut unknown_tagged_fields = BTreeMap::new();
495 if version >= 3 {
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 match tag {
501 0 => {
502 supported_features =
503 types::CompactArray(types::Struct { version }).decode(buf)?;
504 }
505 1 => {
506 finalized_features_epoch = types::Int64.decode(buf)?;
507 }
508 2 => {
509 finalized_features =
510 types::CompactArray(types::Struct { version }).decode(buf)?;
511 }
512 3 => {
513 zk_migration_ready = types::Boolean.decode(buf)?;
514 }
515 _ => {
516 let unknown_value = buf.try_get_bytes(size as usize)?;
517 unknown_tagged_fields.insert(tag as i32, unknown_value);
518 }
519 }
520 }
521 }
522 Ok(Self {
523 error_code,
524 api_keys,
525 throttle_time_ms,
526 supported_features,
527 finalized_features_epoch,
528 finalized_features,
529 zk_migration_ready,
530 unknown_tagged_fields,
531 })
532 }
533}
534
535impl Default for ApiVersionsResponse {
536 fn default() -> Self {
537 Self {
538 error_code: 0,
539 api_keys: Default::default(),
540 throttle_time_ms: 0,
541 supported_features: Default::default(),
542 finalized_features_epoch: -1,
543 finalized_features: Default::default(),
544 zk_migration_ready: false,
545 unknown_tagged_fields: BTreeMap::new(),
546 }
547 }
548}
549
550impl Message for ApiVersionsResponse {
551 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
552 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
553}
554
555#[non_exhaustive]
557#[derive(Debug, Clone, PartialEq)]
558pub struct FinalizedFeatureKey {
559 pub name: StrBytes,
563
564 pub max_version_level: i16,
568
569 pub min_version_level: i16,
573
574 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
576}
577
578impl FinalizedFeatureKey {
579 pub fn with_name(mut self, value: StrBytes) -> Self {
585 self.name = value;
586 self
587 }
588 pub fn with_max_version_level(mut self, value: i16) -> Self {
594 self.max_version_level = value;
595 self
596 }
597 pub fn with_min_version_level(mut self, value: i16) -> Self {
603 self.min_version_level = value;
604 self
605 }
606 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
608 self.unknown_tagged_fields = value;
609 self
610 }
611 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
613 self.unknown_tagged_fields.insert(key, value);
614 self
615 }
616}
617
618#[cfg(feature = "broker")]
619impl Encodable for FinalizedFeatureKey {
620 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
621 if version < 0 || version > 4 {
622 bail!("specified version not supported by this message type");
623 }
624 if version >= 3 {
625 types::CompactString.encode(buf, &self.name)?;
626 } else {
627 if !self.name.is_empty() {
628 bail!("A field is set that is not available on the selected protocol version");
629 }
630 }
631 if version >= 3 {
632 types::Int16.encode(buf, &self.max_version_level)?;
633 } else {
634 if self.max_version_level != 0 {
635 bail!("A field is set that is not available on the selected protocol version");
636 }
637 }
638 if version >= 3 {
639 types::Int16.encode(buf, &self.min_version_level)?;
640 } else {
641 if self.min_version_level != 0 {
642 bail!("A field is set that is not available on the selected protocol version");
643 }
644 }
645 if version >= 3 {
646 let num_tagged_fields = self.unknown_tagged_fields.len();
647 if num_tagged_fields > std::u32::MAX as usize {
648 bail!(
649 "Too many tagged fields to encode ({} fields)",
650 num_tagged_fields
651 );
652 }
653 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
654
655 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
656 }
657 Ok(())
658 }
659 fn compute_size(&self, version: i16) -> Result<usize> {
660 let mut total_size = 0;
661 if version >= 3 {
662 total_size += types::CompactString.compute_size(&self.name)?;
663 } else {
664 if !self.name.is_empty() {
665 bail!("A field is set that is not available on the selected protocol version");
666 }
667 }
668 if version >= 3 {
669 total_size += types::Int16.compute_size(&self.max_version_level)?;
670 } else {
671 if self.max_version_level != 0 {
672 bail!("A field is set that is not available on the selected protocol version");
673 }
674 }
675 if version >= 3 {
676 total_size += types::Int16.compute_size(&self.min_version_level)?;
677 } else {
678 if self.min_version_level != 0 {
679 bail!("A field is set that is not available on the selected protocol version");
680 }
681 }
682 if version >= 3 {
683 let num_tagged_fields = self.unknown_tagged_fields.len();
684 if num_tagged_fields > std::u32::MAX as usize {
685 bail!(
686 "Too many tagged fields to encode ({} fields)",
687 num_tagged_fields
688 );
689 }
690 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
691
692 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
693 }
694 Ok(total_size)
695 }
696}
697
698#[cfg(feature = "client")]
699impl Decodable for FinalizedFeatureKey {
700 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
701 if version < 0 || version > 4 {
702 bail!("specified version not supported by this message type");
703 }
704 let name = if version >= 3 {
705 types::CompactString.decode(buf)?
706 } else {
707 Default::default()
708 };
709 let max_version_level = if version >= 3 {
710 types::Int16.decode(buf)?
711 } else {
712 0
713 };
714 let min_version_level = if version >= 3 {
715 types::Int16.decode(buf)?
716 } else {
717 0
718 };
719 let mut unknown_tagged_fields = BTreeMap::new();
720 if version >= 3 {
721 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
722 for _ in 0..num_tagged_fields {
723 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
724 let size: u32 = types::UnsignedVarInt.decode(buf)?;
725 let unknown_value = buf.try_get_bytes(size as usize)?;
726 unknown_tagged_fields.insert(tag as i32, unknown_value);
727 }
728 }
729 Ok(Self {
730 name,
731 max_version_level,
732 min_version_level,
733 unknown_tagged_fields,
734 })
735 }
736}
737
738impl Default for FinalizedFeatureKey {
739 fn default() -> Self {
740 Self {
741 name: Default::default(),
742 max_version_level: 0,
743 min_version_level: 0,
744 unknown_tagged_fields: BTreeMap::new(),
745 }
746 }
747}
748
749impl Message for FinalizedFeatureKey {
750 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
751 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
752}
753
754#[non_exhaustive]
756#[derive(Debug, Clone, PartialEq)]
757pub struct SupportedFeatureKey {
758 pub name: StrBytes,
762
763 pub min_version: i16,
767
768 pub max_version: i16,
772
773 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
775}
776
777impl SupportedFeatureKey {
778 pub fn with_name(mut self, value: StrBytes) -> Self {
784 self.name = value;
785 self
786 }
787 pub fn with_min_version(mut self, value: i16) -> Self {
793 self.min_version = value;
794 self
795 }
796 pub fn with_max_version(mut self, value: i16) -> Self {
802 self.max_version = value;
803 self
804 }
805 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
807 self.unknown_tagged_fields = value;
808 self
809 }
810 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
812 self.unknown_tagged_fields.insert(key, value);
813 self
814 }
815}
816
817#[cfg(feature = "broker")]
818impl Encodable for SupportedFeatureKey {
819 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
820 if version < 0 || version > 4 {
821 bail!("specified version not supported by this message type");
822 }
823 if version >= 3 {
824 types::CompactString.encode(buf, &self.name)?;
825 } else {
826 if !self.name.is_empty() {
827 bail!("A field is set that is not available on the selected protocol version");
828 }
829 }
830 if version >= 3 {
831 types::Int16.encode(buf, &self.min_version)?;
832 } else {
833 if self.min_version != 0 {
834 bail!("A field is set that is not available on the selected protocol version");
835 }
836 }
837 if version >= 3 {
838 types::Int16.encode(buf, &self.max_version)?;
839 } else {
840 if self.max_version != 0 {
841 bail!("A field is set that is not available on the selected protocol version");
842 }
843 }
844 if version >= 3 {
845 let num_tagged_fields = self.unknown_tagged_fields.len();
846 if num_tagged_fields > std::u32::MAX as usize {
847 bail!(
848 "Too many tagged fields to encode ({} fields)",
849 num_tagged_fields
850 );
851 }
852 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
853
854 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
855 }
856 Ok(())
857 }
858 fn compute_size(&self, version: i16) -> Result<usize> {
859 let mut total_size = 0;
860 if version >= 3 {
861 total_size += types::CompactString.compute_size(&self.name)?;
862 } else {
863 if !self.name.is_empty() {
864 bail!("A field is set that is not available on the selected protocol version");
865 }
866 }
867 if version >= 3 {
868 total_size += types::Int16.compute_size(&self.min_version)?;
869 } else {
870 if self.min_version != 0 {
871 bail!("A field is set that is not available on the selected protocol version");
872 }
873 }
874 if version >= 3 {
875 total_size += types::Int16.compute_size(&self.max_version)?;
876 } else {
877 if self.max_version != 0 {
878 bail!("A field is set that is not available on the selected protocol version");
879 }
880 }
881 if version >= 3 {
882 let num_tagged_fields = self.unknown_tagged_fields.len();
883 if num_tagged_fields > std::u32::MAX as usize {
884 bail!(
885 "Too many tagged fields to encode ({} fields)",
886 num_tagged_fields
887 );
888 }
889 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
890
891 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
892 }
893 Ok(total_size)
894 }
895}
896
897#[cfg(feature = "client")]
898impl Decodable for SupportedFeatureKey {
899 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
900 if version < 0 || version > 4 {
901 bail!("specified version not supported by this message type");
902 }
903 let name = if version >= 3 {
904 types::CompactString.decode(buf)?
905 } else {
906 Default::default()
907 };
908 let min_version = if version >= 3 {
909 types::Int16.decode(buf)?
910 } else {
911 0
912 };
913 let max_version = if version >= 3 {
914 types::Int16.decode(buf)?
915 } else {
916 0
917 };
918 let mut unknown_tagged_fields = BTreeMap::new();
919 if version >= 3 {
920 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
921 for _ in 0..num_tagged_fields {
922 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
923 let size: u32 = types::UnsignedVarInt.decode(buf)?;
924 let unknown_value = buf.try_get_bytes(size as usize)?;
925 unknown_tagged_fields.insert(tag as i32, unknown_value);
926 }
927 }
928 Ok(Self {
929 name,
930 min_version,
931 max_version,
932 unknown_tagged_fields,
933 })
934 }
935}
936
937impl Default for SupportedFeatureKey {
938 fn default() -> Self {
939 Self {
940 name: Default::default(),
941 min_version: 0,
942 max_version: 0,
943 unknown_tagged_fields: BTreeMap::new(),
944 }
945 }
946}
947
948impl Message for SupportedFeatureKey {
949 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
950 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
951}
952
953impl HeaderVersion for ApiVersionsResponse {
954 fn header_version(version: i16) -> i16 {
955 0
956 }
957}