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