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