Skip to main content

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

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