Skip to main content

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

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