Skip to main content

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

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