Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
FetchSnapshotRequest.borrowed.rs

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