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