Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Bytes, BufMut};
4
5use crate::primitives::fixed::{get_i16, get_i64, put_i16, put_i64};
6use crate::primitives::string_bytes::{
7    compact_nullable_string_len, compact_string_len, nullable_string_len,
8    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
9    string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12    get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13    get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
16use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
17use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
18use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
19
20pub const API_KEY: i16 = 36;
21pub const MIN_VERSION: i16 = 0;
22pub const MAX_VERSION: i16 = 2;
23pub const FLEXIBLE_MIN: i16 = 2;
24
25#[inline]
26fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
27
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct SaslAuthenticateResponse<'a> {
30    pub error_code: i16,
31    pub error_message: Option<&'a str>,
32    pub auth_bytes: &'a [u8],
33    pub session_lifetime_ms: i64,
34    pub unknown_tagged_fields: UnknownTaggedFields,
35}
36
37impl<'a> Default for SaslAuthenticateResponse<'a> {
38    fn default() -> Self {
39        Self {
40            error_code: 0i16,
41            error_message: None,
42            auth_bytes: &[],
43            session_lifetime_ms: 0i64,
44            unknown_tagged_fields: Default::default(),
45        }
46    }
47}
48
49impl<'a> SaslAuthenticateResponse<'a> {
50    pub fn to_owned(&self) -> crate::owned::sasl_authenticate_response::SaslAuthenticateResponse {
51        crate::owned::sasl_authenticate_response::SaslAuthenticateResponse {
52            error_code: (self.error_code),
53            error_message: (self.error_message).map(|s| s.to_string()),
54            auth_bytes: Bytes::copy_from_slice(self.auth_bytes),
55            session_lifetime_ms: (self.session_lifetime_ms),
56            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
57        }
58    }
59}
60
61impl<'a> Encode for SaslAuthenticateResponse<'a> {
62    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), 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        if version >= 0 { put_i16(buf, self.error_code) }
68        if version >= 0 { if flex { put_compact_nullable_string(buf, self.error_message) } else { put_nullable_string(buf, self.error_message) } }
69        if version >= 0 { if flex { put_compact_bytes(buf, self.auth_bytes) } else { put_bytes(buf, self.auth_bytes) } }
70        if version >= 1 { put_i64(buf, self.session_lifetime_ms) }
71        if flex {
72            let tagged = WriteTaggedFields::new();
73            tagged.write(buf, &self.unknown_tagged_fields);
74        }
75        Ok(())
76    }
77    fn encoded_len(&self, version: i16) -> usize {
78        let flex = is_flexible(version);
79        let mut n: usize = 0;
80        if version >= 0 { n += 2; }
81        if version >= 0 { n += if flex { compact_nullable_string_len(self.error_message) } else { nullable_string_len(self.error_message) }; }
82        if version >= 0 { n += if flex { crate::primitives::varint::uvarint_len(u32::try_from((self.auth_bytes).len() + 1).unwrap()) + (self.auth_bytes).len() } else { 4 + (self.auth_bytes).len() }; }
83        if version >= 1 { n += 8; }
84        if flex {
85            let known_pairs: Vec<(u32, usize)> = Vec::new();
86            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
87        }
88        n
89    }
90}
91
92impl<'de> DecodeBorrow<'de> for SaslAuthenticateResponse<'de> {
93    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
94        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
95            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
96        }
97        let flex = is_flexible(version);
98        let mut out = Self::default();
99        if version >= 0 { out.error_code = get_i16(buf)?; }
100        if version >= 0 { out.error_message = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
101        if version >= 0 { out.auth_bytes = if flex { get_compact_bytes_borrowed(buf)? } else { get_bytes_borrowed(buf)? }; }
102        if version >= 1 { out.session_lifetime_ms = get_i64(buf)?; }
103        if flex {
104            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
105                Ok(false)
106            })?;
107        }
108        Ok(out)
109    }
110}