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