Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use crate::primitives::fixed::{get_i32, get_i64, put_i32, put_i64};
4use crate::primitives::string_bytes::{
5    compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
6    get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
7    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
8};
9use crate::tagged_fields::{
10    WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len,
11};
12use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
13use bytes::{Buf, BufMut};
14pub const API_KEY: i16 = 59;
15pub const MIN_VERSION: i16 = 0;
16pub const MAX_VERSION: i16 = 1;
17pub const FLEXIBLE_MIN: i16 = 0;
18#[inline]
19fn is_flexible(version: i16) -> bool {
20    version >= FLEXIBLE_MIN
21}
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct FetchSnapshotRequest {
24    pub replica_id: i32,
25    pub max_bytes: i32,
26    pub topics: Vec<TopicSnapshot>,
27    pub cluster_id: Option<String>,
28    pub unknown_tagged_fields: UnknownTaggedFields,
29}
30impl Default for FetchSnapshotRequest {
31    fn default() -> Self {
32        Self {
33            replica_id: -1i32,
34            max_bytes: 2_147_483_647i32,
35            topics: Vec::new(),
36            cluster_id: None,
37            unknown_tagged_fields: Default::default(),
38        }
39    }
40}
41impl Encode for FetchSnapshotRequest {
42    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
43        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
44            return Err(ProtocolError::UnsupportedVersion {
45                api_key: API_KEY,
46                version,
47            });
48        }
49        let flex = is_flexible(version);
50        if version >= 0 {
51            put_i32(buf, self.replica_id);
52        }
53        if version >= 0 {
54            put_i32(buf, self.max_bytes);
55        }
56        if version >= 0 {
57            {
58                crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
59                for it in &self.topics {
60                    it.encode(buf, version)?;
61                }
62            }
63        }
64        if flex {
65            let mut tagged = WriteTaggedFields::new();
66            if !(self.cluster_id.is_none()) {
67                let payload = encode_to_bytes(
68                    if flex {
69                        compact_nullable_string_len(self.cluster_id.as_deref())
70                    } else {
71                        nullable_string_len(self.cluster_id.as_deref())
72                    },
73                    |b| {
74                        if flex {
75                            put_compact_nullable_string(b, self.cluster_id.as_deref());
76                        } else {
77                            put_nullable_string(b, self.cluster_id.as_deref());
78                        }
79                        Ok(())
80                    },
81                );
82                tagged.add(0, payload);
83            }
84            tagged.write(buf, &self.unknown_tagged_fields);
85        }
86        Ok(())
87    }
88    fn encoded_len(&self, version: i16) -> usize {
89        let flex = is_flexible(version);
90        let mut n: usize = 0;
91        if version >= 0 {
92            n += 4;
93        }
94        if version >= 0 {
95            n += 4;
96        }
97        if version >= 0 {
98            n += {
99                let prefix =
100                    crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
101                let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
102                prefix + body
103            };
104        }
105        if flex {
106            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
107            if !(self.cluster_id.is_none()) {
108                known_pairs.push((
109                    0,
110                    if flex {
111                        compact_nullable_string_len(self.cluster_id.as_deref())
112                    } else {
113                        nullable_string_len(self.cluster_id.as_deref())
114                    },
115                ));
116            }
117            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
118        }
119        n
120    }
121}
122impl Decode<'_> for FetchSnapshotRequest {
123    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
124        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
125            return Err(ProtocolError::UnsupportedVersion {
126                api_key: API_KEY,
127                version,
128            });
129        }
130        let flex = is_flexible(version);
131        let mut out = Self::default();
132        if version >= 0 {
133            out.replica_id = get_i32(buf)?;
134        }
135        if version >= 0 {
136            out.max_bytes = get_i32(buf)?;
137        }
138        if version >= 0 {
139            out.topics = {
140                let n = crate::primitives::array::get_array_len(buf, flex)?;
141                let mut v = Vec::with_capacity(n);
142                for _ in 0..n {
143                    v.push(TopicSnapshot::decode(buf, version)?);
144                }
145                v
146            };
147        }
148        if flex {
149            let mut tag_cluster_id = None;
150            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
151                0 => {
152                    tag_cluster_id = Some({
153                        let b: &mut &[u8] = payload;
154                        if flex {
155                            get_compact_nullable_string_owned(b)?
156                        } else {
157                            get_nullable_string_owned(b)?
158                        }
159                    });
160                    Ok(true)
161                }
162                _ => Ok(false),
163            })?;
164            if let Some(v) = tag_cluster_id {
165                out.cluster_id = v;
166            }
167        }
168        Ok(out)
169    }
170}
171#[cfg(test)]
172impl FetchSnapshotRequest {
173    #[must_use]
174    pub fn populated(version: i16) -> Self {
175        let mut m = Self::default();
176        if version >= 0 {
177            m.replica_id = 1i32;
178        }
179        if version >= 0 {
180            m.max_bytes = 1i32;
181        }
182        if version >= 0 {
183            m.topics = vec![TopicSnapshot::populated(version)];
184        }
185        if version >= 0 {
186            m.cluster_id = Some("x".to_string());
187        }
188        m
189    }
190}
191#[derive(Debug, Clone, PartialEq, Eq, Default)]
192pub struct TopicSnapshot {
193    pub name: String,
194    pub partitions: Vec<PartitionSnapshot>,
195    pub unknown_tagged_fields: UnknownTaggedFields,
196}
197impl Encode for TopicSnapshot {
198    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
199        let flex = version >= 0;
200        if version >= 0 {
201            if flex {
202                put_compact_string(buf, &self.name);
203            } else {
204                put_string(buf, &self.name);
205            }
206        }
207        if version >= 0 {
208            {
209                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
210                for it in &self.partitions {
211                    it.encode(buf, version)?;
212                }
213            }
214        }
215        if flex {
216            let tagged = WriteTaggedFields::new();
217            tagged.write(buf, &self.unknown_tagged_fields);
218        }
219        Ok(())
220    }
221    fn encoded_len(&self, version: i16) -> usize {
222        let flex = version >= 0;
223        let mut n: usize = 0;
224        if version >= 0 {
225            n += if flex {
226                compact_string_len(&self.name)
227            } else {
228                string_len(&self.name)
229            };
230        }
231        if version >= 0 {
232            n += {
233                let prefix =
234                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
235                let body: usize = (self.partitions)
236                    .iter()
237                    .map(|it| it.encoded_len(version))
238                    .sum();
239                prefix + body
240            };
241        }
242        if flex {
243            let known_pairs: Vec<(u32, usize)> = Vec::new();
244            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
245        }
246        n
247    }
248}
249impl Decode<'_> for TopicSnapshot {
250    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
251        let flex = version >= 0;
252        let mut out = Self::default();
253        if version >= 0 {
254            out.name = if flex {
255                get_compact_string_owned(buf)?
256            } else {
257                get_string_owned(buf)?
258            };
259        }
260        if version >= 0 {
261            out.partitions = {
262                let n = crate::primitives::array::get_array_len(buf, flex)?;
263                let mut v = Vec::with_capacity(n);
264                for _ in 0..n {
265                    v.push(PartitionSnapshot::decode(buf, version)?);
266                }
267                v
268            };
269        }
270        if flex {
271            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
272        }
273        Ok(out)
274    }
275}
276#[cfg(test)]
277impl TopicSnapshot {
278    #[must_use]
279    pub fn populated(version: i16) -> Self {
280        let mut m = Self::default();
281        if version >= 0 {
282            m.name = "x".to_string();
283        }
284        if version >= 0 {
285            m.partitions = vec![PartitionSnapshot::populated(version)];
286        }
287        m
288    }
289}
290#[derive(Debug, Clone, PartialEq, Eq, Default)]
291pub struct PartitionSnapshot {
292    pub partition: i32,
293    pub current_leader_epoch: i32,
294    pub snapshot_id: SnapshotId,
295    pub position: i64,
296    pub replica_directory_id: crate::primitives::uuid::Uuid,
297    pub unknown_tagged_fields: UnknownTaggedFields,
298}
299impl Encode for PartitionSnapshot {
300    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
301        let flex = version >= 0;
302        if version >= 0 {
303            put_i32(buf, self.partition);
304        }
305        if version >= 0 {
306            put_i32(buf, self.current_leader_epoch);
307        }
308        if version >= 0 {
309            self.snapshot_id.encode(buf, version)?;
310        }
311        if version >= 0 {
312            put_i64(buf, self.position);
313        }
314        if flex {
315            let mut tagged = WriteTaggedFields::new();
316            if !(crate::codegen_helpers::is_default(&self.replica_directory_id)) {
317                let payload = encode_to_bytes(16, |b| {
318                    crate::primitives::uuid::put_uuid(b, self.replica_directory_id);
319                    Ok(())
320                });
321                tagged.add(0, payload);
322            }
323            tagged.write(buf, &self.unknown_tagged_fields);
324        }
325        Ok(())
326    }
327    fn encoded_len(&self, version: i16) -> usize {
328        let flex = version >= 0;
329        let mut n: usize = 0;
330        if version >= 0 {
331            n += 4;
332        }
333        if version >= 0 {
334            n += 4;
335        }
336        if version >= 0 {
337            n += self.snapshot_id.encoded_len(version);
338        }
339        if version >= 0 {
340            n += 8;
341        }
342        if flex {
343            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
344            if !(crate::codegen_helpers::is_default(&self.replica_directory_id)) {
345                known_pairs.push((0, 16));
346            }
347            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
348        }
349        n
350    }
351}
352impl Decode<'_> for PartitionSnapshot {
353    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
354        let flex = version >= 0;
355        let mut out = Self::default();
356        if version >= 0 {
357            out.partition = get_i32(buf)?;
358        }
359        if version >= 0 {
360            out.current_leader_epoch = get_i32(buf)?;
361        }
362        if version >= 0 {
363            out.snapshot_id = SnapshotId::decode(buf, version)?;
364        }
365        if version >= 0 {
366            out.position = get_i64(buf)?;
367        }
368        if flex {
369            let mut tag_replica_directory_id = None;
370            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
371                0 => {
372                    tag_replica_directory_id = Some({
373                        let b: &mut &[u8] = payload;
374                        crate::primitives::uuid::get_uuid(b)?
375                    });
376                    Ok(true)
377                }
378                _ => Ok(false),
379            })?;
380            if let Some(v) = tag_replica_directory_id {
381                out.replica_directory_id = v;
382            }
383        }
384        Ok(out)
385    }
386}
387#[cfg(test)]
388impl PartitionSnapshot {
389    #[must_use]
390    pub fn populated(version: i16) -> Self {
391        let mut m = Self::default();
392        if version >= 0 {
393            m.partition = 1i32;
394        }
395        if version >= 0 {
396            m.current_leader_epoch = 1i32;
397        }
398        if version >= 0 {
399            m.snapshot_id = SnapshotId::populated(version);
400        }
401        if version >= 0 {
402            m.position = 1i64;
403        }
404        if version >= 1 {
405            m.replica_directory_id = crate::primitives::uuid::Uuid([1u8; 16]);
406        }
407        m
408    }
409}
410#[derive(Debug, Clone, PartialEq, Eq, Default)]
411pub struct SnapshotId {
412    pub end_offset: i64,
413    pub epoch: i32,
414    pub unknown_tagged_fields: UnknownTaggedFields,
415}
416impl Encode for SnapshotId {
417    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
418        let flex = version >= 0;
419        if version >= 0 {
420            put_i64(buf, self.end_offset);
421        }
422        if version >= 0 {
423            put_i32(buf, self.epoch);
424        }
425        if flex {
426            let tagged = WriteTaggedFields::new();
427            tagged.write(buf, &self.unknown_tagged_fields);
428        }
429        Ok(())
430    }
431    fn encoded_len(&self, version: i16) -> usize {
432        let flex = version >= 0;
433        let mut n: usize = 0;
434        if version >= 0 {
435            n += 8;
436        }
437        if version >= 0 {
438            n += 4;
439        }
440        if flex {
441            let known_pairs: Vec<(u32, usize)> = Vec::new();
442            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
443        }
444        n
445    }
446}
447impl Decode<'_> for SnapshotId {
448    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
449        let flex = version >= 0;
450        let mut out = Self::default();
451        if version >= 0 {
452            out.end_offset = get_i64(buf)?;
453        }
454        if version >= 0 {
455            out.epoch = get_i32(buf)?;
456        }
457        if flex {
458            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
459        }
460        Ok(out)
461    }
462}
463#[cfg(test)]
464impl SnapshotId {
465    #[must_use]
466    pub fn populated(version: i16) -> Self {
467        let mut m = Self::default();
468        if version >= 0 {
469            m.end_offset = 1i64;
470        }
471        if version >= 0 {
472            m.epoch = 1i32;
473        }
474        m
475    }
476}
477/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
478/// Only includes fields valid for the given version.
479#[must_use]
480#[allow(unused_comparisons)]
481pub fn default_json(version: i16) -> ::serde_json::Value {
482    let mut obj = ::serde_json::Map::new();
483    obj.insert("clusterId".to_string(), ::serde_json::Value::Null);
484    obj.insert("replicaId".to_string(), ::serde_json::json!(-1));
485    obj.insert("maxBytes".to_string(), ::serde_json::json!(2147483647));
486    obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
487    ::serde_json::Value::Object(obj)
488}
489impl crate::ProtocolRequest for FetchSnapshotRequest {
490    const API_KEY: i16 = API_KEY;
491    const MIN_VERSION: i16 = MIN_VERSION;
492    const MAX_VERSION: i16 = MAX_VERSION;
493    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
494    type Response = super::fetch_snapshot_response::FetchSnapshotResponse;
495}