1use bytes::{Buf, BufMut};
4
5use crate::{
6 Decode, Encode, ProtocolError, UnknownTaggedFields,
7 primitives::{
8 fixed::{get_i8, put_i8},
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 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
15};
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 0;
18pub const FLEXIBLE_MIN: i16 = 0;
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 AccessControlEntryRecord {
26 pub id: crate::primitives::uuid::Uuid,
27 pub resource_type: i8,
28 pub resource_name: String,
29 pub pattern_type: i8,
30 pub principal: String,
31 pub host: String,
32 pub operation: i8,
33 pub permission_type: i8,
34 pub unknown_tagged_fields: UnknownTaggedFields,
35}
36impl AccessControlEntryRecord {
37 fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
38 if version >= 0 {
39 crate::primitives::uuid::put_uuid(buf, self.id);
40 }
41 }
42 fn encode_field_1<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
43 if version >= 0 {
44 put_i8(buf, self.resource_type);
45 }
46 }
47 fn encode_field_2<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
48 if version >= 0 {
49 if flex {
50 let () = put_compact_string(buf, &self.resource_name);
51 } else {
52 let () = put_string(buf, &self.resource_name);
53 }
54 }
55 }
56 fn encode_field_3<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
57 if version >= 0 {
58 put_i8(buf, self.pattern_type);
59 }
60 }
61 fn encode_field_4<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
62 if version >= 0 {
63 if flex {
64 let () = put_compact_string(buf, &self.principal);
65 } else {
66 let () = put_string(buf, &self.principal);
67 }
68 }
69 }
70 fn encode_field_5<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
71 if version >= 0 {
72 if flex {
73 let () = put_compact_string(buf, &self.host);
74 } else {
75 let () = put_string(buf, &self.host);
76 }
77 }
78 }
79 fn encode_field_6<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
80 if version >= 0 {
81 put_i8(buf, self.operation);
82 }
83 }
84 fn encode_field_7<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
85 if version >= 0 {
86 put_i8(buf, self.permission_type);
87 }
88 }
89 fn encode_tagged_fields<B: BufMut>(&self, buf: &mut B, _version: i16, flex: bool) {
90 if flex {
91 let tagged = WriteTaggedFields::new();
92 tagged.write(buf, &self.unknown_tagged_fields);
93 }
94 }
95 fn decode_field_0<B: Buf>(
96 out: &mut Self,
97 buf: &mut B,
98 version: i16,
99 _flex: bool,
100 ) -> Result<(), ProtocolError> {
101 if version >= 0 {
102 out.id = crate::primitives::uuid::get_uuid(buf)?;
103 }
104 Ok(())
105 }
106 fn decode_field_1<B: Buf>(
107 out: &mut Self,
108 buf: &mut B,
109 version: i16,
110 _flex: bool,
111 ) -> Result<(), ProtocolError> {
112 if version >= 0 {
113 out.resource_type = get_i8(buf)?;
114 }
115 Ok(())
116 }
117 fn decode_field_2<B: Buf>(
118 out: &mut Self,
119 buf: &mut B,
120 version: i16,
121 flex: bool,
122 ) -> Result<(), ProtocolError> {
123 if version >= 0 {
124 out.resource_name = if flex {
125 get_compact_string_owned(buf)?
126 } else {
127 get_string_owned(buf)?
128 };
129 }
130 Ok(())
131 }
132 fn decode_field_3<B: Buf>(
133 out: &mut Self,
134 buf: &mut B,
135 version: i16,
136 _flex: bool,
137 ) -> Result<(), ProtocolError> {
138 if version >= 0 {
139 out.pattern_type = get_i8(buf)?;
140 }
141 Ok(())
142 }
143 fn decode_field_4<B: Buf>(
144 out: &mut Self,
145 buf: &mut B,
146 version: i16,
147 flex: bool,
148 ) -> Result<(), ProtocolError> {
149 if version >= 0 {
150 out.principal = if flex {
151 get_compact_string_owned(buf)?
152 } else {
153 get_string_owned(buf)?
154 };
155 }
156 Ok(())
157 }
158 fn decode_field_5<B: Buf>(
159 out: &mut Self,
160 buf: &mut B,
161 version: i16,
162 flex: bool,
163 ) -> Result<(), ProtocolError> {
164 if version >= 0 {
165 out.host = if flex {
166 get_compact_string_owned(buf)?
167 } else {
168 get_string_owned(buf)?
169 };
170 }
171 Ok(())
172 }
173 fn decode_field_6<B: Buf>(
174 out: &mut Self,
175 buf: &mut B,
176 version: i16,
177 _flex: bool,
178 ) -> Result<(), ProtocolError> {
179 if version >= 0 {
180 out.operation = get_i8(buf)?;
181 }
182 Ok(())
183 }
184 fn decode_field_7<B: Buf>(
185 out: &mut Self,
186 buf: &mut B,
187 version: i16,
188 _flex: bool,
189 ) -> Result<(), ProtocolError> {
190 if version >= 0 {
191 out.permission_type = get_i8(buf)?;
192 }
193 Ok(())
194 }
195 fn decode_tagged_fields<B: Buf>(
196 out: &mut Self,
197 buf: &mut B,
198 _version: i16,
199 flex: bool,
200 ) -> Result<(), ProtocolError> {
201 if flex {
202 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
203 }
204 Ok(())
205 }
206}
207impl Encode for AccessControlEntryRecord {
208 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
209 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
210 return Err(ProtocolError::SchemaMismatch(
211 "AccessControlEntryRecord version out of range",
212 ));
213 }
214 let flex = is_flexible(version);
215 self.encode_field_0(buf, version, flex);
216 self.encode_field_1(buf, version, flex);
217 self.encode_field_2(buf, version, flex);
218 self.encode_field_3(buf, version, flex);
219 self.encode_field_4(buf, version, flex);
220 self.encode_field_5(buf, version, flex);
221 self.encode_field_6(buf, version, flex);
222 self.encode_field_7(buf, version, flex);
223 self.encode_tagged_fields(buf, version, flex);
224 Ok(())
225 }
226 fn encoded_len(&self, version: i16) -> usize {
227 let flex = is_flexible(version);
228 let mut n: usize = 0;
229 if version >= 0 {
230 n += 16;
231 }
232 if version >= 0 {
233 n += 1;
234 }
235 if version >= 0 {
236 n += if flex {
237 compact_string_len(&self.resource_name)
238 } else {
239 string_len(&self.resource_name)
240 };
241 }
242 if version >= 0 {
243 n += 1;
244 }
245 if version >= 0 {
246 n += if flex {
247 compact_string_len(&self.principal)
248 } else {
249 string_len(&self.principal)
250 };
251 }
252 if version >= 0 {
253 n += if flex {
254 compact_string_len(&self.host)
255 } else {
256 string_len(&self.host)
257 };
258 }
259 if version >= 0 {
260 n += 1;
261 }
262 if version >= 0 {
263 n += 1;
264 }
265 if flex {
266 let known_pairs: Vec<(u32, usize)> = Vec::new();
267 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
268 }
269 n
270 }
271}
272impl Decode<'_> for AccessControlEntryRecord {
273 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
274 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
275 return Err(ProtocolError::SchemaMismatch(
276 "AccessControlEntryRecord version out of range",
277 ));
278 }
279 let flex = is_flexible(version);
280 let mut out = Self::default();
281 Self::decode_field_0(&mut out, buf, version, flex)?;
282 Self::decode_field_1(&mut out, buf, version, flex)?;
283 Self::decode_field_2(&mut out, buf, version, flex)?;
284 Self::decode_field_3(&mut out, buf, version, flex)?;
285 Self::decode_field_4(&mut out, buf, version, flex)?;
286 Self::decode_field_5(&mut out, buf, version, flex)?;
287 Self::decode_field_6(&mut out, buf, version, flex)?;
288 Self::decode_field_7(&mut out, buf, version, flex)?;
289 Self::decode_tagged_fields(&mut out, buf, version, flex)?;
290 Ok(out)
291 }
292}
293#[cfg(test)]
294impl AccessControlEntryRecord {
295 #[must_use]
296 pub fn populated(version: i16) -> Self {
297 let mut m = Self::default();
298 if version >= 0 {
299 m.id = crate::primitives::uuid::Uuid([1u8; 16]);
300 }
301 if version >= 0 {
302 m.resource_type = 1i8;
303 }
304 if version >= 0 {
305 m.resource_name = "x".to_string();
306 }
307 if version >= 0 {
308 m.pattern_type = 1i8;
309 }
310 if version >= 0 {
311 m.principal = "x".to_string();
312 }
313 if version >= 0 {
314 m.host = "x".to_string();
315 }
316 if version >= 0 {
317 m.operation = 1i8;
318 }
319 if version >= 0 {
320 m.permission_type = 1i8;
321 }
322 m
323 }
324}
325#[must_use]
328#[allow(unused_comparisons)]
329pub fn default_json(_version: i16) -> ::serde_json::Value {
330 let mut obj = ::serde_json::Map::new();
331 obj.insert(
332 "id".to_string(),
333 ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
334 );
335 obj.insert("resourceType".to_string(), ::serde_json::json!(0));
336 obj.insert(
337 "resourceName".to_string(),
338 ::serde_json::Value::String(String::new()),
339 );
340 obj.insert("patternType".to_string(), ::serde_json::json!(0));
341 obj.insert(
342 "principal".to_string(),
343 ::serde_json::Value::String(String::new()),
344 );
345 obj.insert(
346 "host".to_string(),
347 ::serde_json::Value::String(String::new()),
348 );
349 obj.insert("operation".to_string(), ::serde_json::json!(0));
350 obj.insert("permissionType".to_string(), ::serde_json::json!(0));
351 ::serde_json::Value::Object(obj)
352}