Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
BrokerRegistrationChangeRecord.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_i8, get_i32, get_i64, put_i8, put_i32, put_i64},
8    tagged_fields::{WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len},
9};
10pub const MIN_VERSION: i16 = 0;
11pub const MAX_VERSION: i16 = 3;
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 BrokerRegistrationChangeRecord {
20    pub broker_id: i32,
21    pub broker_epoch: i64,
22    pub fenced: i8,
23    pub in_controlled_shutdown: i8,
24    pub log_dirs: Vec<crate::primitives::uuid::Uuid>,
25    pub cordoned_log_dirs: Option<Vec<crate::primitives::uuid::Uuid>>,
26    pub unknown_tagged_fields: UnknownTaggedFields,
27}
28impl Encode for BrokerRegistrationChangeRecord {
29    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
30        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
31            return Err(ProtocolError::SchemaMismatch(
32                "BrokerRegistrationChangeRecord version out of range",
33            ));
34        }
35        let flex = is_flexible(version);
36        if version >= 0 {
37            put_i32(buf, self.broker_id);
38        }
39        if version >= 0 {
40            put_i64(buf, self.broker_epoch);
41        }
42        if flex {
43            let mut tagged = WriteTaggedFields::new();
44            if !(crate::codegen_helpers::is_default(&self.fenced)) {
45                let payload = encode_to_bytes(1, |b| {
46                    put_i8(b, self.fenced);
47                    Ok(())
48                });
49                tagged.add(0, payload);
50            }
51            if version >= 1 && !(crate::codegen_helpers::is_default(&self.in_controlled_shutdown)) {
52                let payload = encode_to_bytes(1, |b| {
53                    put_i8(b, self.in_controlled_shutdown);
54                    Ok(())
55                });
56                tagged.add(1, payload);
57            }
58            if version >= 2 && !(crate::codegen_helpers::is_default(&self.log_dirs)) {
59                let payload = encode_to_bytes(
60                    {
61                        let prefix = crate::primitives::array::array_len_prefix_len(
62                            (self.log_dirs).len(),
63                            flex,
64                        );
65                        let body: usize = (self.log_dirs).iter().map(|_| 16).sum();
66                        prefix + body
67                    },
68                    |b| {
69                        {
70                            crate::primitives::array::put_array_len(b, (self.log_dirs).len(), flex);
71                            for it in &self.log_dirs {
72                                crate::primitives::uuid::put_uuid(b, *it);
73                            }
74                        };
75                        Ok(())
76                    },
77                );
78                tagged.add(2, payload);
79            }
80            if version >= 3 && self.cordoned_log_dirs.is_some() {
81                let payload = encode_to_bytes(
82                    {
83                        let opt: Option<&Vec<_>> = (self.cordoned_log_dirs).as_ref();
84                        let prefix = crate::primitives::array::nullable_array_len_prefix_len(
85                            opt.map(std::vec::Vec::len),
86                            flex,
87                        );
88                        let body: usize = opt.map_or(0, |v| v.iter().map(|_| 16).sum());
89                        prefix + body
90                    },
91                    |b| {
92                        {
93                            let len = (self.cordoned_log_dirs).as_ref().map(Vec::len);
94                            crate::primitives::array::put_nullable_array_len(b, len, flex);
95                            if let Some(v) = &self.cordoned_log_dirs {
96                                for it in v {
97                                    crate::primitives::uuid::put_uuid(b, *it);
98                                }
99                            }
100                        };
101                        Ok(())
102                    },
103                );
104                tagged.add(3, payload);
105            }
106            tagged.write(buf, &self.unknown_tagged_fields);
107        }
108        Ok(())
109    }
110    fn encoded_len(&self, version: i16) -> usize {
111        let flex = is_flexible(version);
112        let mut n: usize = 0;
113        if version >= 0 {
114            n += 4;
115        }
116        if version >= 0 {
117            n += 8;
118        }
119        if flex {
120            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
121            if !(crate::codegen_helpers::is_default(&self.fenced)) {
122                known_pairs.push((0, 1));
123            }
124            if version >= 1 && !(crate::codegen_helpers::is_default(&self.in_controlled_shutdown)) {
125                known_pairs.push((1, 1));
126            }
127            if version >= 2 && !(crate::codegen_helpers::is_default(&self.log_dirs)) {
128                known_pairs.push((2, {
129                    let prefix =
130                        crate::primitives::array::array_len_prefix_len((self.log_dirs).len(), flex);
131                    let body: usize = (self.log_dirs).iter().map(|_| 16).sum();
132                    prefix + body
133                }));
134            }
135            if version >= 3 && self.cordoned_log_dirs.is_some() {
136                known_pairs.push((3, {
137                    let opt: Option<&Vec<_>> = (self.cordoned_log_dirs).as_ref();
138                    let prefix = crate::primitives::array::nullable_array_len_prefix_len(
139                        opt.map(std::vec::Vec::len),
140                        flex,
141                    );
142                    let body: usize = opt.map_or(0, |v| v.iter().map(|_| 16).sum());
143                    prefix + body
144                }));
145            }
146            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
147        }
148        n
149    }
150}
151impl Decode<'_> for BrokerRegistrationChangeRecord {
152    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
153        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
154            return Err(ProtocolError::SchemaMismatch(
155                "BrokerRegistrationChangeRecord version out of range",
156            ));
157        }
158        let flex = is_flexible(version);
159        let mut out = Self::default();
160        if version >= 0 {
161            out.broker_id = get_i32(buf)?;
162        }
163        if version >= 0 {
164            out.broker_epoch = get_i64(buf)?;
165        }
166        if flex {
167            let mut tag_fenced = None;
168            let mut tag_in_controlled_shutdown = None;
169            let mut tag_log_dirs = None;
170            let mut tag_cordoned_log_dirs = None;
171            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
172                0 => {
173                    tag_fenced = Some({
174                        let b: &mut &[u8] = payload;
175                        get_i8(b)?
176                    });
177                    Ok(true)
178                }
179                1 if version >= 1 => {
180                    tag_in_controlled_shutdown = Some({
181                        let b: &mut &[u8] = payload;
182                        get_i8(b)?
183                    });
184                    Ok(true)
185                }
186                1 => Err(ProtocolError::TagNotValidForVersion { tag: 1, version }),
187                2 if version >= 2 => {
188                    tag_log_dirs = Some({
189                        let b: &mut &[u8] = payload;
190                        {
191                            let n = crate::primitives::array::get_array_len(b, flex)?;
192                            let mut v = Vec::with_capacity(n);
193                            for _ in 0..n {
194                                v.push(crate::primitives::uuid::get_uuid(b)?);
195                            }
196                            v
197                        }
198                    });
199                    Ok(true)
200                }
201                2 => Err(ProtocolError::TagNotValidForVersion { tag: 2, version }),
202                3 if version >= 3 => {
203                    tag_cordoned_log_dirs = Some({
204                        let b: &mut &[u8] = payload;
205                        {
206                            let opt = crate::primitives::array::get_nullable_array_len(b, flex)?;
207                            match opt {
208                                None => None,
209                                Some(n) => {
210                                    let mut v = Vec::with_capacity(n);
211                                    for _ in 0..n {
212                                        v.push(crate::primitives::uuid::get_uuid(b)?);
213                                    }
214                                    Some(v)
215                                }
216                            }
217                        }
218                    });
219                    Ok(true)
220                }
221                3 => Err(ProtocolError::TagNotValidForVersion { tag: 3, version }),
222                _ => Ok(false),
223            })?;
224            if let Some(v) = tag_fenced {
225                out.fenced = v;
226            }
227            if let Some(v) = tag_in_controlled_shutdown {
228                out.in_controlled_shutdown = v;
229            }
230            if let Some(v) = tag_log_dirs {
231                out.log_dirs = v;
232            }
233            if let Some(v) = tag_cordoned_log_dirs {
234                out.cordoned_log_dirs = v;
235            }
236        }
237        Ok(out)
238    }
239}
240#[cfg(test)]
241impl BrokerRegistrationChangeRecord {
242    #[must_use]
243    pub fn populated(version: i16) -> Self {
244        let mut m = Self::default();
245        if version >= 0 {
246            m.broker_id = 1i32;
247        }
248        if version >= 0 {
249            m.broker_epoch = 1i64;
250        }
251        if version >= 0 {
252            m.fenced = 1i8;
253        }
254        if version >= 1 {
255            m.in_controlled_shutdown = 1i8;
256        }
257        if version >= 2 {
258            m.log_dirs = vec![crate::primitives::uuid::Uuid([1u8; 16])];
259        }
260        if version >= 3 {
261            m.cordoned_log_dirs = Some(vec![crate::primitives::uuid::Uuid([1u8; 16])]);
262        }
263        m
264    }
265}
266/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
267/// Only includes fields valid for the given version.
268#[must_use]
269#[allow(unused_comparisons)]
270pub fn default_json(version: i16) -> ::serde_json::Value {
271    let mut obj = ::serde_json::Map::new();
272    obj.insert("brokerId".to_string(), ::serde_json::json!(0));
273    obj.insert("brokerEpoch".to_string(), ::serde_json::json!(0));
274    obj.insert("fenced".to_string(), ::serde_json::json!(0));
275    if version >= 1 {
276        obj.insert("inControlledShutdown".to_string(), ::serde_json::json!(0));
277    }
278    if version >= 2 {
279        obj.insert("logDirs".to_string(), ::serde_json::Value::Array(vec![]));
280    }
281    if version >= 3 {
282        obj.insert("cordonedLogDirs".to_string(), ::serde_json::Value::Null);
283    }
284    ::serde_json::Value::Object(obj)
285}
286/// A message with a non-default value in every tagged field, at every depth, for
287/// the JVM oracle differential sweep. The value is the same at every version.
288#[must_use]
289pub fn tagged_fixture() -> BrokerRegistrationChangeRecord {
290    BrokerRegistrationChangeRecord {
291        fenced: 1i8,
292        in_controlled_shutdown: 1i8,
293        log_dirs: vec![crate::primitives::uuid::Uuid([1u8; 16])],
294        cordoned_log_dirs: Some(vec![crate::primitives::uuid::Uuid([1u8; 16])]),
295        ..BrokerRegistrationChangeRecord::default()
296    }
297}
298/// The JSON form of `tagged_fixture()` that Kafka's JSON converter reads at
299/// `version`. It holds only the fields that the schema declares at that version.
300#[must_use]
301pub fn tagged_fixture_json(version: i16) -> ::serde_json::Value {
302    let mut m = ::serde_json::Map::new();
303    m.insert("brokerId".to_string(), ::serde_json::json!(0));
304    m.insert("brokerEpoch".to_string(), ::serde_json::json!(0));
305    m.insert("fenced".to_string(), ::serde_json::json!(1));
306    if version >= 1 {
307        m.insert("inControlledShutdown".to_string(), ::serde_json::json!(1));
308    }
309    if version >= 2 {
310        m.insert(
311            "logDirs".to_string(),
312            ::serde_json::Value::Array(vec![::serde_json::Value::String(
313                "AQEBAQEBAQEBAQEBAQEBAQ".to_string(),
314            )]),
315        );
316    }
317    if version == 3 {
318        m.insert(
319            "cordonedLogDirs".to_string(),
320            ::serde_json::Value::Array(vec![::serde_json::Value::String(
321                "AQEBAQEBAQEBAQEBAQEBAQ".to_string(),
322            )]),
323        );
324    }
325    ::serde_json::Value::Object(m)
326}