Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
SaslHandshakeResponse.borrowed.rs

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use bytes::BufMut;
3
4use crate::{
5    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6    primitives::{
7        fixed::{get_i16, put_i16},
8        string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
9        string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
10    },
11};
12pub const API_KEY: i16 = 17;
13pub const MIN_VERSION: i16 = 0;
14pub const MAX_VERSION: i16 = 1;
15pub const FLEXIBLE_MIN: i16 = 32767;
16#[inline]
17#[must_use]
18pub fn is_flexible(version: i16) -> bool {
19    version == FLEXIBLE_MIN
20}
21#[derive(Debug, Clone, PartialEq, Eq, Default)]
22pub struct SaslHandshakeResponse<'a> {
23    pub error_code: i16,
24    pub mechanisms: Vec<&'a str>,
25    pub unknown_tagged_fields: UnknownTaggedFields,
26}
27impl SaslHandshakeResponse<'_> {
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_handshake_response::SaslHandshakeResponse {
33        crate::owned::sasl_handshake_response::SaslHandshakeResponse {
34            error_code: (self.error_code),
35            mechanisms: (self.mechanisms)
36                .iter()
37                .map(std::string::ToString::to_string)
38                .collect(),
39            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
40        }
41    }
42}
43impl Encode for SaslHandshakeResponse<'_> {
44    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
45        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
46            return Err(ProtocolError::UnsupportedVersion {
47                api_key: API_KEY,
48                version,
49            });
50        }
51        let flex = is_flexible(version);
52        if version >= 0 {
53            put_i16(buf, self.error_code);
54        }
55        if version >= 0 {
56            {
57                crate::primitives::array::put_array_len(buf, (self.mechanisms).len(), flex);
58                for it in &self.mechanisms {
59                    if flex {
60                        let () = put_compact_string(buf, it);
61                    } else {
62                        let () = put_string(buf, it);
63                    }
64                }
65            };
66        }
67        Ok(())
68    }
69    fn encoded_len(&self, version: i16) -> usize {
70        let flex = is_flexible(version);
71        let mut n: usize = 0;
72        if version >= 0 {
73            n += 2;
74        }
75        if version >= 0 {
76            n += {
77                let prefix =
78                    crate::primitives::array::array_len_prefix_len((self.mechanisms).len(), flex);
79                let body: usize = (self.mechanisms)
80                    .iter()
81                    .map(|it| {
82                        if flex {
83                            compact_string_len(it)
84                        } else {
85                            string_len(it)
86                        }
87                    })
88                    .sum();
89                prefix + body
90            };
91        }
92        n
93    }
94}
95impl<'de> DecodeBorrow<'de> for SaslHandshakeResponse<'de> {
96    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
97        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
98            return Err(ProtocolError::UnsupportedVersion {
99                api_key: API_KEY,
100                version,
101            });
102        }
103        let flex = is_flexible(version);
104        let mut out = Self::default();
105        if version >= 0 {
106            out.error_code = get_i16(buf)?;
107        }
108        if version >= 0 {
109            out.mechanisms = {
110                let n = crate::primitives::array::get_array_len(buf, flex)?;
111                let mut v = Vec::with_capacity(n);
112                for _ in 0..n {
113                    v.push(if flex {
114                        get_compact_string_borrowed(buf)?
115                    } else {
116                        get_string_borrowed(buf)?
117                    });
118                }
119                v
120            };
121        }
122        Ok(out)
123    }
124}
125#[cfg(test)]
126impl SaslHandshakeResponse<'_> {
127    #[must_use]
128    pub fn populated(version: i16) -> Self {
129        let mut m = Self::default();
130        if version >= 0 {
131            m.error_code = 1i16;
132        }
133        if version >= 0 {
134            m.mechanisms = vec!["x"];
135        }
136        m
137    }
138}