Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
BeginQuorumEpochRequest.owned.rs

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