Skip to main content

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