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