Skip to main content

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