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