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