crabka_protocol/opt/rustwide/workdir/generated/
ElectLeadersResponse.borrowed.rs1use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
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 = 43;
15pub const MIN_VERSION: i16 = 0;
16pub const MAX_VERSION: i16 = 2;
17pub const FLEXIBLE_MIN: i16 = 2;
18#[inline]
19fn is_flexible(version: i16) -> bool {
20 version >= FLEXIBLE_MIN
21}
22#[derive(Debug, Clone, PartialEq, Eq, Default)]
23pub struct ElectLeadersResponse<'a> {
24 pub throttle_time_ms: i32,
25 pub error_code: i16,
26 pub replica_election_results: Vec<ReplicaElectionResult<'a>>,
27 pub unknown_tagged_fields: UnknownTaggedFields,
28}
29impl ElectLeadersResponse<'_> {
30 pub fn to_owned(&self) -> crate::owned::elect_leaders_response::ElectLeadersResponse {
31 crate::owned::elect_leaders_response::ElectLeadersResponse {
32 throttle_time_ms: (self.throttle_time_ms),
33 error_code: (self.error_code),
34 replica_election_results: (self.replica_election_results)
35 .iter()
36 .map(ReplicaElectionResult::to_owned)
37 .collect(),
38 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
39 }
40 }
41}
42impl Encode for ElectLeadersResponse<'_> {
43 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
44 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
45 return Err(ProtocolError::UnsupportedVersion {
46 api_key: API_KEY,
47 version,
48 });
49 }
50 let flex = is_flexible(version);
51 if version >= 0 {
52 put_i32(buf, self.throttle_time_ms);
53 }
54 if version >= 1 {
55 put_i16(buf, self.error_code);
56 }
57 if version >= 0 {
58 {
59 crate::primitives::array::put_array_len(
60 buf,
61 (self.replica_election_results).len(),
62 flex,
63 );
64 for it in &self.replica_election_results {
65 it.encode(buf, version)?;
66 }
67 }
68 }
69 if flex {
70 let tagged = WriteTaggedFields::new();
71 tagged.write(buf, &self.unknown_tagged_fields);
72 }
73 Ok(())
74 }
75 fn encoded_len(&self, version: i16) -> usize {
76 let flex = is_flexible(version);
77 let mut n: usize = 0;
78 if version >= 0 {
79 n += 4;
80 }
81 if version >= 1 {
82 n += 2;
83 }
84 if version >= 0 {
85 n += {
86 let prefix = crate::primitives::array::array_len_prefix_len(
87 (self.replica_election_results).len(),
88 flex,
89 );
90 let body: usize = (self.replica_election_results)
91 .iter()
92 .map(|it| it.encoded_len(version))
93 .sum();
94 prefix + body
95 };
96 }
97 if flex {
98 let known_pairs: Vec<(u32, usize)> = Vec::new();
99 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
100 }
101 n
102 }
103}
104impl<'de> DecodeBorrow<'de> for ElectLeadersResponse<'de> {
105 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
106 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
107 return Err(ProtocolError::UnsupportedVersion {
108 api_key: API_KEY,
109 version,
110 });
111 }
112 let flex = is_flexible(version);
113 let mut out = Self::default();
114 if version >= 0 {
115 out.throttle_time_ms = get_i32(buf)?;
116 }
117 if version >= 1 {
118 out.error_code = get_i16(buf)?;
119 }
120 if version >= 0 {
121 out.replica_election_results = {
122 let n = crate::primitives::array::get_array_len(buf, flex)?;
123 let mut v = Vec::with_capacity(n);
124 for _ in 0..n {
125 v.push(ReplicaElectionResult::decode_borrow(buf, version)?);
126 }
127 v
128 };
129 }
130 if flex {
131 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
132 }
133 Ok(out)
134 }
135}
136#[cfg(test)]
137impl ElectLeadersResponse<'_> {
138 #[must_use]
139 pub fn populated(version: i16) -> Self {
140 let mut m = Self::default();
141 if version >= 0 {
142 m.throttle_time_ms = 1i32;
143 }
144 if version >= 1 {
145 m.error_code = 1i16;
146 }
147 if version >= 0 {
148 m.replica_election_results = vec![ReplicaElectionResult::populated(version)];
149 }
150 m
151 }
152}
153#[derive(Debug, Clone, PartialEq, Eq, Default)]
154pub struct ReplicaElectionResult<'a> {
155 pub topic: &'a str,
156 pub partition_result: Vec<PartitionResult<'a>>,
157 pub unknown_tagged_fields: UnknownTaggedFields,
158}
159impl ReplicaElectionResult<'_> {
160 pub fn to_owned(&self) -> crate::owned::elect_leaders_response::ReplicaElectionResult {
161 crate::owned::elect_leaders_response::ReplicaElectionResult {
162 topic: (self.topic).to_string(),
163 partition_result: (self.partition_result)
164 .iter()
165 .map(PartitionResult::to_owned)
166 .collect(),
167 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
168 }
169 }
170}
171impl Encode for ReplicaElectionResult<'_> {
172 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
173 let flex = version >= 2;
174 if version >= 0 {
175 if flex {
176 put_compact_string(buf, self.topic);
177 } else {
178 put_string(buf, self.topic);
179 }
180 }
181 if version >= 0 {
182 {
183 crate::primitives::array::put_array_len(buf, (self.partition_result).len(), flex);
184 for it in &self.partition_result {
185 it.encode(buf, version)?;
186 }
187 }
188 }
189 if flex {
190 let tagged = WriteTaggedFields::new();
191 tagged.write(buf, &self.unknown_tagged_fields);
192 }
193 Ok(())
194 }
195 fn encoded_len(&self, version: i16) -> usize {
196 let flex = version >= 2;
197 let mut n: usize = 0;
198 if version >= 0 {
199 n += if flex {
200 compact_string_len(self.topic)
201 } else {
202 string_len(self.topic)
203 };
204 }
205 if version >= 0 {
206 n += {
207 let prefix = crate::primitives::array::array_len_prefix_len(
208 (self.partition_result).len(),
209 flex,
210 );
211 let body: usize = (self.partition_result)
212 .iter()
213 .map(|it| it.encoded_len(version))
214 .sum();
215 prefix + body
216 };
217 }
218 if flex {
219 let known_pairs: Vec<(u32, usize)> = Vec::new();
220 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
221 }
222 n
223 }
224}
225impl<'de> DecodeBorrow<'de> for ReplicaElectionResult<'de> {
226 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
227 let flex = version >= 2;
228 let mut out = Self::default();
229 if version >= 0 {
230 out.topic = if flex {
231 get_compact_string_borrowed(buf)?
232 } else {
233 get_string_borrowed(buf)?
234 };
235 }
236 if version >= 0 {
237 out.partition_result = {
238 let n = crate::primitives::array::get_array_len(buf, flex)?;
239 let mut v = Vec::with_capacity(n);
240 for _ in 0..n {
241 v.push(PartitionResult::decode_borrow(buf, version)?);
242 }
243 v
244 };
245 }
246 if flex {
247 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
248 }
249 Ok(out)
250 }
251}
252#[cfg(test)]
253impl ReplicaElectionResult<'_> {
254 #[must_use]
255 pub fn populated(version: i16) -> Self {
256 let mut m = Self::default();
257 if version >= 0 {
258 m.topic = "x";
259 }
260 if version >= 0 {
261 m.partition_result = vec![PartitionResult::populated(version)];
262 }
263 m
264 }
265}
266#[derive(Debug, Clone, PartialEq, Eq, Default)]
267pub struct PartitionResult<'a> {
268 pub partition_id: i32,
269 pub error_code: i16,
270 pub error_message: Option<&'a str>,
271 pub unknown_tagged_fields: UnknownTaggedFields,
272}
273impl PartitionResult<'_> {
274 pub fn to_owned(&self) -> crate::owned::elect_leaders_response::PartitionResult {
275 crate::owned::elect_leaders_response::PartitionResult {
276 partition_id: (self.partition_id),
277 error_code: (self.error_code),
278 error_message: (self.error_message).map(std::string::ToString::to_string),
279 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
280 }
281 }
282}
283impl Encode for PartitionResult<'_> {
284 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
285 let flex = version >= 2;
286 if version >= 0 {
287 put_i32(buf, self.partition_id);
288 }
289 if version >= 0 {
290 put_i16(buf, self.error_code);
291 }
292 if version >= 0 {
293 if flex {
294 put_compact_nullable_string(buf, self.error_message);
295 } else {
296 put_nullable_string(buf, self.error_message);
297 }
298 }
299 if flex {
300 let tagged = WriteTaggedFields::new();
301 tagged.write(buf, &self.unknown_tagged_fields);
302 }
303 Ok(())
304 }
305 fn encoded_len(&self, version: i16) -> usize {
306 let flex = version >= 2;
307 let mut n: usize = 0;
308 if version >= 0 {
309 n += 4;
310 }
311 if version >= 0 {
312 n += 2;
313 }
314 if version >= 0 {
315 n += if flex {
316 compact_nullable_string_len(self.error_message)
317 } else {
318 nullable_string_len(self.error_message)
319 };
320 }
321 if flex {
322 let known_pairs: Vec<(u32, usize)> = Vec::new();
323 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
324 }
325 n
326 }
327}
328impl<'de> DecodeBorrow<'de> for PartitionResult<'de> {
329 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
330 let flex = version >= 2;
331 let mut out = Self::default();
332 if version >= 0 {
333 out.partition_id = get_i32(buf)?;
334 }
335 if version >= 0 {
336 out.error_code = get_i16(buf)?;
337 }
338 if version >= 0 {
339 out.error_message = if flex {
340 get_compact_nullable_string_borrowed(buf)?
341 } else {
342 get_nullable_string_borrowed(buf)?
343 };
344 }
345 if flex {
346 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
347 }
348 Ok(out)
349 }
350}
351#[cfg(test)]
352impl PartitionResult<'_> {
353 #[must_use]
354 pub fn populated(version: i16) -> Self {
355 let mut m = Self::default();
356 if version >= 0 {
357 m.partition_id = 1i32;
358 }
359 if version >= 0 {
360 m.error_code = 1i16;
361 }
362 if version >= 0 {
363 m.error_message = Some("x");
364 }
365 m
366 }
367}