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.
2use crate::primitives::fixed::{get_i16, get_i64, put_i16, put_i64};
3use crate::primitives::string_bytes::{
4    compact_nullable_string_len, nullable_string_len, put_compact_nullable_string,
5    put_nullable_string,
6};
7use crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
8use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
9use crate::primitives::string_bytes_borrowed::{
10    get_compact_nullable_string_borrowed, get_nullable_string_borrowed,
11};
12use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
13use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
14use bytes::{BufMut, Bytes};
15pub const API_KEY: i16 = 36;
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 2;
18pub const FLEXIBLE_MIN: i16 = 2;
19#[inline]
20fn is_flexible(version: i16) -> bool {
21    version >= FLEXIBLE_MIN
22}
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct SaslAuthenticateResponse<'a> {
25    pub error_code: i16,
26    pub error_message: Option<&'a str>,
27    pub auth_bytes: &'a [u8],
28    pub session_lifetime_ms: i64,
29    pub unknown_tagged_fields: UnknownTaggedFields,
30}
31impl SaslAuthenticateResponse<'_> {
32    pub fn to_owned(&self) -> crate::owned::sasl_authenticate_response::SaslAuthenticateResponse {
33        crate::owned::sasl_authenticate_response::SaslAuthenticateResponse {
34            error_code: (self.error_code),
35            error_message: (self.error_message).map(std::string::ToString::to_string),
36            auth_bytes: Bytes::copy_from_slice(self.auth_bytes),
37            session_lifetime_ms: (self.session_lifetime_ms),
38            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
39        }
40    }
41}
42impl Encode for SaslAuthenticateResponse<'_> {
43    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
44        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
45            return Err(ProtocolError::UnsupportedVersion {
46                api_key: API_KEY,
47                version,
48            });
49        }
50        let flex = is_flexible(version);
51        if version >= 0 {
52            put_i16(buf, self.error_code);
53        }
54        if version >= 0 {
55            if flex {
56                put_compact_nullable_string(buf, self.error_message);
57            } else {
58                put_nullable_string(buf, self.error_message);
59            }
60        }
61        if version >= 0 {
62            if flex {
63                put_compact_bytes(buf, self.auth_bytes);
64            } else {
65                put_bytes(buf, self.auth_bytes);
66            }
67        }
68        if version >= 1 {
69            put_i64(buf, self.session_lifetime_ms);
70        }
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 {
81            n += 2;
82        }
83        if version >= 0 {
84            n += if flex {
85                compact_nullable_string_len(self.error_message)
86            } else {
87                nullable_string_len(self.error_message)
88            };
89        }
90        if version >= 0 {
91            n += if flex {
92                crate::primitives::varint::uvarint_len(
93                    u32::try_from((self.auth_bytes).len() + 1).unwrap(),
94                ) + (self.auth_bytes).len()
95            } else {
96                4 + (self.auth_bytes).len()
97            };
98        }
99        if version >= 1 {
100            n += 8;
101        }
102        if flex {
103            let known_pairs: Vec<(u32, usize)> = Vec::new();
104            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
105        }
106        n
107    }
108}
109impl<'de> DecodeBorrow<'de> for SaslAuthenticateResponse<'de> {
110    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
111        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
112            return Err(ProtocolError::UnsupportedVersion {
113                api_key: API_KEY,
114                version,
115            });
116        }
117        let flex = is_flexible(version);
118        let mut out = Self::default();
119        if version >= 0 {
120            out.error_code = get_i16(buf)?;
121        }
122        if version >= 0 {
123            out.error_message = if flex {
124                get_compact_nullable_string_borrowed(buf)?
125            } else {
126                get_nullable_string_borrowed(buf)?
127            };
128        }
129        if version >= 0 {
130            out.auth_bytes = if flex {
131                get_compact_bytes_borrowed(buf)?
132            } else {
133                get_bytes_borrowed(buf)?
134            };
135        }
136        if version >= 1 {
137            out.session_lifetime_ms = get_i64(buf)?;
138        }
139        if flex {
140            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
141        }
142        Ok(out)
143    }
144}
145#[cfg(test)]
146impl SaslAuthenticateResponse<'_> {
147    #[must_use]
148    pub fn populated(version: i16) -> Self {
149        let mut m = Self::default();
150        if version >= 0 {
151            m.error_code = 1i16;
152        }
153        if version >= 0 {
154            m.error_message = Some("x");
155        }
156        if version >= 0 {
157            m.auth_bytes = &b"x"[..];
158        }
159        if version >= 1 {
160            m.session_lifetime_ms = 1i64;
161        }
162        m
163    }
164}