Skip to main content

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

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use bytes::{BufMut, Bytes};
3
4use crate::{
5    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6    owned::sasl_authenticate_response,
7    primitives::{
8        fixed::{get_i16, get_i64, put_i16, put_i64},
9        string_bytes::{
10            bytes_len, compact_bytes_len, compact_nullable_string_len, nullable_string_len,
11            put_bytes, put_compact_bytes, put_compact_nullable_string, put_nullable_string,
12        },
13        string_bytes_borrowed::{
14            get_bytes_borrowed, get_compact_bytes_borrowed, get_compact_nullable_string_borrowed,
15            get_nullable_string_borrowed,
16        },
17    },
18    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
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#[inline]
25#[must_use]
26pub fn is_flexible(version: i16) -> bool {
27    version >= FLEXIBLE_MIN
28}
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct SaslAuthenticateResponse<'a> {
31    pub error_code: i16,
32    pub error_message: Option<&'a str>,
33    pub auth_bytes: &'a [u8],
34    pub session_lifetime_ms: i64,
35    pub unknown_tagged_fields: UnknownTaggedFields,
36}
37impl Default for SaslAuthenticateResponse<'_> {
38    fn default() -> Self {
39        Self {
40            error_code: 0i16,
41            error_message: Some(""),
42            auth_bytes: &[],
43            session_lifetime_ms: 0i64,
44            unknown_tagged_fields: UnknownTaggedFields::default(),
45        }
46    }
47}
48impl SaslAuthenticateResponse<'_> {
49    #[must_use]
50    pub fn to_owned(&self) -> sasl_authenticate_response::SaslAuthenticateResponse {
51        sasl_authenticate_response::SaslAuthenticateResponse {
52            error_code: (self.error_code),
53            error_message: (self.error_message).map(str::to_owned),
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}
60impl Encode for SaslAuthenticateResponse<'_> {
61    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), 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        if version >= 0 {
70            put_i16(buf, self.error_code);
71        }
72        if version >= 0 {
73            if flex {
74                put_compact_nullable_string(buf, self.error_message)?;
75            } else {
76                put_nullable_string(buf, self.error_message)?;
77            }
78        }
79        if version >= 0 {
80            if flex {
81                put_compact_bytes(buf, self.auth_bytes)?;
82            } else {
83                put_bytes(buf, self.auth_bytes)?;
84            }
85        }
86        if version >= 1 {
87            put_i64(buf, self.session_lifetime_ms);
88        }
89        if flex {
90            let tagged = WriteTaggedFields::new();
91            tagged.write(buf, &self.unknown_tagged_fields)?;
92        }
93        Ok(())
94    }
95    fn encoded_len(&self, version: i16) -> usize {
96        let flex = is_flexible(version);
97        let mut n: usize = 0;
98        if version >= 0 {
99            n += 2;
100        }
101        if version >= 0 {
102            n += if flex {
103                compact_nullable_string_len(self.error_message)
104            } else {
105                nullable_string_len(self.error_message)
106            };
107        }
108        if version >= 0 {
109            n += if flex {
110                compact_bytes_len(self.auth_bytes)
111            } else {
112                bytes_len(self.auth_bytes)
113            };
114        }
115        if version >= 1 {
116            n += 8;
117        }
118        if flex {
119            let known_pairs: Vec<(u32, usize)> = Vec::new();
120            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
121        }
122        n
123    }
124}
125impl<'de> DecodeBorrow<'de> for SaslAuthenticateResponse<'de> {
126    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
127        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
128            return Err(ProtocolError::UnsupportedVersion {
129                api_key: API_KEY,
130                version,
131            });
132        }
133        let flex = is_flexible(version);
134        let mut out = Self::default();
135        if version >= 0 {
136            out.error_code = get_i16(buf)?;
137        }
138        if version >= 0 {
139            out.error_message = if flex {
140                get_compact_nullable_string_borrowed(buf)?
141            } else {
142                get_nullable_string_borrowed(buf)?
143            };
144        }
145        if version >= 0 {
146            out.auth_bytes = if flex {
147                get_compact_bytes_borrowed(buf)?
148            } else {
149                get_bytes_borrowed(buf)?
150            };
151        }
152        if version >= 1 {
153            out.session_lifetime_ms = get_i64(buf)?;
154        }
155        if flex {
156            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
157        }
158        Ok(out)
159    }
160}
161#[cfg(test)]
162impl SaslAuthenticateResponse<'_> {
163    #[must_use]
164    pub fn populated(version: i16) -> Self {
165        let mut m = Self::default();
166        if version >= 0 {
167            m.error_code = 1i16;
168        }
169        if version >= 0 {
170            m.error_message = Some("x");
171        }
172        if version >= 0 {
173            m.auth_bytes = &b"x"[..];
174        }
175        if version >= 1 {
176            m.session_lifetime_ms = 1i64;
177        }
178        m
179    }
180}