Skip to main content

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

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