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