Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
LeaderChangeMessage.owned.rs

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