Skip to main content

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