Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_bool, get_i32, get_i64, put_bool, put_i32, put_i64};
6use crate::tagged_fields::{encode_to_bytes, read_tagged_fields, tagged_fields_len, WriteTaggedFields};
7use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
8
9pub const API_KEY: i16 = 63;
10pub const MIN_VERSION: i16 = 0;
11pub const MAX_VERSION: i16 = 2;
12pub const FLEXIBLE_MIN: i16 = 0;
13
14#[inline]
15fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct BrokerHeartbeatRequest {
19    pub broker_id: i32,
20    pub broker_epoch: i64,
21    pub current_metadata_offset: i64,
22    pub want_fence: bool,
23    pub want_shut_down: bool,
24    pub offline_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}
28
29impl Default for BrokerHeartbeatRequest {
30    fn default() -> Self {
31        Self {
32            broker_id: 0i32,
33            broker_epoch: -1i64,
34            current_metadata_offset: 0i64,
35            want_fence: false,
36            want_shut_down: false,
37            offline_log_dirs: Vec::new(),
38            cordoned_log_dirs: None,
39            unknown_tagged_fields: Default::default(),
40        }
41    }
42}
43
44impl Encode for BrokerHeartbeatRequest {
45    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
46        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
47            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
48        }
49        let flex = is_flexible(version);
50        if version >= 0 { put_i32(buf, self.broker_id) }
51        if version >= 0 { put_i64(buf, self.broker_epoch) }
52        if version >= 0 { put_i64(buf, self.current_metadata_offset) }
53        if version >= 0 { put_bool(buf, self.want_fence) }
54        if version >= 0 { put_bool(buf, self.want_shut_down) }
55        if flex {
56            let mut tagged = WriteTaggedFields::new();
57            if !(crate::codegen_helpers::is_default(&self.offline_log_dirs)) {
58                let payload = encode_to_bytes({ let prefix = crate::primitives::array::array_len_prefix_len((self.offline_log_dirs).len(), flex); let body: usize = (self.offline_log_dirs).iter().map(|_| 16).sum(); prefix + body }, |b| { { crate::primitives::array::put_array_len(b, (self.offline_log_dirs).len(), flex); for it in &self.offline_log_dirs { crate::primitives::uuid::put_uuid(b, *it); } }; Ok(()) });
59                tagged.add(0, payload);
60            }
61            if !(self.cordoned_log_dirs.is_none()) {
62                let payload = encode_to_bytes({ let opt: Option<&Vec<_>> = (self.cordoned_log_dirs).as_ref(); let prefix = crate::primitives::array::nullable_array_len_prefix_len(opt.map(|v| v.len()), flex); let body: usize = opt.map_or(0, |v| v.iter().map(|_| 16).sum()); prefix + body }, |b| { { let len = (self.cordoned_log_dirs).as_ref().map(Vec::len); crate::primitives::array::put_nullable_array_len(b, len, flex); if let Some(v) = &self.cordoned_log_dirs { for it in v { crate::primitives::uuid::put_uuid(b, *it); } } }; Ok(()) });
63                tagged.add(1, payload);
64            }
65            tagged.write(buf, &self.unknown_tagged_fields);
66        }
67        Ok(())
68    }
69    fn encoded_len(&self, version: i16) -> usize {
70        let flex = is_flexible(version);
71        let mut n: usize = 0;
72        if version >= 0 { n += 4; }
73        if version >= 0 { n += 8; }
74        if version >= 0 { n += 8; }
75        if version >= 0 { n += 1; }
76        if version >= 0 { n += 1; }
77        if flex {
78            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
79            if !(crate::codegen_helpers::is_default(&self.offline_log_dirs)) {
80                known_pairs.push((0, { let prefix = crate::primitives::array::array_len_prefix_len((self.offline_log_dirs).len(), flex); let body: usize = (self.offline_log_dirs).iter().map(|_| 16).sum(); prefix + body }));
81            }
82            if !(self.cordoned_log_dirs.is_none()) {
83                known_pairs.push((1, { let opt: Option<&Vec<_>> = (self.cordoned_log_dirs).as_ref(); let prefix = crate::primitives::array::nullable_array_len_prefix_len(opt.map(|v| v.len()), flex); let body: usize = opt.map_or(0, |v| v.iter().map(|_| 16).sum()); prefix + body }));
84            }
85            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
86        }
87        n
88    }
89}
90
91impl<'de> Decode<'de> for BrokerHeartbeatRequest {
92    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
93        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
94            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
95        }
96        let flex = is_flexible(version);
97        let mut out = Self::default();
98        if version >= 0 { out.broker_id = get_i32(buf)?; }
99        if version >= 0 { out.broker_epoch = get_i64(buf)?; }
100        if version >= 0 { out.current_metadata_offset = get_i64(buf)?; }
101        if version >= 0 { out.want_fence = get_bool(buf)?; }
102        if version >= 0 { out.want_shut_down = get_bool(buf)?; }
103        if flex {
104            // Pre-declare typed slots for known tagged fields.
105            let mut tag_offline_log_dirs = None;
106            let mut tag_cordoned_log_dirs = None;
107            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
108                match tag {
109                0 => { tag_offline_log_dirs = Some({ let b: &mut &[u8] = payload; { let n = crate::primitives::array::get_array_len(b, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(crate::primitives::uuid::get_uuid(b)?); } v } }); Ok(true) }
110                1 => { tag_cordoned_log_dirs = Some({ let b: &mut &[u8] = payload; { let opt = crate::primitives::array::get_nullable_array_len(b, flex)?; match opt { None => None, Some(n) => { let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(crate::primitives::uuid::get_uuid(b)?); } Some(v) } } } }); Ok(true) }
111                    _ => Ok(false),
112                }
113            })?;
114            if let Some(v) = tag_offline_log_dirs { out.offline_log_dirs = v; }
115            if let Some(v) = tag_cordoned_log_dirs { out.cordoned_log_dirs = v; }
116        }
117        Ok(out)
118    }
119}
120
121/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
122/// Only includes fields valid for the given version.
123#[must_use]
124#[allow(unused_comparisons)]
125pub fn default_json(version: i16) -> ::serde_json::Value {
126    let mut obj = ::serde_json::Map::new();
127    obj.insert("brokerId".to_string(), ::serde_json::json!(0));
128    obj.insert("brokerEpoch".to_string(), ::serde_json::json!(-1));
129    obj.insert("currentMetadataOffset".to_string(), ::serde_json::json!(0));
130    obj.insert("wantFence".to_string(), ::serde_json::Value::Bool(false));
131    obj.insert("wantShutDown".to_string(), ::serde_json::Value::Bool(false));
132    if version >= 1 {
133        obj.insert("offlineLogDirs".to_string(), ::serde_json::Value::Array(vec![]));
134    }
135    if version >= 2 {
136        obj.insert("cordonedLogDirs".to_string(), ::serde_json::Value::Null);
137    }
138    ::serde_json::Value::Object(obj)
139}
140
141impl crate::ProtocolRequest for BrokerHeartbeatRequest {
142    const API_KEY: i16 = API_KEY;
143    const MIN_VERSION: i16 = MIN_VERSION;
144    const MAX_VERSION: i16 = MAX_VERSION;
145    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
146    type Response = super::broker_heartbeat_response::BrokerHeartbeatResponse;
147}