Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
VoteRequest.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_bool, get_i32, get_i64, put_bool, put_i32, put_i64};
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 = 52;
15pub const MIN_VERSION: i16 = 0;
16pub const MAX_VERSION: i16 = 2;
17pub const FLEXIBLE_MIN: i16 = 0;
18
19#[inline]
20fn is_flexible(version: i16) -> bool {
21    version >= FLEXIBLE_MIN
22}
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct VoteRequest {
26    pub cluster_id: Option<String>,
27    pub voter_id: i32,
28    pub topics: Vec<TopicData>,
29    pub unknown_tagged_fields: UnknownTaggedFields,
30}
31impl Default for VoteRequest {
32    fn default() -> Self {
33        Self {
34            cluster_id: None,
35            voter_id: -1i32,
36            topics: Vec::new(),
37            unknown_tagged_fields: Default::default(),
38        }
39    }
40}
41impl Encode for VoteRequest {
42    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
43        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
44            return Err(ProtocolError::UnsupportedVersion {
45                api_key: API_KEY,
46                version,
47            });
48        }
49        let flex = is_flexible(version);
50        if version >= 0 {
51            if flex {
52                put_compact_nullable_string(buf, self.cluster_id.as_deref());
53            } else {
54                put_nullable_string(buf, self.cluster_id.as_deref());
55            }
56        }
57        if version >= 1 {
58            put_i32(buf, self.voter_id);
59        }
60        if version >= 0 {
61            {
62                crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
63                for it in &self.topics {
64                    it.encode(buf, version)?;
65                }
66            }
67        }
68        if flex {
69            let tagged = WriteTaggedFields::new();
70            tagged.write(buf, &self.unknown_tagged_fields);
71        }
72        Ok(())
73    }
74    fn encoded_len(&self, version: i16) -> usize {
75        let flex = is_flexible(version);
76        let mut n: usize = 0;
77        if version >= 0 {
78            n += if flex {
79                compact_nullable_string_len(self.cluster_id.as_deref())
80            } else {
81                nullable_string_len(self.cluster_id.as_deref())
82            };
83        }
84        if version >= 1 {
85            n += 4;
86        }
87        if version >= 0 {
88            n += {
89                let prefix =
90                    crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
91                let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
92                prefix + body
93            };
94        }
95        if flex {
96            let known_pairs: Vec<(u32, usize)> = Vec::new();
97            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
98        }
99        n
100    }
101}
102impl Decode<'_> for VoteRequest {
103    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
104        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
105            return Err(ProtocolError::UnsupportedVersion {
106                api_key: API_KEY,
107                version,
108            });
109        }
110        let flex = is_flexible(version);
111        let mut out = Self::default();
112        if version >= 0 {
113            out.cluster_id = if flex {
114                get_compact_nullable_string_owned(buf)?
115            } else {
116                get_nullable_string_owned(buf)?
117            };
118        }
119        if version >= 1 {
120            out.voter_id = get_i32(buf)?;
121        }
122        if version >= 0 {
123            out.topics = {
124                let n = crate::primitives::array::get_array_len(buf, flex)?;
125                let mut v = Vec::with_capacity(n);
126                for _ in 0..n {
127                    v.push(TopicData::decode(buf, version)?);
128                }
129                v
130            };
131        }
132        if flex {
133            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
134        }
135        Ok(out)
136    }
137}
138#[cfg(test)]
139impl VoteRequest {
140    #[must_use]
141    pub fn populated(version: i16) -> Self {
142        let mut m = Self::default();
143        if version >= 0 {
144            m.cluster_id = Some("x".to_string());
145        }
146        if version >= 1 {
147            m.voter_id = 1i32;
148        }
149        if version >= 0 {
150            m.topics = vec![TopicData::populated(version)];
151        }
152        m
153    }
154}
155#[derive(Debug, Clone, PartialEq, Eq, Default)]
156pub struct TopicData {
157    pub topic_name: String,
158    pub partitions: Vec<PartitionData>,
159    pub unknown_tagged_fields: UnknownTaggedFields,
160}
161impl Encode for TopicData {
162    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
163        let flex = version >= 0;
164        if version >= 0 {
165            if flex {
166                put_compact_string(buf, &self.topic_name);
167            } else {
168                put_string(buf, &self.topic_name);
169            }
170        }
171        if version >= 0 {
172            {
173                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
174                for it in &self.partitions {
175                    it.encode(buf, version)?;
176                }
177            }
178        }
179        if flex {
180            let tagged = WriteTaggedFields::new();
181            tagged.write(buf, &self.unknown_tagged_fields);
182        }
183        Ok(())
184    }
185    fn encoded_len(&self, version: i16) -> usize {
186        let flex = version >= 0;
187        let mut n: usize = 0;
188        if version >= 0 {
189            n += if flex {
190                compact_string_len(&self.topic_name)
191            } else {
192                string_len(&self.topic_name)
193            };
194        }
195        if version >= 0 {
196            n += {
197                let prefix =
198                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
199                let body: usize = (self.partitions)
200                    .iter()
201                    .map(|it| it.encoded_len(version))
202                    .sum();
203                prefix + body
204            };
205        }
206        if flex {
207            let known_pairs: Vec<(u32, usize)> = Vec::new();
208            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
209        }
210        n
211    }
212}
213impl Decode<'_> for TopicData {
214    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
215        let flex = version >= 0;
216        let mut out = Self::default();
217        if version >= 0 {
218            out.topic_name = if flex {
219                get_compact_string_owned(buf)?
220            } else {
221                get_string_owned(buf)?
222            };
223        }
224        if version >= 0 {
225            out.partitions = {
226                let n = crate::primitives::array::get_array_len(buf, flex)?;
227                let mut v = Vec::with_capacity(n);
228                for _ in 0..n {
229                    v.push(PartitionData::decode(buf, version)?);
230                }
231                v
232            };
233        }
234        if flex {
235            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
236        }
237        Ok(out)
238    }
239}
240#[cfg(test)]
241impl TopicData {
242    #[must_use]
243    pub fn populated(version: i16) -> Self {
244        let mut m = Self::default();
245        if version >= 0 {
246            m.topic_name = "x".to_string();
247        }
248        if version >= 0 {
249            m.partitions = vec![PartitionData::populated(version)];
250        }
251        m
252    }
253}
254#[derive(Debug, Clone, PartialEq, Eq, Default)]
255pub struct PartitionData {
256    pub partition_index: i32,
257    pub replica_epoch: i32,
258    pub replica_id: i32,
259    pub replica_directory_id: crate::primitives::uuid::Uuid,
260    pub voter_directory_id: crate::primitives::uuid::Uuid,
261    pub last_offset_epoch: i32,
262    pub last_offset: i64,
263    pub pre_vote: bool,
264    pub unknown_tagged_fields: UnknownTaggedFields,
265}
266impl Encode for PartitionData {
267    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
268        let flex = version >= 0;
269        if version >= 0 {
270            put_i32(buf, self.partition_index);
271        }
272        if version >= 0 {
273            put_i32(buf, self.replica_epoch);
274        }
275        if version >= 0 {
276            put_i32(buf, self.replica_id);
277        }
278        if version >= 1 {
279            crate::primitives::uuid::put_uuid(buf, self.replica_directory_id);
280        }
281        if version >= 1 {
282            crate::primitives::uuid::put_uuid(buf, self.voter_directory_id);
283        }
284        if version >= 0 {
285            put_i32(buf, self.last_offset_epoch);
286        }
287        if version >= 0 {
288            put_i64(buf, self.last_offset);
289        }
290        if version >= 2 {
291            put_bool(buf, self.pre_vote);
292        }
293        if flex {
294            let tagged = WriteTaggedFields::new();
295            tagged.write(buf, &self.unknown_tagged_fields);
296        }
297        Ok(())
298    }
299    fn encoded_len(&self, version: i16) -> usize {
300        let flex = version >= 0;
301        let mut n: usize = 0;
302        if version >= 0 {
303            n += 4;
304        }
305        if version >= 0 {
306            n += 4;
307        }
308        if version >= 0 {
309            n += 4;
310        }
311        if version >= 1 {
312            n += 16;
313        }
314        if version >= 1 {
315            n += 16;
316        }
317        if version >= 0 {
318            n += 4;
319        }
320        if version >= 0 {
321            n += 8;
322        }
323        if version >= 2 {
324            n += 1;
325        }
326        if flex {
327            let known_pairs: Vec<(u32, usize)> = Vec::new();
328            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
329        }
330        n
331    }
332}
333impl Decode<'_> for PartitionData {
334    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
335        let flex = version >= 0;
336        let mut out = Self::default();
337        if version >= 0 {
338            out.partition_index = get_i32(buf)?;
339        }
340        if version >= 0 {
341            out.replica_epoch = get_i32(buf)?;
342        }
343        if version >= 0 {
344            out.replica_id = get_i32(buf)?;
345        }
346        if version >= 1 {
347            out.replica_directory_id = crate::primitives::uuid::get_uuid(buf)?;
348        }
349        if version >= 1 {
350            out.voter_directory_id = crate::primitives::uuid::get_uuid(buf)?;
351        }
352        if version >= 0 {
353            out.last_offset_epoch = get_i32(buf)?;
354        }
355        if version >= 0 {
356            out.last_offset = get_i64(buf)?;
357        }
358        if version >= 2 {
359            out.pre_vote = 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.replica_epoch = 1i32;
377        }
378        if version >= 0 {
379            m.replica_id = 1i32;
380        }
381        if version >= 1 {
382            m.replica_directory_id = crate::primitives::uuid::Uuid([1u8; 16]);
383        }
384        if version >= 1 {
385            m.voter_directory_id = crate::primitives::uuid::Uuid([1u8; 16]);
386        }
387        if version >= 0 {
388            m.last_offset_epoch = 1i32;
389        }
390        if version >= 0 {
391            m.last_offset = 1i64;
392        }
393        if version >= 2 {
394            m.pre_vote = true;
395        }
396        m
397    }
398}
399
400/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
401/// Only includes fields valid for the given version.
402#[must_use]
403#[allow(unused_comparisons)]
404pub fn default_json(version: i16) -> ::serde_json::Value {
405    let mut obj = ::serde_json::Map::new();
406    obj.insert("clusterId".to_string(), ::serde_json::Value::Null);
407    if version >= 1 {
408        obj.insert("voterId".to_string(), ::serde_json::json!(-1));
409    }
410    obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
411    ::serde_json::Value::Object(obj)
412}
413
414impl crate::ProtocolRequest for VoteRequest {
415    const API_KEY: i16 = API_KEY;
416    const MIN_VERSION: i16 = MIN_VERSION;
417    const MAX_VERSION: i16 = MAX_VERSION;
418    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
419    type Response = super::vote_response::VoteResponse;
420}