Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
4use crate::primitives::string_bytes::{
5    compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
6    get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
7    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
8};
9use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
10use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
11use bytes::{Buf, BufMut};
12pub const API_KEY: i16 = 43;
13pub const MIN_VERSION: i16 = 0;
14pub const MAX_VERSION: i16 = 2;
15pub const FLEXIBLE_MIN: i16 = 2;
16#[inline]
17fn is_flexible(version: i16) -> bool {
18    version >= FLEXIBLE_MIN
19}
20#[derive(Debug, Clone, PartialEq, Eq, Default)]
21pub struct ElectLeadersResponse {
22    pub throttle_time_ms: i32,
23    pub error_code: i16,
24    pub replica_election_results: Vec<ReplicaElectionResult>,
25    pub unknown_tagged_fields: UnknownTaggedFields,
26}
27impl Encode for ElectLeadersResponse {
28    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
29        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
30            return Err(ProtocolError::UnsupportedVersion {
31                api_key: API_KEY,
32                version,
33            });
34        }
35        let flex = is_flexible(version);
36        if version >= 0 {
37            put_i32(buf, self.throttle_time_ms);
38        }
39        if version >= 1 {
40            put_i16(buf, self.error_code);
41        }
42        if version >= 0 {
43            {
44                crate::primitives::array::put_array_len(
45                    buf,
46                    (self.replica_election_results).len(),
47                    flex,
48                );
49                for it in &self.replica_election_results {
50                    it.encode(buf, version)?;
51                }
52            }
53        }
54        if flex {
55            let tagged = WriteTaggedFields::new();
56            tagged.write(buf, &self.unknown_tagged_fields);
57        }
58        Ok(())
59    }
60    fn encoded_len(&self, version: i16) -> usize {
61        let flex = is_flexible(version);
62        let mut n: usize = 0;
63        if version >= 0 {
64            n += 4;
65        }
66        if version >= 1 {
67            n += 2;
68        }
69        if version >= 0 {
70            n += {
71                let prefix = crate::primitives::array::array_len_prefix_len(
72                    (self.replica_election_results).len(),
73                    flex,
74                );
75                let body: usize = (self.replica_election_results)
76                    .iter()
77                    .map(|it| it.encoded_len(version))
78                    .sum();
79                prefix + body
80            };
81        }
82        if flex {
83            let known_pairs: Vec<(u32, usize)> = Vec::new();
84            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
85        }
86        n
87    }
88}
89impl Decode<'_> for ElectLeadersResponse {
90    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
91        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
92            return Err(ProtocolError::UnsupportedVersion {
93                api_key: API_KEY,
94                version,
95            });
96        }
97        let flex = is_flexible(version);
98        let mut out = Self::default();
99        if version >= 0 {
100            out.throttle_time_ms = get_i32(buf)?;
101        }
102        if version >= 1 {
103            out.error_code = get_i16(buf)?;
104        }
105        if version >= 0 {
106            out.replica_election_results = {
107                let n = crate::primitives::array::get_array_len(buf, flex)?;
108                let mut v = Vec::with_capacity(n);
109                for _ in 0..n {
110                    v.push(ReplicaElectionResult::decode(buf, version)?);
111                }
112                v
113            };
114        }
115        if flex {
116            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
117        }
118        Ok(out)
119    }
120}
121#[cfg(test)]
122impl ElectLeadersResponse {
123    #[must_use]
124    pub fn populated(version: i16) -> Self {
125        let mut m = Self::default();
126        if version >= 0 {
127            m.throttle_time_ms = 1i32;
128        }
129        if version >= 1 {
130            m.error_code = 1i16;
131        }
132        if version >= 0 {
133            m.replica_election_results = vec![ReplicaElectionResult::populated(version)];
134        }
135        m
136    }
137}
138#[derive(Debug, Clone, PartialEq, Eq, Default)]
139pub struct ReplicaElectionResult {
140    pub topic: String,
141    pub partition_result: Vec<PartitionResult>,
142    pub unknown_tagged_fields: UnknownTaggedFields,
143}
144impl Encode for ReplicaElectionResult {
145    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
146        let flex = version >= 2;
147        if version >= 0 {
148            if flex {
149                put_compact_string(buf, &self.topic);
150            } else {
151                put_string(buf, &self.topic);
152            }
153        }
154        if version >= 0 {
155            {
156                crate::primitives::array::put_array_len(buf, (self.partition_result).len(), flex);
157                for it in &self.partition_result {
158                    it.encode(buf, version)?;
159                }
160            }
161        }
162        if flex {
163            let tagged = WriteTaggedFields::new();
164            tagged.write(buf, &self.unknown_tagged_fields);
165        }
166        Ok(())
167    }
168    fn encoded_len(&self, version: i16) -> usize {
169        let flex = version >= 2;
170        let mut n: usize = 0;
171        if version >= 0 {
172            n += if flex {
173                compact_string_len(&self.topic)
174            } else {
175                string_len(&self.topic)
176            };
177        }
178        if version >= 0 {
179            n += {
180                let prefix = crate::primitives::array::array_len_prefix_len(
181                    (self.partition_result).len(),
182                    flex,
183                );
184                let body: usize = (self.partition_result)
185                    .iter()
186                    .map(|it| it.encoded_len(version))
187                    .sum();
188                prefix + body
189            };
190        }
191        if flex {
192            let known_pairs: Vec<(u32, usize)> = Vec::new();
193            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
194        }
195        n
196    }
197}
198impl Decode<'_> for ReplicaElectionResult {
199    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
200        let flex = version >= 2;
201        let mut out = Self::default();
202        if version >= 0 {
203            out.topic = if flex {
204                get_compact_string_owned(buf)?
205            } else {
206                get_string_owned(buf)?
207            };
208        }
209        if version >= 0 {
210            out.partition_result = {
211                let n = crate::primitives::array::get_array_len(buf, flex)?;
212                let mut v = Vec::with_capacity(n);
213                for _ in 0..n {
214                    v.push(PartitionResult::decode(buf, version)?);
215                }
216                v
217            };
218        }
219        if flex {
220            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
221        }
222        Ok(out)
223    }
224}
225#[cfg(test)]
226impl ReplicaElectionResult {
227    #[must_use]
228    pub fn populated(version: i16) -> Self {
229        let mut m = Self::default();
230        if version >= 0 {
231            m.topic = "x".to_string();
232        }
233        if version >= 0 {
234            m.partition_result = vec![PartitionResult::populated(version)];
235        }
236        m
237    }
238}
239#[derive(Debug, Clone, PartialEq, Eq, Default)]
240pub struct PartitionResult {
241    pub partition_id: i32,
242    pub error_code: i16,
243    pub error_message: Option<String>,
244    pub unknown_tagged_fields: UnknownTaggedFields,
245}
246impl Encode for PartitionResult {
247    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
248        let flex = version >= 2;
249        if version >= 0 {
250            put_i32(buf, self.partition_id);
251        }
252        if version >= 0 {
253            put_i16(buf, self.error_code);
254        }
255        if version >= 0 {
256            if flex {
257                put_compact_nullable_string(buf, self.error_message.as_deref());
258            } else {
259                put_nullable_string(buf, self.error_message.as_deref());
260            }
261        }
262        if flex {
263            let tagged = WriteTaggedFields::new();
264            tagged.write(buf, &self.unknown_tagged_fields);
265        }
266        Ok(())
267    }
268    fn encoded_len(&self, version: i16) -> usize {
269        let flex = version >= 2;
270        let mut n: usize = 0;
271        if version >= 0 {
272            n += 4;
273        }
274        if version >= 0 {
275            n += 2;
276        }
277        if version >= 0 {
278            n += if flex {
279                compact_nullable_string_len(self.error_message.as_deref())
280            } else {
281                nullable_string_len(self.error_message.as_deref())
282            };
283        }
284        if flex {
285            let known_pairs: Vec<(u32, usize)> = Vec::new();
286            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
287        }
288        n
289    }
290}
291impl Decode<'_> for PartitionResult {
292    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
293        let flex = version >= 2;
294        let mut out = Self::default();
295        if version >= 0 {
296            out.partition_id = get_i32(buf)?;
297        }
298        if version >= 0 {
299            out.error_code = get_i16(buf)?;
300        }
301        if version >= 0 {
302            out.error_message = if flex {
303                get_compact_nullable_string_owned(buf)?
304            } else {
305                get_nullable_string_owned(buf)?
306            };
307        }
308        if flex {
309            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
310        }
311        Ok(out)
312    }
313}
314#[cfg(test)]
315impl PartitionResult {
316    #[must_use]
317    pub fn populated(version: i16) -> Self {
318        let mut m = Self::default();
319        if version >= 0 {
320            m.partition_id = 1i32;
321        }
322        if version >= 0 {
323            m.error_code = 1i16;
324        }
325        if version >= 0 {
326            m.error_message = Some("x".to_string());
327        }
328        m
329    }
330}
331/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
332/// Only includes fields valid for the given version.
333#[must_use]
334#[allow(unused_comparisons)]
335pub fn default_json(version: i16) -> ::serde_json::Value {
336    let mut obj = ::serde_json::Map::new();
337    obj.insert("throttleTimeMs".to_string(), ::serde_json::json!(0));
338    if version >= 1 {
339        obj.insert("errorCode".to_string(), ::serde_json::json!(0));
340    }
341    obj.insert(
342        "replicaElectionResults".to_string(),
343        ::serde_json::Value::Array(vec![]),
344    );
345    ::serde_json::Value::Object(obj)
346}