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