Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
VoteRequest.borrowed.rs

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