Skip to main content

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

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