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