Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
BrokerHeartbeatRequest.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_bool, get_i32, get_i64, put_bool, put_i32, put_i64},
8    tagged_fields::{WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len},
9};
10pub const API_KEY: i16 = 63;
11pub const MIN_VERSION: i16 = 0;
12pub const MAX_VERSION: i16 = 2;
13pub const LATEST_STABLE_VERSION: i16 = 2;
14pub const FLEXIBLE_MIN: i16 = 0;
15#[inline]
16#[must_use]
17pub fn is_flexible(version: i16) -> bool {
18    version >= FLEXIBLE_MIN
19}
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct BrokerHeartbeatRequest {
22    pub broker_id: i32,
23    pub broker_epoch: i64,
24    pub current_metadata_offset: i64,
25    pub want_fence: bool,
26    pub want_shut_down: bool,
27    pub offline_log_dirs: Vec<crate::primitives::uuid::Uuid>,
28    pub cordoned_log_dirs: Option<Vec<crate::primitives::uuid::Uuid>>,
29    pub unknown_tagged_fields: UnknownTaggedFields,
30}
31impl Default for BrokerHeartbeatRequest {
32    fn default() -> Self {
33        Self {
34            broker_id: 0i32,
35            broker_epoch: -1i64,
36            current_metadata_offset: 0i64,
37            want_fence: false,
38            want_shut_down: false,
39            offline_log_dirs: Vec::new(),
40            cordoned_log_dirs: None,
41            unknown_tagged_fields: UnknownTaggedFields::default(),
42        }
43    }
44}
45impl Encode for BrokerHeartbeatRequest {
46    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
47        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
48            return Err(ProtocolError::UnsupportedVersion {
49                api_key: API_KEY,
50                version,
51            });
52        }
53        let flex = is_flexible(version);
54        if version >= 0 {
55            put_i32(buf, self.broker_id);
56        }
57        if version >= 0 {
58            put_i64(buf, self.broker_epoch);
59        }
60        if version >= 0 {
61            put_i64(buf, self.current_metadata_offset);
62        }
63        if version >= 0 {
64            put_bool(buf, self.want_fence);
65        }
66        if version >= 0 {
67            put_bool(buf, self.want_shut_down);
68        }
69        if flex {
70            let mut tagged = WriteTaggedFields::new();
71            if version >= 1 && !(crate::codegen_helpers::is_default(&self.offline_log_dirs)) {
72                let payload = encode_to_bytes(
73                    {
74                        let prefix = crate::primitives::array::array_len_prefix_len(
75                            (self.offline_log_dirs).len(),
76                            flex,
77                        );
78                        let body: usize = (self.offline_log_dirs).iter().map(|_| 16).sum();
79                        prefix + body
80                    },
81                    |b| {
82                        {
83                            crate::primitives::array::put_array_len(
84                                b,
85                                (self.offline_log_dirs).len(),
86                                flex,
87                            );
88                            for it in &self.offline_log_dirs {
89                                crate::primitives::uuid::put_uuid(b, *it);
90                            }
91                        };
92                        Ok(())
93                    },
94                );
95                tagged.add(0, payload);
96            }
97            if version >= 2 && self.cordoned_log_dirs.is_some() {
98                let payload = encode_to_bytes(
99                    {
100                        let opt: Option<&Vec<_>> = (self.cordoned_log_dirs).as_ref();
101                        let prefix = crate::primitives::array::nullable_array_len_prefix_len(
102                            opt.map(std::vec::Vec::len),
103                            flex,
104                        );
105                        let body: usize = opt.map_or(0, |v| v.iter().map(|_| 16).sum());
106                        prefix + body
107                    },
108                    |b| {
109                        {
110                            let len = (self.cordoned_log_dirs).as_ref().map(Vec::len);
111                            crate::primitives::array::put_nullable_array_len(b, len, flex);
112                            if let Some(v) = &self.cordoned_log_dirs {
113                                for it in v {
114                                    crate::primitives::uuid::put_uuid(b, *it);
115                                }
116                            }
117                        };
118                        Ok(())
119                    },
120                );
121                tagged.add(1, payload);
122            }
123            tagged.write(buf, &self.unknown_tagged_fields);
124        }
125        Ok(())
126    }
127    fn encoded_len(&self, version: i16) -> usize {
128        let flex = is_flexible(version);
129        let mut n: usize = 0;
130        if version >= 0 {
131            n += 4;
132        }
133        if version >= 0 {
134            n += 8;
135        }
136        if version >= 0 {
137            n += 8;
138        }
139        if version >= 0 {
140            n += 1;
141        }
142        if version >= 0 {
143            n += 1;
144        }
145        if flex {
146            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
147            if version >= 1 && !(crate::codegen_helpers::is_default(&self.offline_log_dirs)) {
148                known_pairs.push((0, {
149                    let prefix = crate::primitives::array::array_len_prefix_len(
150                        (self.offline_log_dirs).len(),
151                        flex,
152                    );
153                    let body: usize = (self.offline_log_dirs).iter().map(|_| 16).sum();
154                    prefix + body
155                }));
156            }
157            if version >= 2 && self.cordoned_log_dirs.is_some() {
158                known_pairs.push((1, {
159                    let opt: Option<&Vec<_>> = (self.cordoned_log_dirs).as_ref();
160                    let prefix = crate::primitives::array::nullable_array_len_prefix_len(
161                        opt.map(std::vec::Vec::len),
162                        flex,
163                    );
164                    let body: usize = opt.map_or(0, |v| v.iter().map(|_| 16).sum());
165                    prefix + body
166                }));
167            }
168            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
169        }
170        n
171    }
172}
173impl Decode<'_> for BrokerHeartbeatRequest {
174    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
175        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
176            return Err(ProtocolError::UnsupportedVersion {
177                api_key: API_KEY,
178                version,
179            });
180        }
181        let flex = is_flexible(version);
182        let mut out = Self::default();
183        if version >= 0 {
184            out.broker_id = get_i32(buf)?;
185        }
186        if version >= 0 {
187            out.broker_epoch = get_i64(buf)?;
188        }
189        if version >= 0 {
190            out.current_metadata_offset = get_i64(buf)?;
191        }
192        if version >= 0 {
193            out.want_fence = get_bool(buf)?;
194        }
195        if version >= 0 {
196            out.want_shut_down = get_bool(buf)?;
197        }
198        if flex {
199            let mut tag_offline_log_dirs = None;
200            let mut tag_cordoned_log_dirs = None;
201            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
202                0 if version >= 1 => {
203                    tag_offline_log_dirs = Some({
204                        let b: &mut &[u8] = payload;
205                        {
206                            let n = crate::primitives::array::get_array_len(b, flex)?;
207                            let mut v = Vec::with_capacity(n);
208                            for _ in 0..n {
209                                v.push(crate::primitives::uuid::get_uuid(b)?);
210                            }
211                            v
212                        }
213                    });
214                    Ok(true)
215                }
216                0 => Err(ProtocolError::TagNotValidForVersion { tag: 0, version }),
217                1 if version >= 2 => {
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                1 => Err(ProtocolError::TagNotValidForVersion { tag: 1, version }),
237                _ => Ok(false),
238            })?;
239            if let Some(v) = tag_offline_log_dirs {
240                out.offline_log_dirs = v;
241            }
242            if let Some(v) = tag_cordoned_log_dirs {
243                out.cordoned_log_dirs = v;
244            }
245        }
246        Ok(out)
247    }
248}
249#[cfg(test)]
250impl BrokerHeartbeatRequest {
251    #[must_use]
252    pub fn populated(version: i16) -> Self {
253        let mut m = Self::default();
254        if version >= 0 {
255            m.broker_id = 1i32;
256        }
257        if version >= 0 {
258            m.broker_epoch = 1i64;
259        }
260        if version >= 0 {
261            m.current_metadata_offset = 1i64;
262        }
263        if version >= 0 {
264            m.want_fence = true;
265        }
266        if version >= 0 {
267            m.want_shut_down = true;
268        }
269        if version >= 1 {
270            m.offline_log_dirs = vec![crate::primitives::uuid::Uuid([1u8; 16])];
271        }
272        if version >= 2 {
273            m.cordoned_log_dirs = Some(vec![crate::primitives::uuid::Uuid([1u8; 16])]);
274        }
275        m
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/// A message with a non-default value in every tagged field, at every depth, for
301/// the JVM oracle differential sweep. The value is the same at every version.
302#[must_use]
303pub fn tagged_fixture() -> BrokerHeartbeatRequest {
304    BrokerHeartbeatRequest {
305        offline_log_dirs: vec![crate::primitives::uuid::Uuid([1u8; 16])],
306        cordoned_log_dirs: Some(vec![crate::primitives::uuid::Uuid([1u8; 16])]),
307        ..BrokerHeartbeatRequest::default()
308    }
309}
310/// The JSON form of `tagged_fixture()` that Kafka's JSON converter reads at
311/// `version`. It holds only the fields that the schema declares at that version.
312#[must_use]
313pub fn tagged_fixture_json(version: i16) -> ::serde_json::Value {
314    let mut m = ::serde_json::Map::new();
315    m.insert("brokerId".to_string(), ::serde_json::json!(0));
316    m.insert("brokerEpoch".to_string(), ::serde_json::json!(-1));
317    m.insert("currentMetadataOffset".to_string(), ::serde_json::json!(0));
318    m.insert("wantFence".to_string(), ::serde_json::Value::Bool(false));
319    m.insert("wantShutDown".to_string(), ::serde_json::Value::Bool(false));
320    if version >= 1 {
321        m.insert(
322            "offlineLogDirs".to_string(),
323            ::serde_json::Value::Array(vec![::serde_json::Value::String(
324                "AQEBAQEBAQEBAQEBAQEBAQ".to_string(),
325            )]),
326        );
327    }
328    if version == 2 {
329        m.insert(
330            "cordonedLogDirs".to_string(),
331            ::serde_json::Value::Array(vec![::serde_json::Value::String(
332                "AQEBAQEBAQEBAQEBAQEBAQ".to_string(),
333            )]),
334        );
335    }
336    ::serde_json::Value::Object(m)
337}
338impl crate::ProtocolRequest for BrokerHeartbeatRequest {
339    const API_KEY: i16 = API_KEY;
340    const MIN_VERSION: i16 = MIN_VERSION;
341    const MAX_VERSION: i16 = MAX_VERSION;
342    const LATEST_STABLE_VERSION: i16 = LATEST_STABLE_VERSION;
343    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
344    type Response = super::broker_heartbeat_response::BrokerHeartbeatResponse;
345}