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