Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
PartitionRecord.owned.rs

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