crabka_protocol/opt/rustwide/workdir/generated/
VoteResponse.owned.rs1use crate::primitives::fixed::{
4 get_bool, get_i16, get_i32, get_u16, put_bool, put_i16, put_i32, put_u16,
5};
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::tagged_fields::{
11 WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len,
12};
13use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
14use bytes::{Buf, BufMut};
15pub const API_KEY: i16 = 52;
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 2;
18pub const FLEXIBLE_MIN: i16 = 0;
19#[inline]
20fn is_flexible(version: i16) -> bool {
21 version >= FLEXIBLE_MIN
22}
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct VoteResponse {
25 pub error_code: i16,
26 pub topics: Vec<TopicData>,
27 pub node_endpoints: Vec<NodeEndpoint>,
28 pub unknown_tagged_fields: UnknownTaggedFields,
29}
30impl Encode for VoteResponse {
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.topics).len(), flex);
45 for it in &self.topics {
46 it.encode(buf, version)?;
47 }
48 }
49 }
50 if flex {
51 let mut tagged = WriteTaggedFields::new();
52 if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
53 let payload = encode_to_bytes(
54 {
55 let prefix = crate::primitives::array::array_len_prefix_len(
56 (self.node_endpoints).len(),
57 flex,
58 );
59 let body: usize = (self.node_endpoints)
60 .iter()
61 .map(|it| it.encoded_len(version))
62 .sum();
63 prefix + body
64 },
65 |b| {
66 {
67 crate::primitives::array::put_array_len(
68 b,
69 (self.node_endpoints).len(),
70 flex,
71 );
72 for it in &self.node_endpoints {
73 it.encode(b, version)?;
74 }
75 };
76 Ok(())
77 },
78 );
79 tagged.add(0, payload);
80 }
81 tagged.write(buf, &self.unknown_tagged_fields);
82 }
83 Ok(())
84 }
85 fn encoded_len(&self, version: i16) -> usize {
86 let flex = is_flexible(version);
87 let mut n: usize = 0;
88 if version >= 0 {
89 n += 2;
90 }
91 if version >= 0 {
92 n += {
93 let prefix =
94 crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
95 let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
96 prefix + body
97 };
98 }
99 if flex {
100 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
101 if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
102 known_pairs.push((0, {
103 let prefix = crate::primitives::array::array_len_prefix_len(
104 (self.node_endpoints).len(),
105 flex,
106 );
107 let body: usize = (self.node_endpoints)
108 .iter()
109 .map(|it| it.encoded_len(version))
110 .sum();
111 prefix + body
112 }));
113 }
114 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
115 }
116 n
117 }
118}
119impl Decode<'_> for VoteResponse {
120 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
121 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
122 return Err(ProtocolError::UnsupportedVersion {
123 api_key: API_KEY,
124 version,
125 });
126 }
127 let flex = is_flexible(version);
128 let mut out = Self::default();
129 if version >= 0 {
130 out.error_code = get_i16(buf)?;
131 }
132 if version >= 0 {
133 out.topics = {
134 let n = crate::primitives::array::get_array_len(buf, flex)?;
135 let mut v = Vec::with_capacity(n);
136 for _ in 0..n {
137 v.push(TopicData::decode(buf, version)?);
138 }
139 v
140 };
141 }
142 if flex {
143 let mut tag_node_endpoints = None;
144 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
145 0 => {
146 tag_node_endpoints = Some({
147 let b: &mut &[u8] = payload;
148 {
149 let n = crate::primitives::array::get_array_len(b, flex)?;
150 let mut v = Vec::with_capacity(n);
151 for _ in 0..n {
152 v.push(NodeEndpoint::decode(b, version)?);
153 }
154 v
155 }
156 });
157 Ok(true)
158 }
159 _ => Ok(false),
160 })?;
161 if let Some(v) = tag_node_endpoints {
162 out.node_endpoints = v;
163 }
164 }
165 Ok(out)
166 }
167}
168#[cfg(test)]
169impl VoteResponse {
170 #[must_use]
171 pub fn populated(version: i16) -> Self {
172 let mut m = Self::default();
173 if version >= 0 {
174 m.error_code = 1i16;
175 }
176 if version >= 0 {
177 m.topics = vec![TopicData::populated(version)];
178 }
179 if version >= 1 {
180 m.node_endpoints = vec![NodeEndpoint::populated(version)];
181 }
182 m
183 }
184}
185#[derive(Debug, Clone, PartialEq, Eq, Default)]
186pub struct TopicData {
187 pub topic_name: String,
188 pub partitions: Vec<PartitionData>,
189 pub unknown_tagged_fields: UnknownTaggedFields,
190}
191impl Encode for TopicData {
192 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
193 let flex = version >= 0;
194 if version >= 0 {
195 if flex {
196 put_compact_string(buf, &self.topic_name);
197 } else {
198 put_string(buf, &self.topic_name);
199 }
200 }
201 if version >= 0 {
202 {
203 crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
204 for it in &self.partitions {
205 it.encode(buf, version)?;
206 }
207 }
208 }
209 if flex {
210 let tagged = WriteTaggedFields::new();
211 tagged.write(buf, &self.unknown_tagged_fields);
212 }
213 Ok(())
214 }
215 fn encoded_len(&self, version: i16) -> usize {
216 let flex = version >= 0;
217 let mut n: usize = 0;
218 if version >= 0 {
219 n += if flex {
220 compact_string_len(&self.topic_name)
221 } else {
222 string_len(&self.topic_name)
223 };
224 }
225 if version >= 0 {
226 n += {
227 let prefix =
228 crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
229 let body: usize = (self.partitions)
230 .iter()
231 .map(|it| it.encoded_len(version))
232 .sum();
233 prefix + body
234 };
235 }
236 if flex {
237 let known_pairs: Vec<(u32, usize)> = Vec::new();
238 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
239 }
240 n
241 }
242}
243impl Decode<'_> for TopicData {
244 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
245 let flex = version >= 0;
246 let mut out = Self::default();
247 if version >= 0 {
248 out.topic_name = if flex {
249 get_compact_string_owned(buf)?
250 } else {
251 get_string_owned(buf)?
252 };
253 }
254 if version >= 0 {
255 out.partitions = {
256 let n = crate::primitives::array::get_array_len(buf, flex)?;
257 let mut v = Vec::with_capacity(n);
258 for _ in 0..n {
259 v.push(PartitionData::decode(buf, version)?);
260 }
261 v
262 };
263 }
264 if flex {
265 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
266 }
267 Ok(out)
268 }
269}
270#[cfg(test)]
271impl TopicData {
272 #[must_use]
273 pub fn populated(version: i16) -> Self {
274 let mut m = Self::default();
275 if version >= 0 {
276 m.topic_name = "x".to_string();
277 }
278 if version >= 0 {
279 m.partitions = vec![PartitionData::populated(version)];
280 }
281 m
282 }
283}
284#[derive(Debug, Clone, PartialEq, Eq, Default)]
285pub struct PartitionData {
286 pub partition_index: i32,
287 pub error_code: i16,
288 pub leader_id: i32,
289 pub leader_epoch: i32,
290 pub vote_granted: bool,
291 pub unknown_tagged_fields: UnknownTaggedFields,
292}
293impl Encode for PartitionData {
294 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
295 let flex = version >= 0;
296 if version >= 0 {
297 put_i32(buf, self.partition_index);
298 }
299 if version >= 0 {
300 put_i16(buf, self.error_code);
301 }
302 if version >= 0 {
303 put_i32(buf, self.leader_id);
304 }
305 if version >= 0 {
306 put_i32(buf, self.leader_epoch);
307 }
308 if version >= 0 {
309 put_bool(buf, self.vote_granted);
310 }
311 if flex {
312 let tagged = WriteTaggedFields::new();
313 tagged.write(buf, &self.unknown_tagged_fields);
314 }
315 Ok(())
316 }
317 fn encoded_len(&self, version: i16) -> usize {
318 let flex = version >= 0;
319 let mut n: usize = 0;
320 if version >= 0 {
321 n += 4;
322 }
323 if version >= 0 {
324 n += 2;
325 }
326 if version >= 0 {
327 n += 4;
328 }
329 if version >= 0 {
330 n += 4;
331 }
332 if version >= 0 {
333 n += 1;
334 }
335 if flex {
336 let known_pairs: Vec<(u32, usize)> = Vec::new();
337 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
338 }
339 n
340 }
341}
342impl Decode<'_> for PartitionData {
343 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
344 let flex = version >= 0;
345 let mut out = Self::default();
346 if version >= 0 {
347 out.partition_index = get_i32(buf)?;
348 }
349 if version >= 0 {
350 out.error_code = get_i16(buf)?;
351 }
352 if version >= 0 {
353 out.leader_id = get_i32(buf)?;
354 }
355 if version >= 0 {
356 out.leader_epoch = get_i32(buf)?;
357 }
358 if version >= 0 {
359 out.vote_granted = get_bool(buf)?;
360 }
361 if flex {
362 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
363 }
364 Ok(out)
365 }
366}
367#[cfg(test)]
368impl PartitionData {
369 #[must_use]
370 pub fn populated(version: i16) -> Self {
371 let mut m = Self::default();
372 if version >= 0 {
373 m.partition_index = 1i32;
374 }
375 if version >= 0 {
376 m.error_code = 1i16;
377 }
378 if version >= 0 {
379 m.leader_id = 1i32;
380 }
381 if version >= 0 {
382 m.leader_epoch = 1i32;
383 }
384 if version >= 0 {
385 m.vote_granted = true;
386 }
387 m
388 }
389}
390#[derive(Debug, Clone, PartialEq, Eq, Default)]
391pub struct NodeEndpoint {
392 pub node_id: i32,
393 pub host: String,
394 pub port: u16,
395 pub unknown_tagged_fields: UnknownTaggedFields,
396}
397impl Encode for NodeEndpoint {
398 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
399 let flex = version >= 0;
400 if version >= 1 {
401 put_i32(buf, self.node_id);
402 }
403 if version >= 1 {
404 if flex {
405 put_compact_string(buf, &self.host);
406 } else {
407 put_string(buf, &self.host);
408 }
409 }
410 if version >= 1 {
411 put_u16(buf, self.port);
412 }
413 if flex {
414 let tagged = WriteTaggedFields::new();
415 tagged.write(buf, &self.unknown_tagged_fields);
416 }
417 Ok(())
418 }
419 fn encoded_len(&self, version: i16) -> usize {
420 let flex = version >= 0;
421 let mut n: usize = 0;
422 if version >= 1 {
423 n += 4;
424 }
425 if version >= 1 {
426 n += if flex {
427 compact_string_len(&self.host)
428 } else {
429 string_len(&self.host)
430 };
431 }
432 if version >= 1 {
433 n += 2;
434 }
435 if flex {
436 let known_pairs: Vec<(u32, usize)> = Vec::new();
437 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
438 }
439 n
440 }
441}
442impl Decode<'_> for NodeEndpoint {
443 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
444 let flex = version >= 0;
445 let mut out = Self::default();
446 if version >= 1 {
447 out.node_id = get_i32(buf)?;
448 }
449 if version >= 1 {
450 out.host = if flex {
451 get_compact_string_owned(buf)?
452 } else {
453 get_string_owned(buf)?
454 };
455 }
456 if version >= 1 {
457 out.port = get_u16(buf)?;
458 }
459 if flex {
460 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
461 }
462 Ok(out)
463 }
464}
465#[cfg(test)]
466impl NodeEndpoint {
467 #[must_use]
468 pub fn populated(version: i16) -> Self {
469 let mut m = Self::default();
470 if version >= 1 {
471 m.node_id = 1i32;
472 }
473 if version >= 1 {
474 m.host = "x".to_string();
475 }
476 if version >= 1 {
477 m.port = 1u16;
478 }
479 m
480 }
481}
482#[must_use]
485#[allow(unused_comparisons)]
486pub fn default_json(version: i16) -> ::serde_json::Value {
487 let mut obj = ::serde_json::Map::new();
488 obj.insert("errorCode".to_string(), ::serde_json::json!(0));
489 obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
490 if version >= 1 {
491 obj.insert(
492 "nodeEndpoints".to_string(),
493 ::serde_json::Value::Array(vec![]),
494 );
495 }
496 ::serde_json::Value::Object(obj)
497}