crabka_protocol/opt/rustwide/workdir/generated/
PartitionRecord.owned.rs1use crate::primitives::fixed::{get_i8, get_i32, put_i8, put_i32};
4use crate::tagged_fields::{
5 WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len,
6};
7use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
8use bytes::{Buf, BufMut};
9pub const MIN_VERSION: i16 = 0;
10pub const MAX_VERSION: i16 = 2;
11pub const FLEXIBLE_MIN: i16 = 0;
12#[inline]
13fn is_flexible(version: i16) -> bool {
14 version >= FLEXIBLE_MIN
15}
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct PartitionRecord {
18 pub partition_id: i32,
19 pub topic_id: crate::primitives::uuid::Uuid,
20 pub replicas: Vec<i32>,
21 pub isr: Vec<i32>,
22 pub removing_replicas: Vec<i32>,
23 pub adding_replicas: Vec<i32>,
24 pub leader: i32,
25 pub leader_epoch: i32,
26 pub partition_epoch: i32,
27 pub directories: Vec<crate::primitives::uuid::Uuid>,
28 pub leader_recovery_state: i8,
29 pub eligible_leader_replicas: Option<Vec<i32>>,
30 pub last_known_elr: Option<Vec<i32>>,
31 pub unknown_tagged_fields: UnknownTaggedFields,
32}
33impl Default for PartitionRecord {
34 fn default() -> Self {
35 Self {
36 partition_id: -1i32,
37 topic_id: Default::default(),
38 replicas: Vec::new(),
39 isr: Vec::new(),
40 removing_replicas: Vec::new(),
41 adding_replicas: Vec::new(),
42 leader: -1i32,
43 leader_epoch: -1i32,
44 partition_epoch: -1i32,
45 directories: Vec::new(),
46 leader_recovery_state: 0i8,
47 eligible_leader_replicas: None,
48 last_known_elr: None,
49 unknown_tagged_fields: Default::default(),
50 }
51 }
52}
53impl Encode for PartitionRecord {
54 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
55 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
56 return Err(ProtocolError::SchemaMismatch(
57 "PartitionRecord version out of range",
58 ));
59 }
60 let flex = is_flexible(version);
61 if version >= 0 {
62 put_i32(buf, self.partition_id);
63 }
64 if version >= 0 {
65 crate::primitives::uuid::put_uuid(buf, self.topic_id);
66 }
67 if version >= 0 {
68 {
69 crate::primitives::array::put_array_len(buf, (self.replicas).len(), flex);
70 for it in &self.replicas {
71 put_i32(buf, *it);
72 }
73 }
74 }
75 if version >= 0 {
76 {
77 crate::primitives::array::put_array_len(buf, (self.isr).len(), flex);
78 for it in &self.isr {
79 put_i32(buf, *it);
80 }
81 }
82 }
83 if version >= 0 {
84 {
85 crate::primitives::array::put_array_len(buf, (self.removing_replicas).len(), flex);
86 for it in &self.removing_replicas {
87 put_i32(buf, *it);
88 }
89 }
90 }
91 if version >= 0 {
92 {
93 crate::primitives::array::put_array_len(buf, (self.adding_replicas).len(), flex);
94 for it in &self.adding_replicas {
95 put_i32(buf, *it);
96 }
97 }
98 }
99 if version >= 0 {
100 put_i32(buf, self.leader);
101 }
102 if version >= 0 {
103 put_i32(buf, self.leader_epoch);
104 }
105 if version >= 0 {
106 put_i32(buf, self.partition_epoch);
107 }
108 if version >= 1 {
109 {
110 crate::primitives::array::put_array_len(buf, (self.directories).len(), flex);
111 for it in &self.directories {
112 crate::primitives::uuid::put_uuid(buf, *it);
113 }
114 }
115 }
116 if flex {
117 let mut tagged = WriteTaggedFields::new();
118 if !(self.leader_recovery_state == 0i8) {
119 let payload = encode_to_bytes(1, |b| {
120 put_i8(b, self.leader_recovery_state);
121 Ok(())
122 });
123 tagged.add(0, payload);
124 }
125 if !(self.eligible_leader_replicas.is_none()) {
126 let payload = encode_to_bytes(
127 {
128 let opt: Option<&Vec<_>> = (self.eligible_leader_replicas).as_ref();
129 let prefix = crate::primitives::array::nullable_array_len_prefix_len(
130 opt.map(std::vec::Vec::len),
131 flex,
132 );
133 let body: usize = opt.map_or(0, |v| v.iter().map(|_| 4).sum());
134 prefix + body
135 },
136 |b| {
137 {
138 let len = (self.eligible_leader_replicas).as_ref().map(Vec::len);
139 crate::primitives::array::put_nullable_array_len(b, len, flex);
140 if let Some(v) = &self.eligible_leader_replicas {
141 for it in v {
142 put_i32(b, *it);
143 }
144 }
145 };
146 Ok(())
147 },
148 );
149 tagged.add(1, payload);
150 }
151 if !(self.last_known_elr.is_none()) {
152 let payload = encode_to_bytes(
153 {
154 let opt: Option<&Vec<_>> = (self.last_known_elr).as_ref();
155 let prefix = crate::primitives::array::nullable_array_len_prefix_len(
156 opt.map(std::vec::Vec::len),
157 flex,
158 );
159 let body: usize = opt.map_or(0, |v| v.iter().map(|_| 4).sum());
160 prefix + body
161 },
162 |b| {
163 {
164 let len = (self.last_known_elr).as_ref().map(Vec::len);
165 crate::primitives::array::put_nullable_array_len(b, len, flex);
166 if let Some(v) = &self.last_known_elr {
167 for it in v {
168 put_i32(b, *it);
169 }
170 }
171 };
172 Ok(())
173 },
174 );
175 tagged.add(2, payload);
176 }
177 tagged.write(buf, &self.unknown_tagged_fields);
178 }
179 Ok(())
180 }
181 fn encoded_len(&self, version: i16) -> usize {
182 let flex = is_flexible(version);
183 let mut n: usize = 0;
184 if version >= 0 {
185 n += 4;
186 }
187 if version >= 0 {
188 n += 16;
189 }
190 if version >= 0 {
191 n += {
192 let prefix =
193 crate::primitives::array::array_len_prefix_len((self.replicas).len(), flex);
194 let body: usize = (self.replicas).iter().map(|_| 4).sum();
195 prefix + body
196 };
197 }
198 if version >= 0 {
199 n += {
200 let prefix = crate::primitives::array::array_len_prefix_len((self.isr).len(), flex);
201 let body: usize = (self.isr).iter().map(|_| 4).sum();
202 prefix + body
203 };
204 }
205 if version >= 0 {
206 n += {
207 let prefix = crate::primitives::array::array_len_prefix_len(
208 (self.removing_replicas).len(),
209 flex,
210 );
211 let body: usize = (self.removing_replicas).iter().map(|_| 4).sum();
212 prefix + body
213 };
214 }
215 if version >= 0 {
216 n += {
217 let prefix = crate::primitives::array::array_len_prefix_len(
218 (self.adding_replicas).len(),
219 flex,
220 );
221 let body: usize = (self.adding_replicas).iter().map(|_| 4).sum();
222 prefix + body
223 };
224 }
225 if version >= 0 {
226 n += 4;
227 }
228 if version >= 0 {
229 n += 4;
230 }
231 if version >= 0 {
232 n += 4;
233 }
234 if version >= 1 {
235 n += {
236 let prefix =
237 crate::primitives::array::array_len_prefix_len((self.directories).len(), flex);
238 let body: usize = (self.directories).iter().map(|_| 16).sum();
239 prefix + body
240 };
241 }
242 if flex {
243 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
244 if !(self.leader_recovery_state == 0i8) {
245 known_pairs.push((0, 1));
246 }
247 if !(self.eligible_leader_replicas.is_none()) {
248 known_pairs.push((1, {
249 let opt: Option<&Vec<_>> = (self.eligible_leader_replicas).as_ref();
250 let prefix = crate::primitives::array::nullable_array_len_prefix_len(
251 opt.map(std::vec::Vec::len),
252 flex,
253 );
254 let body: usize = opt.map_or(0, |v| v.iter().map(|_| 4).sum());
255 prefix + body
256 }));
257 }
258 if !(self.last_known_elr.is_none()) {
259 known_pairs.push((2, {
260 let opt: Option<&Vec<_>> = (self.last_known_elr).as_ref();
261 let prefix = crate::primitives::array::nullable_array_len_prefix_len(
262 opt.map(std::vec::Vec::len),
263 flex,
264 );
265 let body: usize = opt.map_or(0, |v| v.iter().map(|_| 4).sum());
266 prefix + body
267 }));
268 }
269 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
270 }
271 n
272 }
273}
274impl Decode<'_> for PartitionRecord {
275 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
276 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
277 return Err(ProtocolError::SchemaMismatch(
278 "PartitionRecord version out of range",
279 ));
280 }
281 let flex = is_flexible(version);
282 let mut out = Self::default();
283 if version >= 0 {
284 out.partition_id = get_i32(buf)?;
285 }
286 if version >= 0 {
287 out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
288 }
289 if version >= 0 {
290 out.replicas = {
291 let n = crate::primitives::array::get_array_len(buf, flex)?;
292 let mut v = Vec::with_capacity(n);
293 for _ in 0..n {
294 v.push(get_i32(buf)?);
295 }
296 v
297 };
298 }
299 if version >= 0 {
300 out.isr = {
301 let n = crate::primitives::array::get_array_len(buf, flex)?;
302 let mut v = Vec::with_capacity(n);
303 for _ in 0..n {
304 v.push(get_i32(buf)?);
305 }
306 v
307 };
308 }
309 if version >= 0 {
310 out.removing_replicas = {
311 let n = crate::primitives::array::get_array_len(buf, flex)?;
312 let mut v = Vec::with_capacity(n);
313 for _ in 0..n {
314 v.push(get_i32(buf)?);
315 }
316 v
317 };
318 }
319 if version >= 0 {
320 out.adding_replicas = {
321 let n = crate::primitives::array::get_array_len(buf, flex)?;
322 let mut v = Vec::with_capacity(n);
323 for _ in 0..n {
324 v.push(get_i32(buf)?);
325 }
326 v
327 };
328 }
329 if version >= 0 {
330 out.leader = get_i32(buf)?;
331 }
332 if version >= 0 {
333 out.leader_epoch = get_i32(buf)?;
334 }
335 if version >= 0 {
336 out.partition_epoch = get_i32(buf)?;
337 }
338 if version >= 1 {
339 out.directories = {
340 let n = crate::primitives::array::get_array_len(buf, flex)?;
341 let mut v = Vec::with_capacity(n);
342 for _ in 0..n {
343 v.push(crate::primitives::uuid::get_uuid(buf)?);
344 }
345 v
346 };
347 }
348 if flex {
349 let mut tag_leader_recovery_state = None;
350 let mut tag_eligible_leader_replicas = None;
351 let mut tag_last_known_elr = None;
352 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
353 0 => {
354 tag_leader_recovery_state = Some({
355 let b: &mut &[u8] = payload;
356 get_i8(b)?
357 });
358 Ok(true)
359 }
360 1 => {
361 tag_eligible_leader_replicas = Some({
362 let b: &mut &[u8] = payload;
363 {
364 let opt = crate::primitives::array::get_nullable_array_len(b, flex)?;
365 match opt {
366 None => None,
367 Some(n) => {
368 let mut v = Vec::with_capacity(n);
369 for _ in 0..n {
370 v.push(get_i32(b)?);
371 }
372 Some(v)
373 }
374 }
375 }
376 });
377 Ok(true)
378 }
379 2 => {
380 tag_last_known_elr = Some({
381 let b: &mut &[u8] = payload;
382 {
383 let opt = crate::primitives::array::get_nullable_array_len(b, flex)?;
384 match opt {
385 None => None,
386 Some(n) => {
387 let mut v = Vec::with_capacity(n);
388 for _ in 0..n {
389 v.push(get_i32(b)?);
390 }
391 Some(v)
392 }
393 }
394 }
395 });
396 Ok(true)
397 }
398 _ => Ok(false),
399 })?;
400 if let Some(v) = tag_leader_recovery_state {
401 out.leader_recovery_state = v;
402 }
403 if let Some(v) = tag_eligible_leader_replicas {
404 out.eligible_leader_replicas = v;
405 }
406 if let Some(v) = tag_last_known_elr {
407 out.last_known_elr = v;
408 }
409 }
410 Ok(out)
411 }
412}
413#[cfg(test)]
414impl PartitionRecord {
415 #[must_use]
416 pub fn populated(version: i16) -> Self {
417 let mut m = Self::default();
418 if version >= 0 {
419 m.partition_id = 1i32;
420 }
421 if version >= 0 {
422 m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
423 }
424 if version >= 0 {
425 m.replicas = vec![1i32];
426 }
427 if version >= 0 {
428 m.isr = vec![1i32];
429 }
430 if version >= 0 {
431 m.removing_replicas = vec![1i32];
432 }
433 if version >= 0 {
434 m.adding_replicas = vec![1i32];
435 }
436 if version >= 0 {
437 m.leader = 1i32;
438 }
439 if version >= 0 {
440 m.leader_epoch = 1i32;
441 }
442 if version >= 0 {
443 m.partition_epoch = 1i32;
444 }
445 if version >= 1 {
446 m.directories = vec![crate::primitives::uuid::Uuid([1u8; 16])];
447 }
448 if version >= 0 {
449 m.leader_recovery_state = 1i8;
450 }
451 if version >= 2 {
452 m.eligible_leader_replicas = Some(vec![1i32]);
453 }
454 if version >= 2 {
455 m.last_known_elr = Some(vec![1i32]);
456 }
457 m
458 }
459}
460#[must_use]
463#[allow(unused_comparisons)]
464pub fn default_json(version: i16) -> ::serde_json::Value {
465 let mut obj = ::serde_json::Map::new();
466 obj.insert("partitionId".to_string(), ::serde_json::json!(-1));
467 obj.insert(
468 "topicId".to_string(),
469 ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
470 );
471 obj.insert("replicas".to_string(), ::serde_json::Value::Array(vec![]));
472 obj.insert("isr".to_string(), ::serde_json::Value::Array(vec![]));
473 obj.insert(
474 "removingReplicas".to_string(),
475 ::serde_json::Value::Array(vec![]),
476 );
477 obj.insert(
478 "addingReplicas".to_string(),
479 ::serde_json::Value::Array(vec![]),
480 );
481 obj.insert("leader".to_string(), ::serde_json::json!(-1));
482 obj.insert("leaderRecoveryState".to_string(), ::serde_json::json!(0));
483 obj.insert("leaderEpoch".to_string(), ::serde_json::json!(-1));
484 obj.insert("partitionEpoch".to_string(), ::serde_json::json!(-1));
485 if version >= 1 {
486 obj.insert(
487 "directories".to_string(),
488 ::serde_json::Value::Array(vec![]),
489 );
490 }
491 if version >= 2 {
492 obj.insert(
493 "eligibleLeaderReplicas".to_string(),
494 ::serde_json::Value::Null,
495 );
496 }
497 if version >= 2 {
498 obj.insert("lastKnownElr".to_string(), ::serde_json::Value::Null);
499 }
500 ::serde_json::Value::Object(obj)
501}