Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
OffsetForLeaderEpochResponse.owned.rs

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