Skip to main content

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