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