Skip to main content

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

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