krabka_protocol/opt/rustwide/workdir/generated/
SaslHandshakeResponse.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::{
6 Decode, Encode, ProtocolError, UnknownTaggedFields,
7 primitives::{
8 fixed::{get_i16, put_i16},
9 string_bytes::{
10 compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string,
11 put_string, string_len,
12 },
13 },
14};
15pub const API_KEY: i16 = 17;
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 1;
18pub const FLEXIBLE_MIN: i16 = 32767;
19#[inline]
20#[must_use]
21pub fn is_flexible(version: i16) -> bool {
22 version == FLEXIBLE_MIN
23}
24#[derive(Debug, Clone, PartialEq, Eq, Default)]
25pub struct SaslHandshakeResponse {
26 pub error_code: i16,
27 pub mechanisms: Vec<String>,
28 pub unknown_tagged_fields: UnknownTaggedFields,
29}
30impl Encode for SaslHandshakeResponse {
31 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
32 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
33 return Err(ProtocolError::UnsupportedVersion {
34 api_key: API_KEY,
35 version,
36 });
37 }
38 let flex = is_flexible(version);
39 if version >= 0 {
40 put_i16(buf, self.error_code);
41 }
42 if version >= 0 {
43 {
44 crate::primitives::array::put_array_len(buf, (self.mechanisms).len(), flex);
45 for it in &self.mechanisms {
46 if flex {
47 let () = put_compact_string(buf, it);
48 } else {
49 let () = put_string(buf, it);
50 }
51 }
52 };
53 }
54 Ok(())
55 }
56 fn encoded_len(&self, version: i16) -> usize {
57 let flex = is_flexible(version);
58 let mut n: usize = 0;
59 if version >= 0 {
60 n += 2;
61 }
62 if version >= 0 {
63 n += {
64 let prefix =
65 crate::primitives::array::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 = crate::primitives::array::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_string()];
122 }
123 m
124 }
125}
126#[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_string(), ::serde_json::json!(0));
133 obj.insert("mechanisms".to_string(), ::serde_json::Value::Array(vec![]));
134 ::serde_json::Value::Object(obj)
135}