Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{
6    get_bool, get_i16, get_i32, get_i64, get_u16, put_bool, put_i16, put_i32, put_i64, put_u16,
7};
8use crate::primitives::string_bytes::{
9    compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
10    get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
11    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
12};
13use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
14use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
15
16pub const API_KEY: i16 = 62;
17pub const MIN_VERSION: i16 = 0;
18pub const MAX_VERSION: i16 = 4;
19pub const FLEXIBLE_MIN: i16 = 0;
20
21#[inline]
22fn is_flexible(version: i16) -> bool {
23    version >= FLEXIBLE_MIN
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct BrokerRegistrationRequest {
28    pub broker_id: i32,
29    pub cluster_id: String,
30    pub incarnation_id: crate::primitives::uuid::Uuid,
31    pub listeners: Vec<Listener>,
32    pub features: Vec<Feature>,
33    pub rack: Option<String>,
34    pub is_migrating_zk_broker: bool,
35    pub log_dirs: Vec<crate::primitives::uuid::Uuid>,
36    pub previous_broker_epoch: i64,
37    pub unknown_tagged_fields: UnknownTaggedFields,
38}
39impl Default for BrokerRegistrationRequest {
40    fn default() -> Self {
41        Self {
42            broker_id: 0i32,
43            cluster_id: String::new(),
44            incarnation_id: Default::default(),
45            listeners: Vec::new(),
46            features: Vec::new(),
47            rack: None,
48            is_migrating_zk_broker: false,
49            log_dirs: Vec::new(),
50            previous_broker_epoch: -1i64,
51            unknown_tagged_fields: Default::default(),
52        }
53    }
54}
55impl Encode for BrokerRegistrationRequest {
56    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
57        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
58            return Err(ProtocolError::UnsupportedVersion {
59                api_key: API_KEY,
60                version,
61            });
62        }
63        let flex = is_flexible(version);
64        if version >= 0 {
65            put_i32(buf, self.broker_id);
66        }
67        if version >= 0 {
68            if flex {
69                put_compact_string(buf, &self.cluster_id);
70            } else {
71                put_string(buf, &self.cluster_id);
72            }
73        }
74        if version >= 0 {
75            crate::primitives::uuid::put_uuid(buf, self.incarnation_id);
76        }
77        if version >= 0 {
78            {
79                crate::primitives::array::put_array_len(buf, (self.listeners).len(), flex);
80                for it in &self.listeners {
81                    it.encode(buf, version)?;
82                }
83            }
84        }
85        if version >= 0 {
86            {
87                crate::primitives::array::put_array_len(buf, (self.features).len(), flex);
88                for it in &self.features {
89                    it.encode(buf, version)?;
90                }
91            }
92        }
93        if version >= 0 {
94            if flex {
95                put_compact_nullable_string(buf, self.rack.as_deref());
96            } else {
97                put_nullable_string(buf, self.rack.as_deref());
98            }
99        }
100        if version >= 1 {
101            put_bool(buf, self.is_migrating_zk_broker);
102        }
103        if version >= 2 {
104            {
105                crate::primitives::array::put_array_len(buf, (self.log_dirs).len(), flex);
106                for it in &self.log_dirs {
107                    crate::primitives::uuid::put_uuid(buf, *it);
108                }
109            }
110        }
111        if version >= 3 {
112            put_i64(buf, self.previous_broker_epoch);
113        }
114        if flex {
115            let tagged = WriteTaggedFields::new();
116            tagged.write(buf, &self.unknown_tagged_fields);
117        }
118        Ok(())
119    }
120    fn encoded_len(&self, version: i16) -> usize {
121        let flex = is_flexible(version);
122        let mut n: usize = 0;
123        if version >= 0 {
124            n += 4;
125        }
126        if version >= 0 {
127            n += if flex {
128                compact_string_len(&self.cluster_id)
129            } else {
130                string_len(&self.cluster_id)
131            };
132        }
133        if version >= 0 {
134            n += 16;
135        }
136        if version >= 0 {
137            n += {
138                let prefix =
139                    crate::primitives::array::array_len_prefix_len((self.listeners).len(), flex);
140                let body: usize = (self.listeners)
141                    .iter()
142                    .map(|it| it.encoded_len(version))
143                    .sum();
144                prefix + body
145            };
146        }
147        if version >= 0 {
148            n += {
149                let prefix =
150                    crate::primitives::array::array_len_prefix_len((self.features).len(), flex);
151                let body: usize = (self.features)
152                    .iter()
153                    .map(|it| it.encoded_len(version))
154                    .sum();
155                prefix + body
156            };
157        }
158        if version >= 0 {
159            n += if flex {
160                compact_nullable_string_len(self.rack.as_deref())
161            } else {
162                nullable_string_len(self.rack.as_deref())
163            };
164        }
165        if version >= 1 {
166            n += 1;
167        }
168        if version >= 2 {
169            n += {
170                let prefix =
171                    crate::primitives::array::array_len_prefix_len((self.log_dirs).len(), flex);
172                let body: usize = (self.log_dirs).iter().map(|_| 16).sum();
173                prefix + body
174            };
175        }
176        if version >= 3 {
177            n += 8;
178        }
179        if flex {
180            let known_pairs: Vec<(u32, usize)> = Vec::new();
181            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
182        }
183        n
184    }
185}
186impl Decode<'_> for BrokerRegistrationRequest {
187    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
188        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
189            return Err(ProtocolError::UnsupportedVersion {
190                api_key: API_KEY,
191                version,
192            });
193        }
194        let flex = is_flexible(version);
195        let mut out = Self::default();
196        if version >= 0 {
197            out.broker_id = get_i32(buf)?;
198        }
199        if version >= 0 {
200            out.cluster_id = if flex {
201                get_compact_string_owned(buf)?
202            } else {
203                get_string_owned(buf)?
204            };
205        }
206        if version >= 0 {
207            out.incarnation_id = crate::primitives::uuid::get_uuid(buf)?;
208        }
209        if version >= 0 {
210            out.listeners = {
211                let n = crate::primitives::array::get_array_len(buf, flex)?;
212                let mut v = Vec::with_capacity(n);
213                for _ in 0..n {
214                    v.push(Listener::decode(buf, version)?);
215                }
216                v
217            };
218        }
219        if version >= 0 {
220            out.features = {
221                let n = crate::primitives::array::get_array_len(buf, flex)?;
222                let mut v = Vec::with_capacity(n);
223                for _ in 0..n {
224                    v.push(Feature::decode(buf, version)?);
225                }
226                v
227            };
228        }
229        if version >= 0 {
230            out.rack = if flex {
231                get_compact_nullable_string_owned(buf)?
232            } else {
233                get_nullable_string_owned(buf)?
234            };
235        }
236        if version >= 1 {
237            out.is_migrating_zk_broker = get_bool(buf)?;
238        }
239        if version >= 2 {
240            out.log_dirs = {
241                let n = crate::primitives::array::get_array_len(buf, flex)?;
242                let mut v = Vec::with_capacity(n);
243                for _ in 0..n {
244                    v.push(crate::primitives::uuid::get_uuid(buf)?);
245                }
246                v
247            };
248        }
249        if version >= 3 {
250            out.previous_broker_epoch = get_i64(buf)?;
251        }
252        if flex {
253            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
254        }
255        Ok(out)
256    }
257}
258#[cfg(test)]
259impl BrokerRegistrationRequest {
260    #[must_use]
261    pub fn populated(version: i16) -> Self {
262        let mut m = Self::default();
263        if version >= 0 {
264            m.broker_id = 1i32;
265        }
266        if version >= 0 {
267            m.cluster_id = "x".to_string();
268        }
269        if version >= 0 {
270            m.incarnation_id = crate::primitives::uuid::Uuid([1u8; 16]);
271        }
272        if version >= 0 {
273            m.listeners = vec![Listener::populated(version)];
274        }
275        if version >= 0 {
276            m.features = vec![Feature::populated(version)];
277        }
278        if version >= 0 {
279            m.rack = Some("x".to_string());
280        }
281        if version >= 1 {
282            m.is_migrating_zk_broker = true;
283        }
284        if version >= 2 {
285            m.log_dirs = vec![crate::primitives::uuid::Uuid([1u8; 16])];
286        }
287        if version >= 3 {
288            m.previous_broker_epoch = 1i64;
289        }
290        m
291    }
292}
293#[derive(Debug, Clone, PartialEq, Eq, Default)]
294pub struct Listener {
295    pub name: String,
296    pub host: String,
297    pub port: u16,
298    pub security_protocol: i16,
299    pub unknown_tagged_fields: UnknownTaggedFields,
300}
301impl Encode for Listener {
302    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
303        let flex = version >= 0;
304        if version >= 0 {
305            if flex {
306                put_compact_string(buf, &self.name);
307            } else {
308                put_string(buf, &self.name);
309            }
310        }
311        if version >= 0 {
312            if flex {
313                put_compact_string(buf, &self.host);
314            } else {
315                put_string(buf, &self.host);
316            }
317        }
318        if version >= 0 {
319            put_u16(buf, self.port);
320        }
321        if version >= 0 {
322            put_i16(buf, self.security_protocol);
323        }
324        if flex {
325            let tagged = WriteTaggedFields::new();
326            tagged.write(buf, &self.unknown_tagged_fields);
327        }
328        Ok(())
329    }
330    fn encoded_len(&self, version: i16) -> usize {
331        let flex = version >= 0;
332        let mut n: usize = 0;
333        if version >= 0 {
334            n += if flex {
335                compact_string_len(&self.name)
336            } else {
337                string_len(&self.name)
338            };
339        }
340        if version >= 0 {
341            n += if flex {
342                compact_string_len(&self.host)
343            } else {
344                string_len(&self.host)
345            };
346        }
347        if version >= 0 {
348            n += 2;
349        }
350        if version >= 0 {
351            n += 2;
352        }
353        if flex {
354            let known_pairs: Vec<(u32, usize)> = Vec::new();
355            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
356        }
357        n
358    }
359}
360impl Decode<'_> for Listener {
361    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
362        let flex = version >= 0;
363        let mut out = Self::default();
364        if version >= 0 {
365            out.name = if flex {
366                get_compact_string_owned(buf)?
367            } else {
368                get_string_owned(buf)?
369            };
370        }
371        if version >= 0 {
372            out.host = if flex {
373                get_compact_string_owned(buf)?
374            } else {
375                get_string_owned(buf)?
376            };
377        }
378        if version >= 0 {
379            out.port = get_u16(buf)?;
380        }
381        if version >= 0 {
382            out.security_protocol = get_i16(buf)?;
383        }
384        if flex {
385            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
386        }
387        Ok(out)
388    }
389}
390#[cfg(test)]
391impl Listener {
392    #[must_use]
393    pub fn populated(version: i16) -> Self {
394        let mut m = Self::default();
395        if version >= 0 {
396            m.name = "x".to_string();
397        }
398        if version >= 0 {
399            m.host = "x".to_string();
400        }
401        if version >= 0 {
402            m.port = 1u16;
403        }
404        if version >= 0 {
405            m.security_protocol = 1i16;
406        }
407        m
408    }
409}
410#[derive(Debug, Clone, PartialEq, Eq, Default)]
411pub struct Feature {
412    pub name: String,
413    pub min_supported_version: i16,
414    pub max_supported_version: i16,
415    pub unknown_tagged_fields: UnknownTaggedFields,
416}
417impl Encode for Feature {
418    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
419        let flex = version >= 0;
420        if version >= 0 {
421            if flex {
422                put_compact_string(buf, &self.name);
423            } else {
424                put_string(buf, &self.name);
425            }
426        }
427        if version >= 0 {
428            put_i16(buf, self.min_supported_version);
429        }
430        if version >= 0 {
431            put_i16(buf, self.max_supported_version);
432        }
433        if flex {
434            let tagged = WriteTaggedFields::new();
435            tagged.write(buf, &self.unknown_tagged_fields);
436        }
437        Ok(())
438    }
439    fn encoded_len(&self, version: i16) -> usize {
440        let flex = version >= 0;
441        let mut n: usize = 0;
442        if version >= 0 {
443            n += if flex {
444                compact_string_len(&self.name)
445            } else {
446                string_len(&self.name)
447            };
448        }
449        if version >= 0 {
450            n += 2;
451        }
452        if version >= 0 {
453            n += 2;
454        }
455        if flex {
456            let known_pairs: Vec<(u32, usize)> = Vec::new();
457            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
458        }
459        n
460    }
461}
462impl Decode<'_> for Feature {
463    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
464        let flex = version >= 0;
465        let mut out = Self::default();
466        if version >= 0 {
467            out.name = if flex {
468                get_compact_string_owned(buf)?
469            } else {
470                get_string_owned(buf)?
471            };
472        }
473        if version >= 0 {
474            out.min_supported_version = get_i16(buf)?;
475        }
476        if version >= 0 {
477            out.max_supported_version = get_i16(buf)?;
478        }
479        if flex {
480            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
481        }
482        Ok(out)
483    }
484}
485#[cfg(test)]
486impl Feature {
487    #[must_use]
488    pub fn populated(version: i16) -> Self {
489        let mut m = Self::default();
490        if version >= 0 {
491            m.name = "x".to_string();
492        }
493        if version >= 0 {
494            m.min_supported_version = 1i16;
495        }
496        if version >= 0 {
497            m.max_supported_version = 1i16;
498        }
499        m
500    }
501}
502
503/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
504/// Only includes fields valid for the given version.
505#[must_use]
506#[allow(unused_comparisons)]
507pub fn default_json(version: i16) -> ::serde_json::Value {
508    let mut obj = ::serde_json::Map::new();
509    obj.insert("brokerId".to_string(), ::serde_json::json!(0));
510    obj.insert(
511        "clusterId".to_string(),
512        ::serde_json::Value::String(String::new()),
513    );
514    obj.insert(
515        "incarnationId".to_string(),
516        ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
517    );
518    obj.insert("listeners".to_string(), ::serde_json::Value::Array(vec![]));
519    obj.insert("features".to_string(), ::serde_json::Value::Array(vec![]));
520    obj.insert("rack".to_string(), ::serde_json::Value::Null);
521    if version >= 1 {
522        obj.insert(
523            "isMigratingZkBroker".to_string(),
524            ::serde_json::Value::Bool(false),
525        );
526    }
527    if version >= 2 {
528        obj.insert("logDirs".to_string(), ::serde_json::Value::Array(vec![]));
529    }
530    if version >= 3 {
531        obj.insert("previousBrokerEpoch".to_string(), ::serde_json::json!(-1));
532    }
533    ::serde_json::Value::Object(obj)
534}
535
536impl crate::ProtocolRequest for BrokerRegistrationRequest {
537    const API_KEY: i16 = API_KEY;
538    const MIN_VERSION: i16 = MIN_VERSION;
539    const MAX_VERSION: i16 = MAX_VERSION;
540    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
541    type Response = super::broker_registration_response::BrokerRegistrationResponse;
542}