crabka_protocol/opt/rustwide/workdir/generated/
SaslAuthenticateRequest.borrowed.rs1use crate::primitives::string_bytes::{put_bytes, put_compact_bytes};
4use crate::primitives::string_bytes_borrowed::{get_bytes_borrowed, get_compact_bytes_borrowed};
5use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
6use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
7use bytes::{BufMut, Bytes};
8
9pub const API_KEY: i16 = 36;
10pub const MIN_VERSION: i16 = 0;
11pub const MAX_VERSION: i16 = 2;
12pub const FLEXIBLE_MIN: i16 = 2;
13
14#[inline]
15fn is_flexible(version: i16) -> bool {
16 version >= FLEXIBLE_MIN
17}
18
19#[derive(Debug, Clone, PartialEq, Eq, Default)]
20pub struct SaslAuthenticateRequest<'a> {
21 pub auth_bytes: &'a [u8],
22 pub unknown_tagged_fields: UnknownTaggedFields,
23}
24impl SaslAuthenticateRequest<'_> {
25 pub fn to_owned(&self) -> crate::owned::sasl_authenticate_request::SaslAuthenticateRequest {
26 crate::owned::sasl_authenticate_request::SaslAuthenticateRequest {
27 auth_bytes: Bytes::copy_from_slice(self.auth_bytes),
28 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
29 }
30 }
31}
32impl Encode for SaslAuthenticateRequest<'_> {
33 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
34 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
35 return Err(ProtocolError::UnsupportedVersion {
36 api_key: API_KEY,
37 version,
38 });
39 }
40 let flex = is_flexible(version);
41 if version >= 0 {
42 if flex {
43 put_compact_bytes(buf, self.auth_bytes);
44 } else {
45 put_bytes(buf, self.auth_bytes);
46 }
47 }
48 if flex {
49 let tagged = WriteTaggedFields::new();
50 tagged.write(buf, &self.unknown_tagged_fields);
51 }
52 Ok(())
53 }
54 fn encoded_len(&self, version: i16) -> usize {
55 let flex = is_flexible(version);
56 let mut n: usize = 0;
57 if version >= 0 {
58 n += if flex {
59 crate::primitives::varint::uvarint_len(
60 u32::try_from((self.auth_bytes).len() + 1).unwrap(),
61 ) + (self.auth_bytes).len()
62 } else {
63 4 + (self.auth_bytes).len()
64 };
65 }
66 if flex {
67 let known_pairs: Vec<(u32, usize)> = Vec::new();
68 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
69 }
70 n
71 }
72}
73impl<'de> DecodeBorrow<'de> for SaslAuthenticateRequest<'de> {
74 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
75 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
76 return Err(ProtocolError::UnsupportedVersion {
77 api_key: API_KEY,
78 version,
79 });
80 }
81 let flex = is_flexible(version);
82 let mut out = Self::default();
83 if version >= 0 {
84 out.auth_bytes = if flex {
85 get_compact_bytes_borrowed(buf)?
86 } else {
87 get_bytes_borrowed(buf)?
88 };
89 }
90 if flex {
91 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
92 }
93 Ok(out)
94 }
95}
96#[cfg(test)]
97impl SaslAuthenticateRequest<'_> {
98 #[must_use]
99 pub fn populated(version: i16) -> Self {
100 let mut m = Self::default();
101 if version >= 0 {
102 m.auth_bytes = &b"x"[..];
103 }
104 m
105 }
106}