Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
SaslHandshakeRequest.borrowed.rs

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