Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
LeaderChangeMessage.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_i16, get_i32, put_i16, put_i32};
6use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
7use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
8pub const MIN_VERSION: i16 = 0;
9pub const MAX_VERSION: i16 = 1;
10pub const FLEXIBLE_MIN: i16 = 0;
11
12#[inline]
13fn is_flexible(version: i16) -> bool {
14    version >= FLEXIBLE_MIN
15}
16
17#[derive(Debug, Clone, PartialEq, Eq, Default)]
18pub struct LeaderChangeMessage {
19    pub version: i16,
20    pub leader_id: i32,
21    pub voters: Vec<super::common::leader_change_message::voter::Voter>,
22    pub granting_voters: Vec<super::common::leader_change_message::voter::Voter>,
23    pub unknown_tagged_fields: UnknownTaggedFields,
24}
25impl Encode for LeaderChangeMessage {
26    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
27        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
28            return Err(ProtocolError::SchemaMismatch(
29                "LeaderChangeMessage version out of range",
30            ));
31        }
32        let flex = is_flexible(version);
33        if version >= 0 {
34            put_i16(buf, self.version);
35        }
36        if version >= 0 {
37            put_i32(buf, self.leader_id);
38        }
39        if version >= 0 {
40            {
41                crate::primitives::array::put_array_len(buf, (self.voters).len(), flex);
42                for it in &self.voters {
43                    it.encode(buf, version)?;
44                }
45            }
46        }
47        if version >= 0 {
48            {
49                crate::primitives::array::put_array_len(buf, (self.granting_voters).len(), flex);
50                for it in &self.granting_voters {
51                    it.encode(buf, version)?;
52                }
53            }
54        }
55        if flex {
56            let tagged = WriteTaggedFields::new();
57            tagged.write(buf, &self.unknown_tagged_fields);
58        }
59        Ok(())
60    }
61    fn encoded_len(&self, version: i16) -> usize {
62        let flex = is_flexible(version);
63        let mut n: usize = 0;
64        if version >= 0 {
65            n += 2;
66        }
67        if version >= 0 {
68            n += 4;
69        }
70        if version >= 0 {
71            n += {
72                let prefix =
73                    crate::primitives::array::array_len_prefix_len((self.voters).len(), flex);
74                let body: usize = (self.voters).iter().map(|it| it.encoded_len(version)).sum();
75                prefix + body
76            };
77        }
78        if version >= 0 {
79            n += {
80                let prefix = crate::primitives::array::array_len_prefix_len(
81                    (self.granting_voters).len(),
82                    flex,
83                );
84                let body: usize = (self.granting_voters)
85                    .iter()
86                    .map(|it| it.encoded_len(version))
87                    .sum();
88                prefix + body
89            };
90        }
91        if flex {
92            let known_pairs: Vec<(u32, usize)> = Vec::new();
93            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
94        }
95        n
96    }
97}
98impl Decode<'_> for LeaderChangeMessage {
99    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
100        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
101            return Err(ProtocolError::SchemaMismatch(
102                "LeaderChangeMessage version out of range",
103            ));
104        }
105        let flex = is_flexible(version);
106        let mut out = Self::default();
107        if version >= 0 {
108            out.version = get_i16(buf)?;
109        }
110        if version >= 0 {
111            out.leader_id = get_i32(buf)?;
112        }
113        if version >= 0 {
114            out.voters = {
115                let n = crate::primitives::array::get_array_len(buf, flex)?;
116                let mut v = Vec::with_capacity(n);
117                for _ in 0..n {
118                    v.push(super::common::leader_change_message::voter::Voter::decode(
119                        buf, version,
120                    )?);
121                }
122                v
123            };
124        }
125        if version >= 0 {
126            out.granting_voters = {
127                let n = crate::primitives::array::get_array_len(buf, flex)?;
128                let mut v = Vec::with_capacity(n);
129                for _ in 0..n {
130                    v.push(super::common::leader_change_message::voter::Voter::decode(
131                        buf, version,
132                    )?);
133                }
134                v
135            };
136        }
137        if flex {
138            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
139        }
140        Ok(out)
141    }
142}
143#[cfg(test)]
144impl LeaderChangeMessage {
145    #[must_use]
146    pub fn populated(version: i16) -> Self {
147        let mut m = Self::default();
148        if version >= 0 {
149            m.version = 1i16;
150        }
151        if version >= 0 {
152            m.leader_id = 1i32;
153        }
154        if version >= 0 {
155            m.voters = vec![super::common::leader_change_message::voter::Voter::populated(version)];
156        }
157        if version >= 0 {
158            m.granting_voters =
159                vec![super::common::leader_change_message::voter::Voter::populated(version)];
160        }
161        m
162    }
163}
164
165/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
166/// Only includes fields valid for the given version.
167#[must_use]
168#[allow(unused_comparisons)]
169pub fn default_json(version: i16) -> ::serde_json::Value {
170    let mut obj = ::serde_json::Map::new();
171    obj.insert("version".to_string(), ::serde_json::json!(0));
172    obj.insert("leaderId".to_string(), ::serde_json::json!(0));
173    obj.insert("voters".to_string(), ::serde_json::Value::Array(vec![]));
174    obj.insert(
175        "grantingVoters".to_string(),
176        ::serde_json::Value::Array(vec![]),
177    );
178    ::serde_json::Value::Object(obj)
179}