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.
2
3use bytes::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, nullable_string_len,
8    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
9    string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12    get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13    get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
16use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
17
18pub const API_KEY: i16 = 52;
19pub const MIN_VERSION: i16 = 0;
20pub const MAX_VERSION: i16 = 2;
21pub const FLEXIBLE_MIN: i16 = 0;
22
23#[inline]
24fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct VoteRequest<'a> {
28    pub cluster_id: Option<&'a str>,
29    pub voter_id: i32,
30    pub topics: Vec<TopicData<'a>>,
31    pub unknown_tagged_fields: UnknownTaggedFields,
32}
33
34impl<'a> Default for VoteRequest<'a> {
35    fn default() -> Self {
36        Self {
37            cluster_id: None,
38            voter_id: -1i32,
39            topics: Vec::new(),
40            unknown_tagged_fields: Default::default(),
41        }
42    }
43}
44
45impl<'a> VoteRequest<'a> {
46    pub fn to_owned(&self) -> crate::owned::vote_request::VoteRequest {
47        crate::owned::vote_request::VoteRequest {
48            cluster_id: (self.cluster_id).map(|s| s.to_string()),
49            voter_id: (self.voter_id),
50            topics: (self.topics).iter().map(|it| it.to_owned()).collect(),
51            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
52        }
53    }
54}
55
56impl<'a> Encode for VoteRequest<'a> {
57    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
58        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
59            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
60        }
61        let flex = is_flexible(version);
62        if version >= 0 { if flex { put_compact_nullable_string(buf, self.cluster_id) } else { put_nullable_string(buf, self.cluster_id) } }
63        if version >= 1 { put_i32(buf, self.voter_id) }
64        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
65        if flex {
66            let tagged = WriteTaggedFields::new();
67            tagged.write(buf, &self.unknown_tagged_fields);
68        }
69        Ok(())
70    }
71    fn encoded_len(&self, version: i16) -> usize {
72        let flex = is_flexible(version);
73        let mut n: usize = 0;
74        if version >= 0 { n += if flex { compact_nullable_string_len(self.cluster_id) } else { nullable_string_len(self.cluster_id) }; }
75        if version >= 1 { n += 4; }
76        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
77        if flex {
78            let known_pairs: Vec<(u32, usize)> = Vec::new();
79            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
80        }
81        n
82    }
83}
84
85impl<'de> DecodeBorrow<'de> for VoteRequest<'de> {
86    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
87        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
88            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
89        }
90        let flex = is_flexible(version);
91        let mut out = Self::default();
92        if version >= 0 { out.cluster_id = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
93        if version >= 1 { out.voter_id = get_i32(buf)?; }
94        if version >= 0 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(TopicData::decode_borrow(buf, version)?); } v }; }
95        if flex {
96            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
97                Ok(false)
98            })?;
99        }
100        Ok(out)
101    }
102}
103
104#[derive(Debug, Clone, PartialEq, Eq)]
105pub struct TopicData<'a> {
106    pub topic_name: &'a str,
107    pub partitions: Vec<PartitionData>,
108    pub unknown_tagged_fields: UnknownTaggedFields,
109}
110
111impl<'a> Default for TopicData<'a> {
112    fn default() -> Self {
113        Self {
114            topic_name: "",
115            partitions: Vec::new(),
116            unknown_tagged_fields: Default::default(),
117        }
118    }
119}
120
121impl<'a> TopicData<'a> {
122    pub fn to_owned(&self) -> crate::owned::vote_request::TopicData {
123        crate::owned::vote_request::TopicData {
124            topic_name: (self.topic_name).to_string(),
125            partitions: (self.partitions).iter().map(|it| it.to_owned()).collect(),
126            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
127        }
128    }
129}
130
131impl<'a> Encode for TopicData<'a> {
132    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
133        let flex = version >= 0;
134        if version >= 0 { if flex { put_compact_string(buf, self.topic_name) } else { put_string(buf, self.topic_name) } }
135        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
136        if flex {
137            let tagged = WriteTaggedFields::new();
138            tagged.write(buf, &self.unknown_tagged_fields);
139        }
140        Ok(())
141    }
142    fn encoded_len(&self, version: i16) -> usize {
143        let flex = version >= 0;
144        let mut n: usize = 0;
145        if version >= 0 { n += if flex { compact_string_len(self.topic_name) } else { string_len(self.topic_name) }; }
146        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
147        if flex {
148            let known_pairs: Vec<(u32, usize)> = Vec::new();
149            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
150        }
151        n
152    }
153}
154
155impl<'de> DecodeBorrow<'de> for TopicData<'de> {
156    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
157        let flex = version >= 0;
158        let mut out = Self::default();
159        if version >= 0 { out.topic_name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
160        if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(PartitionData::decode_borrow(buf, version)?); } v }; }
161        if flex {
162            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
163                Ok(false)
164            })?;
165        }
166        Ok(out)
167    }
168}
169
170#[derive(Debug, Clone, PartialEq, Eq)]
171pub struct PartitionData {
172    pub partition_index: i32,
173    pub replica_epoch: i32,
174    pub replica_id: i32,
175    pub replica_directory_id: crate::primitives::uuid::Uuid,
176    pub voter_directory_id: crate::primitives::uuid::Uuid,
177    pub last_offset_epoch: i32,
178    pub last_offset: i64,
179    pub pre_vote: bool,
180    pub unknown_tagged_fields: UnknownTaggedFields,
181}
182
183impl Default for PartitionData {
184    fn default() -> Self {
185        Self {
186            partition_index: 0i32,
187            replica_epoch: 0i32,
188            replica_id: 0i32,
189            replica_directory_id: Default::default(),
190            voter_directory_id: Default::default(),
191            last_offset_epoch: 0i32,
192            last_offset: 0i64,
193            pre_vote: false,
194            unknown_tagged_fields: Default::default(),
195        }
196    }
197}
198
199impl PartitionData {
200    pub fn to_owned(&self) -> crate::owned::vote_request::PartitionData {
201        crate::owned::vote_request::PartitionData {
202            partition_index: (self.partition_index),
203            replica_epoch: (self.replica_epoch),
204            replica_id: (self.replica_id),
205            replica_directory_id: (self.replica_directory_id),
206            voter_directory_id: (self.voter_directory_id),
207            last_offset_epoch: (self.last_offset_epoch),
208            last_offset: (self.last_offset),
209            pre_vote: (self.pre_vote),
210            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
211        }
212    }
213}
214
215impl Encode for PartitionData {
216    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
217        let flex = version >= 0;
218        if version >= 0 { put_i32(buf, self.partition_index) }
219        if version >= 0 { put_i32(buf, self.replica_epoch) }
220        if version >= 0 { put_i32(buf, self.replica_id) }
221        if version >= 1 { crate::primitives::uuid::put_uuid(buf, self.replica_directory_id) }
222        if version >= 1 { crate::primitives::uuid::put_uuid(buf, self.voter_directory_id) }
223        if version >= 0 { put_i32(buf, self.last_offset_epoch) }
224        if version >= 0 { put_i64(buf, self.last_offset) }
225        if version >= 2 { put_bool(buf, self.pre_vote) }
226        if flex {
227            let tagged = WriteTaggedFields::new();
228            tagged.write(buf, &self.unknown_tagged_fields);
229        }
230        Ok(())
231    }
232    fn encoded_len(&self, version: i16) -> usize {
233        let flex = version >= 0;
234        let mut n: usize = 0;
235        if version >= 0 { n += 4; }
236        if version >= 0 { n += 4; }
237        if version >= 0 { n += 4; }
238        if version >= 1 { n += 16; }
239        if version >= 1 { n += 16; }
240        if version >= 0 { n += 4; }
241        if version >= 0 { n += 8; }
242        if version >= 2 { n += 1; }
243        if flex {
244            let known_pairs: Vec<(u32, usize)> = Vec::new();
245            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
246        }
247        n
248    }
249}
250
251impl<'de> DecodeBorrow<'de> for PartitionData {
252    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
253        let flex = version >= 0;
254        let mut out = Self::default();
255        if version >= 0 { out.partition_index = get_i32(buf)?; }
256        if version >= 0 { out.replica_epoch = get_i32(buf)?; }
257        if version >= 0 { out.replica_id = get_i32(buf)?; }
258        if version >= 1 { out.replica_directory_id = crate::primitives::uuid::get_uuid(buf)?; }
259        if version >= 1 { out.voter_directory_id = crate::primitives::uuid::get_uuid(buf)?; }
260        if version >= 0 { out.last_offset_epoch = get_i32(buf)?; }
261        if version >= 0 { out.last_offset = get_i64(buf)?; }
262        if version >= 2 { out.pre_vote = get_bool(buf)?; }
263        if flex {
264            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
265                Ok(false)
266            })?;
267        }
268        Ok(out)
269    }
270}