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