odradek_protocol/messages/
sasl_handshake_response.rs1#![allow(
6 clippy::field_reassign_with_default,
7 clippy::nonminimal_bool,
8 clippy::derivable_impls,
9 clippy::borrow_deref_ref,
10 clippy::double_parens,
11 unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21pub fn is_flexible(version: i16) -> bool {
23 let _ = version;
24 false
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct SaslHandshakeResponse {
31 pub error_code: i16,
33 pub mechanisms: Vec<String>,
35 pub unknown_tagged_fields: Vec<RawTaggedField>,
37}
38
39impl Default for SaslHandshakeResponse {
40 fn default() -> Self {
41 Self {
42 error_code: 0,
43 mechanisms: Vec::new(),
44 unknown_tagged_fields: Vec::new(),
45 }
46 }
47}
48
49impl SaslHandshakeResponse {
50 pub const API_KEY: i16 = 17;
52 pub const MIN_VERSION: i16 = 0;
54 pub const MAX_VERSION: i16 = 1;
56
57 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
59 buf.put_i16(self.error_code);
60 if is_flexible(version) {
61 wire::put_compact_array_len(buf, Some(self.mechanisms.len()));
62 } else {
63 wire::put_array_len(buf, Some(self.mechanisms.len()))?;
64 }
65 for item in &self.mechanisms {
66 if is_flexible(version) {
67 wire::put_compact_string(buf, item);
68 } else {
69 wire::put_string(buf, item)?;
70 }
71 }
72 if is_flexible(version) {
73 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
74 }
75 Ok(())
76 }
77
78 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
81 Self::decode_with_limits(buf, version, Limits::default())
82 }
83
84 pub fn decode_with_limits(
86 buf: &mut impl Buf,
87 version: i16,
88 limits: Limits,
89 ) -> Result<Self, DecodeError> {
90 let mut budget = limits.budget(buf.remaining());
91 Self::decode_with_budget(buf, version, &mut budget)
92 }
93
94 pub fn decode_with_budget(
96 buf: &mut impl Buf,
97 version: i16,
98 budget: &mut Budget,
99 ) -> Result<Self, DecodeError> {
100 let mut this = Self::default();
101 this.error_code = wire::get_i16(buf)?;
102 this.mechanisms = {
103 let len = if is_flexible(version) {
104 wire::get_compact_array_len(buf)?
105 } else {
106 wire::get_array_len(buf)?
107 };
108 match len {
109 None => return Err(DecodeError::InvalidLength(-1)),
110 Some(n) => {
111 let mut items = Vec::new();
112 for _ in 0..n {
113 budget.push(
114 &mut items,
115 if is_flexible(version) {
116 wire::get_compact_string(buf)?
117 } else {
118 wire::get_string(buf)?
119 },
120 )?;
121 }
122 items
123 }
124 }
125 };
126 if is_flexible(version) {
127 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
128 }
129 Ok(this)
130 }
131}
132
133impl crate::Message for SaslHandshakeResponse {
134 const API_KEY: i16 = 17;
135 const MIN_VERSION: i16 = 0;
136 const MAX_VERSION: i16 = 1;
137
138 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
139 SaslHandshakeResponse::encode(self, buf, version)
140 }
141 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
142 SaslHandshakeResponse::decode(buf, version)
143 }
144 fn decode_with_limits(
145 buf: &mut impl Buf,
146 version: i16,
147 limits: Limits,
148 ) -> Result<Self, DecodeError> {
149 SaslHandshakeResponse::decode_with_limits(buf, version, limits)
150 }
151}