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