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