Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
ElectLeadersResponse.borrowed.rs

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