odradek_protocol/messages/
sasl_authenticate_request.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 SaslAuthenticateRequest {
31 pub auth_bytes: bytes::Bytes,
33 pub unknown_tagged_fields: Vec<RawTaggedField>,
35}
36
37impl Default for SaslAuthenticateRequest {
38 fn default() -> Self {
39 Self {
40 auth_bytes: bytes::Bytes::new(),
41 unknown_tagged_fields: Vec::new(),
42 }
43 }
44}
45
46impl SaslAuthenticateRequest {
47 pub const API_KEY: i16 = 36;
49 pub const MIN_VERSION: i16 = 0;
51 pub const MAX_VERSION: i16 = 2;
53
54 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
56 if is_flexible(version) {
57 wire::put_compact_bytes(buf, &self.auth_bytes);
58 } else {
59 wire::put_bytes(buf, &self.auth_bytes)?;
60 }
61 if is_flexible(version) {
62 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
63 }
64 Ok(())
65 }
66
67 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
70 Self::decode_with_limits(buf, version, Limits::default())
71 }
72
73 pub fn decode_with_limits(
75 buf: &mut impl Buf,
76 version: i16,
77 limits: Limits,
78 ) -> Result<Self, DecodeError> {
79 let mut budget = limits.budget(buf.remaining());
80 Self::decode_with_budget(buf, version, &mut budget)
81 }
82
83 pub fn decode_with_budget(
85 buf: &mut impl Buf,
86 version: i16,
87 budget: &mut Budget,
88 ) -> Result<Self, DecodeError> {
89 let mut this = Self::default();
90 this.auth_bytes = {
91 let raw = if is_flexible(version) {
92 wire::get_compact_nullable_bytes(buf)?
93 } else {
94 wire::get_nullable_bytes(buf)?
95 };
96 match raw {
97 Some(v) => v,
98 None => return Err(DecodeError::InvalidLength(-1)),
99 }
100 };
101 if is_flexible(version) {
102 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
103 }
104 Ok(this)
105 }
106}
107
108impl crate::Message for SaslAuthenticateRequest {
109 const API_KEY: i16 = 36;
110 const MIN_VERSION: i16 = 0;
111 const MAX_VERSION: i16 = 2;
112
113 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
114 SaslAuthenticateRequest::encode(self, buf, version)
115 }
116 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
117 SaslAuthenticateRequest::decode(buf, version)
118 }
119 fn decode_with_limits(
120 buf: &mut impl Buf,
121 version: i16,
122 limits: Limits,
123 ) -> Result<Self, DecodeError> {
124 SaslAuthenticateRequest::decode_with_limits(buf, version, limits)
125 }
126}