crabka_protocol/opt/rustwide/workdir/generated/
SaslHandshakeResponse.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i16, put_i16};
6use crate::primitives::string_bytes::{
7 compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
8 string_len,
9};
10use crate::{Decode, 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 {
24 pub error_code: i16,
25 pub mechanisms: Vec<String>,
26 pub unknown_tagged_fields: UnknownTaggedFields,
27}
28impl Encode for SaslHandshakeResponse {
29 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
30 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
31 return Err(ProtocolError::UnsupportedVersion {
32 api_key: API_KEY,
33 version,
34 });
35 }
36 let flex = is_flexible(version);
37 if version >= 0 {
38 put_i16(buf, self.error_code);
39 }
40 if version >= 0 {
41 {
42 crate::primitives::array::put_array_len(buf, (self.mechanisms).len(), flex);
43 for it in &self.mechanisms {
44 if flex {
45 put_compact_string(buf, it);
46 } else {
47 put_string(buf, it);
48 }
49 }
50 }
51 }
52 Ok(())
53 }
54 fn encoded_len(&self, version: i16) -> usize {
55 let flex = is_flexible(version);
56 let mut n: usize = 0;
57 if version >= 0 {
58 n += 2;
59 }
60 if version >= 0 {
61 n += {
62 let prefix =
63 crate::primitives::array::array_len_prefix_len((self.mechanisms).len(), flex);
64 let body: usize = (self.mechanisms)
65 .iter()
66 .map(|it| {
67 if flex {
68 compact_string_len(it)
69 } else {
70 string_len(it)
71 }
72 })
73 .sum();
74 prefix + body
75 };
76 }
77 n
78 }
79}
80impl Decode<'_> for SaslHandshakeResponse {
81 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
82 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
83 return Err(ProtocolError::UnsupportedVersion {
84 api_key: API_KEY,
85 version,
86 });
87 }
88 let flex = is_flexible(version);
89 let mut out = Self::default();
90 if version >= 0 {
91 out.error_code = get_i16(buf)?;
92 }
93 if version >= 0 {
94 out.mechanisms = {
95 let n = crate::primitives::array::get_array_len(buf, flex)?;
96 let mut v = Vec::with_capacity(n);
97 for _ in 0..n {
98 v.push(if flex {
99 get_compact_string_owned(buf)?
100 } else {
101 get_string_owned(buf)?
102 });
103 }
104 v
105 };
106 }
107 Ok(out)
108 }
109}
110#[cfg(test)]
111impl SaslHandshakeResponse {
112 #[must_use]
113 pub fn populated(version: i16) -> Self {
114 let mut m = Self::default();
115 if version >= 0 {
116 m.error_code = 1i16;
117 }
118 if version >= 0 {
119 m.mechanisms = vec!["x".to_string()];
120 }
121 m
122 }
123}
124
125#[must_use]
128#[allow(unused_comparisons)]
129pub fn default_json(version: i16) -> ::serde_json::Value {
130 let mut obj = ::serde_json::Map::new();
131 obj.insert("errorCode".to_string(), ::serde_json::json!(0));
132 obj.insert("mechanisms".to_string(), ::serde_json::Value::Array(vec![]));
133 ::serde_json::Value::Object(obj)
134}