Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
OffsetForLeaderEpochRequest.owned.rs

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