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