Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
SaslAuthenticateRequest.borrowed.rs

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