Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use crate::primitives::fixed::{get_i16, put_i16};
4use crate::primitives::string_bytes::{
5    compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
6    string_len,
7};
8use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
9use bytes::{Buf, BufMut};
10pub const API_KEY: i16 = 17;
11pub const MIN_VERSION: i16 = 0;
12pub const MAX_VERSION: i16 = 1;
13pub const FLEXIBLE_MIN: i16 = 32767;
14#[inline]
15fn is_flexible(version: i16) -> bool {
16    version >= FLEXIBLE_MIN
17}
18#[derive(Debug, Clone, PartialEq, Eq, Default)]
19pub struct SaslHandshakeResponse {
20    pub error_code: i16,
21    pub mechanisms: Vec<String>,
22    pub unknown_tagged_fields: UnknownTaggedFields,
23}
24impl Encode for SaslHandshakeResponse {
25    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
26        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
27            return Err(ProtocolError::UnsupportedVersion {
28                api_key: API_KEY,
29                version,
30            });
31        }
32        let flex = is_flexible(version);
33        if version >= 0 {
34            put_i16(buf, self.error_code);
35        }
36        if version >= 0 {
37            {
38                crate::primitives::array::put_array_len(buf, (self.mechanisms).len(), flex);
39                for it in &self.mechanisms {
40                    if flex {
41                        put_compact_string(buf, it);
42                    } else {
43                        put_string(buf, it);
44                    }
45                }
46            }
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 += 2;
55        }
56        if version >= 0 {
57            n += {
58                let prefix =
59                    crate::primitives::array::array_len_prefix_len((self.mechanisms).len(), flex);
60                let body: usize = (self.mechanisms)
61                    .iter()
62                    .map(|it| {
63                        if flex {
64                            compact_string_len(it)
65                        } else {
66                            string_len(it)
67                        }
68                    })
69                    .sum();
70                prefix + body
71            };
72        }
73        n
74    }
75}
76impl Decode<'_> for SaslHandshakeResponse {
77    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
78        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
79            return Err(ProtocolError::UnsupportedVersion {
80                api_key: API_KEY,
81                version,
82            });
83        }
84        let flex = is_flexible(version);
85        let mut out = Self::default();
86        if version >= 0 {
87            out.error_code = get_i16(buf)?;
88        }
89        if version >= 0 {
90            out.mechanisms = {
91                let n = crate::primitives::array::get_array_len(buf, flex)?;
92                let mut v = Vec::with_capacity(n);
93                for _ in 0..n {
94                    v.push(if flex {
95                        get_compact_string_owned(buf)?
96                    } else {
97                        get_string_owned(buf)?
98                    });
99                }
100                v
101            };
102        }
103        Ok(out)
104    }
105}
106#[cfg(test)]
107impl SaslHandshakeResponse {
108    #[must_use]
109    pub fn populated(version: i16) -> Self {
110        let mut m = Self::default();
111        if version >= 0 {
112            m.error_code = 1i16;
113        }
114        if version >= 0 {
115            m.mechanisms = vec!["x".to_string()];
116        }
117        m
118    }
119}
120/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
121/// Only includes fields valid for the given version.
122#[must_use]
123#[allow(unused_comparisons)]
124pub fn default_json(version: i16) -> ::serde_json::Value {
125    let mut obj = ::serde_json::Map::new();
126    obj.insert("errorCode".to_string(), ::serde_json::json!(0));
127    obj.insert("mechanisms".to_string(), ::serde_json::Value::Array(vec![]));
128    ::serde_json::Value::Object(obj)
129}