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    owned::offset_for_leader_epoch_request,
7    primitives::{
8        array::{array_len_prefix_len, get_array_len, put_array_len},
9        fixed::{get_i32, put_i32},
10        string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
11        string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
12    },
13    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
14};
15pub const API_KEY: i16 = 23;
16pub const MIN_VERSION: i16 = 2;
17pub const MAX_VERSION: i16 = 4;
18pub const LATEST_STABLE_VERSION: i16 = 4;
19pub const FLEXIBLE_MIN: i16 = 4;
20#[inline]
21#[must_use]
22pub fn is_flexible(version: i16) -> bool {
23    version >= FLEXIBLE_MIN
24}
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct OffsetForLeaderEpochRequest<'a> {
27    pub replica_id: i32,
28    pub topics: Vec<OffsetForLeaderTopic<'a>>,
29    pub unknown_tagged_fields: UnknownTaggedFields,
30}
31impl Default for OffsetForLeaderEpochRequest<'_> {
32    fn default() -> Self {
33        Self {
34            replica_id: -2i32,
35            topics: Vec::new(),
36            unknown_tagged_fields: UnknownTaggedFields::default(),
37        }
38    }
39}
40impl OffsetForLeaderEpochRequest<'_> {
41    #[must_use]
42    pub fn to_owned(&self) -> offset_for_leader_epoch_request::OffsetForLeaderEpochRequest {
43        offset_for_leader_epoch_request::OffsetForLeaderEpochRequest {
44            replica_id: (self.replica_id),
45            topics: (self.topics)
46                .iter()
47                .map(OffsetForLeaderTopic::to_owned)
48                .collect(),
49            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
50        }
51    }
52}
53impl Encode for OffsetForLeaderEpochRequest<'_> {
54    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
55        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
56            return Err(ProtocolError::UnsupportedVersion {
57                api_key: API_KEY,
58                version,
59            });
60        }
61        let flex = is_flexible(version);
62        if version >= 3 {
63            put_i32(buf, self.replica_id);
64        }
65        if version >= 0 {
66            {
67                put_array_len(buf, (self.topics).len(), flex)?;
68                for it in &self.topics {
69                    it.encode(buf, version)?;
70                }
71            }
72        }
73        if flex {
74            let tagged = WriteTaggedFields::new();
75            tagged.write(buf, &self.unknown_tagged_fields)?;
76        }
77        Ok(())
78    }
79    fn encoded_len(&self, version: i16) -> usize {
80        let flex = is_flexible(version);
81        let mut n: usize = 0;
82        if version >= 3 {
83            n += 4;
84        }
85        if version >= 0 {
86            n += {
87                let prefix = 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 = 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    #[must_use]
150    pub fn to_owned(&self) -> offset_for_leader_epoch_request::OffsetForLeaderTopic {
151        offset_for_leader_epoch_request::OffsetForLeaderTopic {
152            topic: (self.topic).to_owned(),
153            partitions: (self.partitions)
154                .iter()
155                .map(OffsetForLeaderPartition::to_owned)
156                .collect(),
157            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
158        }
159    }
160}
161impl Encode for OffsetForLeaderTopic<'_> {
162    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
163        let flex = version >= 4;
164        if version >= 0 {
165            if flex {
166                put_compact_string(buf, self.topic)?;
167            } else {
168                put_string(buf, self.topic)?;
169            }
170        }
171        if version >= 0 {
172            {
173                put_array_len(buf, (self.partitions).len(), flex)?;
174                for it in &self.partitions {
175                    it.encode(buf, version)?;
176                }
177            }
178        }
179        if flex {
180            let tagged = WriteTaggedFields::new();
181            tagged.write(buf, &self.unknown_tagged_fields)?;
182        }
183        Ok(())
184    }
185    fn encoded_len(&self, version: i16) -> usize {
186        let flex = version >= 4;
187        let mut n: usize = 0;
188        if version >= 0 {
189            n += if flex {
190                compact_string_len(self.topic)
191            } else {
192                string_len(self.topic)
193            };
194        }
195        if version >= 0 {
196            n += {
197                let prefix = 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 = 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: UnknownTaggedFields::default(),
267        }
268    }
269}
270impl OffsetForLeaderPartition {
271    #[must_use]
272    pub fn to_owned(&self) -> offset_for_leader_epoch_request::OffsetForLeaderPartition {
273        offset_for_leader_epoch_request::OffsetForLeaderPartition {
274            partition: (self.partition),
275            current_leader_epoch: (self.current_leader_epoch),
276            leader_epoch: (self.leader_epoch),
277            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
278        }
279    }
280}
281impl Encode for OffsetForLeaderPartition {
282    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
283        let flex = version >= 4;
284        if version >= 0 {
285            put_i32(buf, self.partition);
286        }
287        if version >= 2 {
288            put_i32(buf, self.current_leader_epoch);
289        }
290        if version >= 0 {
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 >= 4;
301        let mut n: usize = 0;
302        if version >= 0 {
303            n += 4;
304        }
305        if version >= 2 {
306            n += 4;
307        }
308        if version >= 0 {
309            n += 4;
310        }
311        if flex {
312            let known_pairs: Vec<(u32, usize)> = Vec::new();
313            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
314        }
315        n
316    }
317}
318impl<'de> DecodeBorrow<'de> for OffsetForLeaderPartition {
319    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
320        let flex = version >= 4;
321        let mut out = Self::default();
322        if version >= 0 {
323            out.partition = get_i32(buf)?;
324        }
325        if version >= 2 {
326            out.current_leader_epoch = get_i32(buf)?;
327        }
328        if version >= 0 {
329            out.leader_epoch = get_i32(buf)?;
330        }
331        if flex {
332            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
333        }
334        Ok(out)
335    }
336}
337#[cfg(test)]
338impl OffsetForLeaderPartition {
339    #[must_use]
340    pub fn populated(version: i16) -> Self {
341        let mut m = Self::default();
342        if version >= 0 {
343            m.partition = 1i32;
344        }
345        if version >= 2 {
346            m.current_leader_epoch = 1i32;
347        }
348        if version >= 0 {
349            m.leader_epoch = 1i32;
350        }
351        m
352    }
353}