odradek_protocol/messages/
sasl_handshake_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 false
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct SaslHandshakeRequest {
31 pub mechanism: String,
33 pub unknown_tagged_fields: Vec<RawTaggedField>,
35}
36
37impl Default for SaslHandshakeRequest {
38 fn default() -> Self {
39 Self {
40 mechanism: String::new(),
41 unknown_tagged_fields: Vec::new(),
42 }
43 }
44}
45
46impl SaslHandshakeRequest {
47 pub const API_KEY: i16 = 17;
49 pub const MIN_VERSION: i16 = 0;
51 pub const MAX_VERSION: i16 = 1;
53
54 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
56 if is_flexible(version) {
57 wire::put_compact_string(buf, &self.mechanism);
58 } else {
59 wire::put_string(buf, &self.mechanism)?;
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.mechanism = if is_flexible(version) {
91 wire::get_compact_string(buf)?
92 } else {
93 wire::get_string(buf)?
94 };
95 if is_flexible(version) {
96 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
97 }
98 Ok(this)
99 }
100}
101
102impl crate::Message for SaslHandshakeRequest {
103 const API_KEY: i16 = 17;
104 const MIN_VERSION: i16 = 0;
105 const MAX_VERSION: i16 = 1;
106
107 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
108 SaslHandshakeRequest::encode(self, buf, version)
109 }
110 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
111 SaslHandshakeRequest::decode(buf, version)
112 }
113 fn decode_with_limits(
114 buf: &mut impl Buf,
115 version: i16,
116 limits: Limits,
117 ) -> Result<Self, DecodeError> {
118 SaslHandshakeRequest::decode_with_limits(buf, version, limits)
119 }
120}