Skip to main content

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