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 bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i32, get_i64, put_i32, put_i64};
6use crate::primitives::string_bytes::{
7    compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
8    get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
9    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
10    string_len,
11};
12use crate::tagged_fields::{encode_to_bytes, read_tagged_fields, tagged_fields_len, WriteTaggedFields};
13use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
14
15pub const API_KEY: i16 = 59;
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 1;
18pub const FLEXIBLE_MIN: i16 = 0;
19
20#[inline]
21fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
22
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct FetchSnapshotRequest {
25    pub replica_id: i32,
26    pub max_bytes: i32,
27    pub topics: Vec<TopicSnapshot>,
28    pub cluster_id: Option<String>,
29    pub unknown_tagged_fields: UnknownTaggedFields,
30}
31
32impl Default for FetchSnapshotRequest {
33    fn default() -> Self {
34        Self {
35            replica_id: -1i32,
36            max_bytes: 2_147_483_647i32,
37            topics: Vec::new(),
38            cluster_id: None,
39            unknown_tagged_fields: Default::default(),
40        }
41    }
42}
43
44impl Encode for FetchSnapshotRequest {
45    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
46        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
47            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
48        }
49        let flex = is_flexible(version);
50        if version >= 0 { put_i32(buf, self.replica_id) }
51        if version >= 0 { put_i32(buf, self.max_bytes) }
52        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
53        if flex {
54            let mut tagged = WriteTaggedFields::new();
55            if !(self.cluster_id.is_none()) {
56                let payload = encode_to_bytes(if flex { compact_nullable_string_len(self.cluster_id.as_deref()) } else { nullable_string_len(self.cluster_id.as_deref()) }, |b| { if flex { put_compact_nullable_string(b, self.cluster_id.as_deref()) } else { put_nullable_string(b, self.cluster_id.as_deref()) }; Ok(()) });
57                tagged.add(0, payload);
58            }
59            tagged.write(buf, &self.unknown_tagged_fields);
60        }
61        Ok(())
62    }
63    fn encoded_len(&self, version: i16) -> usize {
64        let flex = is_flexible(version);
65        let mut n: usize = 0;
66        if version >= 0 { n += 4; }
67        if version >= 0 { n += 4; }
68        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
69        if flex {
70            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
71            if !(self.cluster_id.is_none()) {
72                known_pairs.push((0, if flex { compact_nullable_string_len(self.cluster_id.as_deref()) } else { nullable_string_len(self.cluster_id.as_deref()) }));
73            }
74            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
75        }
76        n
77    }
78}
79
80impl<'de> Decode<'de> for FetchSnapshotRequest {
81    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
82        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
83            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
84        }
85        let flex = is_flexible(version);
86        let mut out = Self::default();
87        if version >= 0 { out.replica_id = get_i32(buf)?; }
88        if version >= 0 { out.max_bytes = get_i32(buf)?; }
89        if version >= 0 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(TopicSnapshot::decode(buf, version)?); } v }; }
90        if flex {
91            // Pre-declare typed slots for known tagged fields.
92            let mut tag_cluster_id = None;
93            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
94                match tag {
95                0 => { tag_cluster_id = Some({ let b: &mut &[u8] = payload; if flex { get_compact_nullable_string_owned(b)? } else { get_nullable_string_owned(b)? } }); Ok(true) }
96                    _ => Ok(false),
97                }
98            })?;
99            if let Some(v) = tag_cluster_id { out.cluster_id = v; }
100        }
101        Ok(out)
102    }
103}
104
105#[derive(Debug, Clone, PartialEq, Eq, Default)]
106pub struct TopicSnapshot {
107    pub name: String,
108    pub partitions: Vec<PartitionSnapshot>,
109    pub unknown_tagged_fields: UnknownTaggedFields,
110}
111
112impl Encode for TopicSnapshot {
113    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
114        let flex = version >= 0;
115        if version >= 0 { if flex { put_compact_string(buf, &self.name) } else { put_string(buf, &self.name) } }
116        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
117        if flex {
118            let tagged = WriteTaggedFields::new();
119            tagged.write(buf, &self.unknown_tagged_fields);
120        }
121        Ok(())
122    }
123    fn encoded_len(&self, version: i16) -> usize {
124        let flex = version >= 0;
125        let mut n: usize = 0;
126        if version >= 0 { n += if flex { compact_string_len(&self.name) } else { string_len(&self.name) }; }
127        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
128        if flex {
129            let known_pairs: Vec<(u32, usize)> = Vec::new();
130            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
131        }
132        n
133    }
134}
135
136impl<'de> Decode<'de> for TopicSnapshot {
137    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
138        let flex = version >= 0;
139        let mut out = Self::default();
140        if version >= 0 { out.name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
141        if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(PartitionSnapshot::decode(buf, version)?); } v }; }
142        if flex {
143            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
144                Ok(false)
145            })?;
146        }
147        Ok(out)
148    }
149}
150
151#[derive(Debug, Clone, PartialEq, Eq, Default)]
152pub struct PartitionSnapshot {
153    pub partition: i32,
154    pub current_leader_epoch: i32,
155    pub snapshot_id: SnapshotId,
156    pub position: i64,
157    pub replica_directory_id: crate::primitives::uuid::Uuid,
158    pub unknown_tagged_fields: UnknownTaggedFields,
159}
160
161impl Encode for PartitionSnapshot {
162    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
163        let flex = version >= 0;
164        if version >= 0 { put_i32(buf, self.partition) }
165        if version >= 0 { put_i32(buf, self.current_leader_epoch) }
166        if version >= 0 { self.snapshot_id.encode(buf, version)? }
167        if version >= 0 { put_i64(buf, self.position) }
168        if flex {
169            let mut tagged = WriteTaggedFields::new();
170            if !(crate::codegen_helpers::is_default(&self.replica_directory_id)) {
171                let payload = encode_to_bytes(16, |b| { crate::primitives::uuid::put_uuid(b, self.replica_directory_id); Ok(()) });
172                tagged.add(0, payload);
173            }
174            tagged.write(buf, &self.unknown_tagged_fields);
175        }
176        Ok(())
177    }
178    fn encoded_len(&self, version: i16) -> usize {
179        let flex = version >= 0;
180        let mut n: usize = 0;
181        if version >= 0 { n += 4; }
182        if version >= 0 { n += 4; }
183        if version >= 0 { n += self.snapshot_id.encoded_len(version); }
184        if version >= 0 { n += 8; }
185        if flex {
186            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
187            if !(crate::codegen_helpers::is_default(&self.replica_directory_id)) {
188                known_pairs.push((0, 16));
189            }
190            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
191        }
192        n
193    }
194}
195
196impl<'de> Decode<'de> for PartitionSnapshot {
197    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
198        let flex = version >= 0;
199        let mut out = Self::default();
200        if version >= 0 { out.partition = get_i32(buf)?; }
201        if version >= 0 { out.current_leader_epoch = get_i32(buf)?; }
202        if version >= 0 { out.snapshot_id = SnapshotId::decode(buf, version)?; }
203        if version >= 0 { out.position = get_i64(buf)?; }
204        if flex {
205            // Pre-declare typed slots for known tagged fields.
206            let mut tag_replica_directory_id = None;
207            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
208                match tag {
209                0 => { tag_replica_directory_id = Some({ let b: &mut &[u8] = payload; crate::primitives::uuid::get_uuid(b)? }); Ok(true) }
210                    _ => Ok(false),
211                }
212            })?;
213            if let Some(v) = tag_replica_directory_id { out.replica_directory_id = v; }
214        }
215        Ok(out)
216    }
217}
218
219#[derive(Debug, Clone, PartialEq, Eq, Default)]
220pub struct SnapshotId {
221    pub end_offset: i64,
222    pub epoch: i32,
223    pub unknown_tagged_fields: UnknownTaggedFields,
224}
225
226impl Encode for SnapshotId {
227    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
228        let flex = version >= 0;
229        if version >= 0 { put_i64(buf, self.end_offset) }
230        if version >= 0 { put_i32(buf, self.epoch) }
231        if flex {
232            let tagged = WriteTaggedFields::new();
233            tagged.write(buf, &self.unknown_tagged_fields);
234        }
235        Ok(())
236    }
237    fn encoded_len(&self, version: i16) -> usize {
238        let flex = version >= 0;
239        let mut n: usize = 0;
240        if version >= 0 { n += 8; }
241        if version >= 0 { n += 4; }
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}
249
250impl<'de> Decode<'de> for SnapshotId {
251    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
252        let flex = version >= 0;
253        let mut out = Self::default();
254        if version >= 0 { out.end_offset = get_i64(buf)?; }
255        if version >= 0 { out.epoch = get_i32(buf)?; }
256        if flex {
257            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
258                Ok(false)
259            })?;
260        }
261        Ok(out)
262    }
263}
264
265/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
266/// Only includes fields valid for the given version.
267#[must_use]
268#[allow(unused_comparisons)]
269pub fn default_json(version: i16) -> ::serde_json::Value {
270    let mut obj = ::serde_json::Map::new();
271    obj.insert("clusterId".to_string(), ::serde_json::Value::Null);
272    obj.insert("replicaId".to_string(), ::serde_json::json!(-1));
273    obj.insert("maxBytes".to_string(), ::serde_json::json!(2147483647));
274    obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
275    ::serde_json::Value::Object(obj)
276}
277
278impl crate::ProtocolRequest for FetchSnapshotRequest {
279    const API_KEY: i16 = API_KEY;
280    const MIN_VERSION: i16 = MIN_VERSION;
281    const MAX_VERSION: i16 = MAX_VERSION;
282    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
283    type Response = super::fetch_snapshot_response::FetchSnapshotResponse;
284}