krabka_protocol/opt/rustwide/workdir/generated/
SaslHandshakeRequest.borrowed.rs1use bytes::BufMut;
3
4use crate::{
5 DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6 owned::sasl_handshake_request,
7 primitives::{
8 string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
9 string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
10 },
11};
12pub const API_KEY: i16 = 17;
13pub const MIN_VERSION: i16 = 0;
14pub const MAX_VERSION: i16 = 1;
15pub const LATEST_STABLE_VERSION: i16 = 1;
16pub const FLEXIBLE_MIN: i16 = 32767;
17#[inline]
18#[must_use]
19pub fn is_flexible(version: i16) -> bool {
20 version == FLEXIBLE_MIN
21}
22#[derive(Debug, Clone, PartialEq, Eq, Default)]
23pub struct SaslHandshakeRequest<'a> {
24 pub mechanism: &'a str,
25 pub unknown_tagged_fields: UnknownTaggedFields,
26}
27impl SaslHandshakeRequest<'_> {
28 #[must_use]
29 pub fn to_owned(&self) -> sasl_handshake_request::SaslHandshakeRequest {
30 sasl_handshake_request::SaslHandshakeRequest {
31 mechanism: (self.mechanism).to_owned(),
32 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
33 }
34 }
35}
36impl Encode for SaslHandshakeRequest<'_> {
37 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
38 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
39 return Err(ProtocolError::UnsupportedVersion {
40 api_key: API_KEY,
41 version,
42 });
43 }
44 let flex = is_flexible(version);
45 if version >= 0 {
46 if flex {
47 put_compact_string(buf, self.mechanism)?;
48 } else {
49 put_string(buf, self.mechanism)?;
50 }
51 }
52 Ok(())
53 }
54 fn encoded_len(&self, version: i16) -> usize {
55 let flex = is_flexible(version);
56 let mut n: usize = 0;
57 if version >= 0 {
58 n += if flex {
59 compact_string_len(self.mechanism)
60 } else {
61 string_len(self.mechanism)
62 };
63 }
64 n
65 }
66}
67impl<'de> DecodeBorrow<'de> for SaslHandshakeRequest<'de> {
68 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
69 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
70 return Err(ProtocolError::UnsupportedVersion {
71 api_key: API_KEY,
72 version,
73 });
74 }
75 let flex = is_flexible(version);
76 let mut out = Self::default();
77 if version >= 0 {
78 out.mechanism = if flex {
79 get_compact_string_borrowed(buf)?
80 } else {
81 get_string_borrowed(buf)?
82 };
83 }
84 Ok(out)
85 }
86}
87#[cfg(test)]
88impl SaslHandshakeRequest<'_> {
89 #[must_use]
90 pub fn populated(version: i16) -> Self {
91 let mut m = Self::default();
92 if version >= 0 {
93 m.mechanism = "x";
94 }
95 m
96 }
97}