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