Skip to main content

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