Skip to main content

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

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