Skip to main content

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

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