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