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