Skip to main content

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

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