Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use crate::primitives::fixed::{get_i8, get_i32, get_i64, put_i8, put_i32, put_i64};
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 = 3;
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, Default)]
17pub struct BrokerRegistrationChangeRecord {
18    pub broker_id: i32,
19    pub broker_epoch: i64,
20    pub fenced: i8,
21    pub in_controlled_shutdown: i8,
22    pub log_dirs: Vec<crate::primitives::uuid::Uuid>,
23    pub cordoned_log_dirs: Option<Vec<crate::primitives::uuid::Uuid>>,
24    pub unknown_tagged_fields: UnknownTaggedFields,
25}
26impl Encode for BrokerRegistrationChangeRecord {
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                "BrokerRegistrationChangeRecord version out of range",
31            ));
32        }
33        let flex = is_flexible(version);
34        if version >= 0 {
35            put_i32(buf, self.broker_id);
36        }
37        if version >= 0 {
38            put_i64(buf, self.broker_epoch);
39        }
40        if flex {
41            let mut tagged = WriteTaggedFields::new();
42            if !(crate::codegen_helpers::is_default(&self.fenced)) {
43                let payload = encode_to_bytes(1, |b| {
44                    put_i8(b, self.fenced);
45                    Ok(())
46                });
47                tagged.add(0, payload);
48            }
49            if !(crate::codegen_helpers::is_default(&self.in_controlled_shutdown)) {
50                let payload = encode_to_bytes(1, |b| {
51                    put_i8(b, self.in_controlled_shutdown);
52                    Ok(())
53                });
54                tagged.add(1, payload);
55            }
56            if !(crate::codegen_helpers::is_default(&self.log_dirs)) {
57                let payload = encode_to_bytes(
58                    {
59                        let prefix = crate::primitives::array::array_len_prefix_len(
60                            (self.log_dirs).len(),
61                            flex,
62                        );
63                        let body: usize = (self.log_dirs).iter().map(|_| 16).sum();
64                        prefix + body
65                    },
66                    |b| {
67                        {
68                            crate::primitives::array::put_array_len(b, (self.log_dirs).len(), flex);
69                            for it in &self.log_dirs {
70                                crate::primitives::uuid::put_uuid(b, *it);
71                            }
72                        };
73                        Ok(())
74                    },
75                );
76                tagged.add(2, payload);
77            }
78            if !(self.cordoned_log_dirs.is_none()) {
79                let payload = encode_to_bytes(
80                    {
81                        let opt: Option<&Vec<_>> = (self.cordoned_log_dirs).as_ref();
82                        let prefix = crate::primitives::array::nullable_array_len_prefix_len(
83                            opt.map(std::vec::Vec::len),
84                            flex,
85                        );
86                        let body: usize = opt.map_or(0, |v| v.iter().map(|_| 16).sum());
87                        prefix + body
88                    },
89                    |b| {
90                        {
91                            let len = (self.cordoned_log_dirs).as_ref().map(Vec::len);
92                            crate::primitives::array::put_nullable_array_len(b, len, flex);
93                            if let Some(v) = &self.cordoned_log_dirs {
94                                for it in v {
95                                    crate::primitives::uuid::put_uuid(b, *it);
96                                }
97                            }
98                        };
99                        Ok(())
100                    },
101                );
102                tagged.add(3, payload);
103            }
104            tagged.write(buf, &self.unknown_tagged_fields);
105        }
106        Ok(())
107    }
108    fn encoded_len(&self, version: i16) -> usize {
109        let flex = is_flexible(version);
110        let mut n: usize = 0;
111        if version >= 0 {
112            n += 4;
113        }
114        if version >= 0 {
115            n += 8;
116        }
117        if flex {
118            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
119            if !(crate::codegen_helpers::is_default(&self.fenced)) {
120                known_pairs.push((0, 1));
121            }
122            if !(crate::codegen_helpers::is_default(&self.in_controlled_shutdown)) {
123                known_pairs.push((1, 1));
124            }
125            if !(crate::codegen_helpers::is_default(&self.log_dirs)) {
126                known_pairs.push((2, {
127                    let prefix =
128                        crate::primitives::array::array_len_prefix_len((self.log_dirs).len(), flex);
129                    let body: usize = (self.log_dirs).iter().map(|_| 16).sum();
130                    prefix + body
131                }));
132            }
133            if !(self.cordoned_log_dirs.is_none()) {
134                known_pairs.push((3, {
135                    let opt: Option<&Vec<_>> = (self.cordoned_log_dirs).as_ref();
136                    let prefix = crate::primitives::array::nullable_array_len_prefix_len(
137                        opt.map(std::vec::Vec::len),
138                        flex,
139                    );
140                    let body: usize = opt.map_or(0, |v| v.iter().map(|_| 16).sum());
141                    prefix + body
142                }));
143            }
144            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
145        }
146        n
147    }
148}
149impl Decode<'_> for BrokerRegistrationChangeRecord {
150    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
151        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
152            return Err(ProtocolError::SchemaMismatch(
153                "BrokerRegistrationChangeRecord version out of range",
154            ));
155        }
156        let flex = is_flexible(version);
157        let mut out = Self::default();
158        if version >= 0 {
159            out.broker_id = get_i32(buf)?;
160        }
161        if version >= 0 {
162            out.broker_epoch = get_i64(buf)?;
163        }
164        if flex {
165            let mut tag_fenced = None;
166            let mut tag_in_controlled_shutdown = None;
167            let mut tag_log_dirs = None;
168            let mut tag_cordoned_log_dirs = None;
169            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
170                0 => {
171                    tag_fenced = Some({
172                        let b: &mut &[u8] = payload;
173                        get_i8(b)?
174                    });
175                    Ok(true)
176                }
177                1 => {
178                    tag_in_controlled_shutdown = Some({
179                        let b: &mut &[u8] = payload;
180                        get_i8(b)?
181                    });
182                    Ok(true)
183                }
184                2 => {
185                    tag_log_dirs = Some({
186                        let b: &mut &[u8] = payload;
187                        {
188                            let n = crate::primitives::array::get_array_len(b, flex)?;
189                            let mut v = Vec::with_capacity(n);
190                            for _ in 0..n {
191                                v.push(crate::primitives::uuid::get_uuid(b)?);
192                            }
193                            v
194                        }
195                    });
196                    Ok(true)
197                }
198                3 => {
199                    tag_cordoned_log_dirs = Some({
200                        let b: &mut &[u8] = payload;
201                        {
202                            let opt = crate::primitives::array::get_nullable_array_len(b, flex)?;
203                            match opt {
204                                None => None,
205                                Some(n) => {
206                                    let mut v = Vec::with_capacity(n);
207                                    for _ in 0..n {
208                                        v.push(crate::primitives::uuid::get_uuid(b)?);
209                                    }
210                                    Some(v)
211                                }
212                            }
213                        }
214                    });
215                    Ok(true)
216                }
217                _ => Ok(false),
218            })?;
219            if let Some(v) = tag_fenced {
220                out.fenced = v;
221            }
222            if let Some(v) = tag_in_controlled_shutdown {
223                out.in_controlled_shutdown = v;
224            }
225            if let Some(v) = tag_log_dirs {
226                out.log_dirs = v;
227            }
228            if let Some(v) = tag_cordoned_log_dirs {
229                out.cordoned_log_dirs = v;
230            }
231        }
232        Ok(out)
233    }
234}
235#[cfg(test)]
236impl BrokerRegistrationChangeRecord {
237    #[must_use]
238    pub fn populated(version: i16) -> Self {
239        let mut m = Self::default();
240        if version >= 0 {
241            m.broker_id = 1i32;
242        }
243        if version >= 0 {
244            m.broker_epoch = 1i64;
245        }
246        if version >= 0 {
247            m.fenced = 1i8;
248        }
249        if version >= 1 {
250            m.in_controlled_shutdown = 1i8;
251        }
252        if version >= 2 {
253            m.log_dirs = vec![crate::primitives::uuid::Uuid([1u8; 16])];
254        }
255        if version >= 3 {
256            m.cordoned_log_dirs = Some(vec![crate::primitives::uuid::Uuid([1u8; 16])]);
257        }
258        m
259    }
260}
261/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
262/// Only includes fields valid for the given version.
263#[must_use]
264#[allow(unused_comparisons)]
265pub fn default_json(version: i16) -> ::serde_json::Value {
266    let mut obj = ::serde_json::Map::new();
267    obj.insert("brokerId".to_string(), ::serde_json::json!(0));
268    obj.insert("brokerEpoch".to_string(), ::serde_json::json!(0));
269    obj.insert("fenced".to_string(), ::serde_json::json!(0));
270    if version >= 1 {
271        obj.insert("inControlledShutdown".to_string(), ::serde_json::json!(0));
272    }
273    if version >= 2 {
274        obj.insert("logDirs".to_string(), ::serde_json::Value::Array(vec![]));
275    }
276    if version >= 3 {
277        obj.insert("cordonedLogDirs".to_string(), ::serde_json::Value::Null);
278    }
279    ::serde_json::Value::Object(obj)
280}