crabka_protocol/opt/rustwide/workdir/generated/
ClientQuotaRecord.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_bool, get_f64, put_bool, put_f64};
6use crate::primitives::string_bytes::{
7 compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
8 get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
9 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
10};
11use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
12use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
13pub const MIN_VERSION: i16 = 0;
14pub const MAX_VERSION: i16 = 0;
15pub const FLEXIBLE_MIN: i16 = 0;
16
17#[inline]
18fn is_flexible(version: i16) -> bool {
19 version >= FLEXIBLE_MIN
20}
21
22#[derive(Debug, Clone, PartialEq, Default)]
23pub struct ClientQuotaRecord {
24 pub entity: Vec<EntityData>,
25 pub key: String,
26 pub value: f64,
27 pub remove: bool,
28 pub unknown_tagged_fields: UnknownTaggedFields,
29}
30impl Encode for ClientQuotaRecord {
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::SchemaMismatch(
34 "ClientQuotaRecord version out of range",
35 ));
36 }
37 let flex = is_flexible(version);
38 if version >= 0 {
39 {
40 crate::primitives::array::put_array_len(buf, (self.entity).len(), flex);
41 for it in &self.entity {
42 it.encode(buf, version)?;
43 }
44 }
45 }
46 if version >= 0 {
47 if flex {
48 put_compact_string(buf, &self.key);
49 } else {
50 put_string(buf, &self.key);
51 }
52 }
53 if version >= 0 {
54 put_f64(buf, self.value);
55 }
56 if version >= 0 {
57 put_bool(buf, self.remove);
58 }
59 if flex {
60 let tagged = WriteTaggedFields::new();
61 tagged.write(buf, &self.unknown_tagged_fields);
62 }
63 Ok(())
64 }
65 fn encoded_len(&self, version: i16) -> usize {
66 let flex = is_flexible(version);
67 let mut n: usize = 0;
68 if version >= 0 {
69 n += {
70 let prefix =
71 crate::primitives::array::array_len_prefix_len((self.entity).len(), flex);
72 let body: usize = (self.entity).iter().map(|it| it.encoded_len(version)).sum();
73 prefix + body
74 };
75 }
76 if version >= 0 {
77 n += if flex {
78 compact_string_len(&self.key)
79 } else {
80 string_len(&self.key)
81 };
82 }
83 if version >= 0 {
84 n += 8;
85 }
86 if version >= 0 {
87 n += 1;
88 }
89 if flex {
90 let known_pairs: Vec<(u32, usize)> = Vec::new();
91 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
92 }
93 n
94 }
95}
96impl Decode<'_> for ClientQuotaRecord {
97 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
98 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
99 return Err(ProtocolError::SchemaMismatch(
100 "ClientQuotaRecord version out of range",
101 ));
102 }
103 let flex = is_flexible(version);
104 let mut out = Self::default();
105 if version >= 0 {
106 out.entity = {
107 let n = crate::primitives::array::get_array_len(buf, flex)?;
108 let mut v = Vec::with_capacity(n);
109 for _ in 0..n {
110 v.push(EntityData::decode(buf, version)?);
111 }
112 v
113 };
114 }
115 if version >= 0 {
116 out.key = if flex {
117 get_compact_string_owned(buf)?
118 } else {
119 get_string_owned(buf)?
120 };
121 }
122 if version >= 0 {
123 out.value = get_f64(buf)?;
124 }
125 if version >= 0 {
126 out.remove = get_bool(buf)?;
127 }
128 if flex {
129 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
130 }
131 Ok(out)
132 }
133}
134#[cfg(test)]
135impl ClientQuotaRecord {
136 #[must_use]
137 pub fn populated(version: i16) -> Self {
138 let mut m = Self::default();
139 if version >= 0 {
140 m.entity = vec![EntityData::populated(version)];
141 }
142 if version >= 0 {
143 m.key = "x".to_string();
144 }
145 if version >= 0 {
146 m.value = 1.0f64;
147 }
148 if version >= 0 {
149 m.remove = true;
150 }
151 m
152 }
153}
154#[derive(Debug, Clone, PartialEq, Eq, Default)]
155pub struct EntityData {
156 pub entity_type: String,
157 pub entity_name: Option<String>,
158 pub unknown_tagged_fields: UnknownTaggedFields,
159}
160impl Encode for EntityData {
161 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
162 let flex = version >= 0;
163 if version >= 0 {
164 if flex {
165 put_compact_string(buf, &self.entity_type);
166 } else {
167 put_string(buf, &self.entity_type);
168 }
169 }
170 if version >= 0 {
171 if flex {
172 put_compact_nullable_string(buf, self.entity_name.as_deref());
173 } else {
174 put_nullable_string(buf, self.entity_name.as_deref());
175 }
176 }
177 if flex {
178 let tagged = WriteTaggedFields::new();
179 tagged.write(buf, &self.unknown_tagged_fields);
180 }
181 Ok(())
182 }
183 fn encoded_len(&self, version: i16) -> usize {
184 let flex = version >= 0;
185 let mut n: usize = 0;
186 if version >= 0 {
187 n += if flex {
188 compact_string_len(&self.entity_type)
189 } else {
190 string_len(&self.entity_type)
191 };
192 }
193 if version >= 0 {
194 n += if flex {
195 compact_nullable_string_len(self.entity_name.as_deref())
196 } else {
197 nullable_string_len(self.entity_name.as_deref())
198 };
199 }
200 if flex {
201 let known_pairs: Vec<(u32, usize)> = Vec::new();
202 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
203 }
204 n
205 }
206}
207impl Decode<'_> for EntityData {
208 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
209 let flex = version >= 0;
210 let mut out = Self::default();
211 if version >= 0 {
212 out.entity_type = if flex {
213 get_compact_string_owned(buf)?
214 } else {
215 get_string_owned(buf)?
216 };
217 }
218 if version >= 0 {
219 out.entity_name = if flex {
220 get_compact_nullable_string_owned(buf)?
221 } else {
222 get_nullable_string_owned(buf)?
223 };
224 }
225 if flex {
226 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
227 }
228 Ok(out)
229 }
230}
231#[cfg(test)]
232impl EntityData {
233 #[must_use]
234 pub fn populated(version: i16) -> Self {
235 let mut m = Self::default();
236 if version >= 0 {
237 m.entity_type = "x".to_string();
238 }
239 if version >= 0 {
240 m.entity_name = Some("x".to_string());
241 }
242 m
243 }
244}
245
246#[must_use]
249#[allow(unused_comparisons)]
250pub fn default_json(version: i16) -> ::serde_json::Value {
251 let mut obj = ::serde_json::Map::new();
252 obj.insert("entity".to_string(), ::serde_json::Value::Array(vec![]));
253 obj.insert(
254 "key".to_string(),
255 ::serde_json::Value::String(String::new()),
256 );
257 obj.insert("value".to_string(), ::serde_json::json!(0.0));
258 obj.insert("remove".to_string(), ::serde_json::Value::Bool(false));
259 ::serde_json::Value::Object(obj)
260}