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