Skip to main content

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