odradek_protocol/messages/
sasl_authenticate_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 version >= 2
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct SaslAuthenticateResponse {
31 pub error_code: i16,
33 pub error_message: Option<String>,
35 pub auth_bytes: bytes::Bytes,
37 pub session_lifetime_ms: i64,
39 pub unknown_tagged_fields: Vec<RawTaggedField>,
41}
42
43impl Default for SaslAuthenticateResponse {
44 fn default() -> Self {
45 Self {
46 error_code: 0,
47 error_message: Some(String::new()),
48 auth_bytes: bytes::Bytes::new(),
49 session_lifetime_ms: 0,
50 unknown_tagged_fields: Vec::new(),
51 }
52 }
53}
54
55impl SaslAuthenticateResponse {
56 pub const API_KEY: i16 = 36;
58 pub const MIN_VERSION: i16 = 0;
60 pub const MAX_VERSION: i16 = 2;
62
63 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
65 buf.put_i16(self.error_code);
66 if is_flexible(version) {
67 wire::put_compact_nullable_string(buf, self.error_message.as_deref());
68 } else {
69 wire::put_nullable_string(buf, self.error_message.as_deref())?;
70 }
71 if is_flexible(version) {
72 wire::put_compact_bytes(buf, &self.auth_bytes);
73 } else {
74 wire::put_bytes(buf, &self.auth_bytes)?;
75 }
76 if version >= 1 {
77 buf.put_i64(self.session_lifetime_ms);
78 }
79 if is_flexible(version) {
80 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
81 }
82 Ok(())
83 }
84
85 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
88 Self::decode_with_limits(buf, version, Limits::default())
89 }
90
91 pub fn decode_with_limits(
93 buf: &mut impl Buf,
94 version: i16,
95 limits: Limits,
96 ) -> Result<Self, DecodeError> {
97 let mut budget = limits.budget(buf.remaining());
98 Self::decode_with_budget(buf, version, &mut budget)
99 }
100
101 pub fn decode_with_budget(
103 buf: &mut impl Buf,
104 version: i16,
105 budget: &mut Budget,
106 ) -> Result<Self, DecodeError> {
107 let mut this = Self::default();
108 this.error_code = wire::get_i16(buf)?;
109 this.error_message = if is_flexible(version) {
110 wire::get_compact_nullable_string(buf)?
111 } else {
112 wire::get_nullable_string(buf)?
113 };
114 this.auth_bytes = {
115 let raw = if is_flexible(version) {
116 wire::get_compact_nullable_bytes(buf)?
117 } else {
118 wire::get_nullable_bytes(buf)?
119 };
120 match raw {
121 Some(v) => v,
122 None => return Err(DecodeError::InvalidLength(-1)),
123 }
124 };
125 if version >= 1 {
126 this.session_lifetime_ms = wire::get_i64(buf)?;
127 }
128 if is_flexible(version) {
129 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
130 }
131 Ok(this)
132 }
133}
134
135impl crate::Message for SaslAuthenticateResponse {
136 const API_KEY: i16 = 36;
137 const MIN_VERSION: i16 = 0;
138 const MAX_VERSION: i16 = 2;
139
140 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
141 SaslAuthenticateResponse::encode(self, buf, version)
142 }
143 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
144 SaslAuthenticateResponse::decode(buf, version)
145 }
146 fn decode_with_limits(
147 buf: &mut impl Buf,
148 version: i16,
149 limits: Limits,
150 ) -> Result<Self, DecodeError> {
151 SaslAuthenticateResponse::decode_with_limits(buf, version, limits)
152 }
153}