1use crate::primitives::fixed::{
4 get_bool, get_i8, get_i32, get_i64, put_bool, put_i8, put_i32, put_i64,
5};
6use crate::primitives::string_bytes::{
7 compact_nullable_string_len, get_compact_nullable_string_owned, get_nullable_string_owned,
8 nullable_string_len, put_compact_nullable_string, put_nullable_string,
9};
10use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
11use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
12use bytes::{Buf, BufMut};
13pub const API_KEY: i16 = 78;
14pub const MIN_VERSION: i16 = 1;
15pub const MAX_VERSION: i16 = 2;
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 ShareFetchRequest {
23 pub group_id: Option<String>,
24 pub member_id: Option<String>,
25 pub share_session_epoch: i32,
26 pub max_wait_ms: i32,
27 pub min_bytes: i32,
28 pub max_bytes: i32,
29 pub max_records: i32,
30 pub batch_size: i32,
31 pub share_acquire_mode: i8,
32 pub is_renew_ack: bool,
33 pub topics: Vec<FetchTopic>,
34 pub forgotten_topics_data: Vec<ForgottenTopic>,
35 pub unknown_tagged_fields: UnknownTaggedFields,
36}
37impl Default for ShareFetchRequest {
38 fn default() -> Self {
39 Self {
40 group_id: None,
41 member_id: None,
42 share_session_epoch: 0i32,
43 max_wait_ms: 0i32,
44 min_bytes: 0i32,
45 max_bytes: 2_147_483_647i32,
46 max_records: 0i32,
47 batch_size: 0i32,
48 share_acquire_mode: 0i8,
49 is_renew_ack: false,
50 topics: Vec::new(),
51 forgotten_topics_data: Vec::new(),
52 unknown_tagged_fields: Default::default(),
53 }
54 }
55}
56impl Encode for ShareFetchRequest {
57 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
58 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
59 return Err(ProtocolError::UnsupportedVersion {
60 api_key: API_KEY,
61 version,
62 });
63 }
64 let flex = is_flexible(version);
65 if version >= 0 {
66 if flex {
67 put_compact_nullable_string(buf, self.group_id.as_deref());
68 } else {
69 put_nullable_string(buf, self.group_id.as_deref());
70 }
71 }
72 if version >= 0 {
73 if flex {
74 put_compact_nullable_string(buf, self.member_id.as_deref());
75 } else {
76 put_nullable_string(buf, self.member_id.as_deref());
77 }
78 }
79 if version >= 0 {
80 put_i32(buf, self.share_session_epoch);
81 }
82 if version >= 0 {
83 put_i32(buf, self.max_wait_ms);
84 }
85 if version >= 0 {
86 put_i32(buf, self.min_bytes);
87 }
88 if version >= 0 {
89 put_i32(buf, self.max_bytes);
90 }
91 if version >= 1 {
92 put_i32(buf, self.max_records);
93 }
94 if version >= 1 {
95 put_i32(buf, self.batch_size);
96 }
97 if version >= 2 {
98 put_i8(buf, self.share_acquire_mode);
99 }
100 if version >= 2 {
101 put_bool(buf, self.is_renew_ack);
102 }
103 if version >= 0 {
104 {
105 crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
106 for it in &self.topics {
107 it.encode(buf, version)?;
108 }
109 }
110 }
111 if version >= 0 {
112 {
113 crate::primitives::array::put_array_len(
114 buf,
115 (self.forgotten_topics_data).len(),
116 flex,
117 );
118 for it in &self.forgotten_topics_data {
119 it.encode(buf, version)?;
120 }
121 }
122 }
123 if flex {
124 let tagged = WriteTaggedFields::new();
125 tagged.write(buf, &self.unknown_tagged_fields);
126 }
127 Ok(())
128 }
129 fn encoded_len(&self, version: i16) -> usize {
130 let flex = is_flexible(version);
131 let mut n: usize = 0;
132 if version >= 0 {
133 n += if flex {
134 compact_nullable_string_len(self.group_id.as_deref())
135 } else {
136 nullable_string_len(self.group_id.as_deref())
137 };
138 }
139 if version >= 0 {
140 n += if flex {
141 compact_nullable_string_len(self.member_id.as_deref())
142 } else {
143 nullable_string_len(self.member_id.as_deref())
144 };
145 }
146 if version >= 0 {
147 n += 4;
148 }
149 if version >= 0 {
150 n += 4;
151 }
152 if version >= 0 {
153 n += 4;
154 }
155 if version >= 0 {
156 n += 4;
157 }
158 if version >= 1 {
159 n += 4;
160 }
161 if version >= 1 {
162 n += 4;
163 }
164 if version >= 2 {
165 n += 1;
166 }
167 if version >= 2 {
168 n += 1;
169 }
170 if version >= 0 {
171 n += {
172 let prefix =
173 crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
174 let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
175 prefix + body
176 };
177 }
178 if version >= 0 {
179 n += {
180 let prefix = crate::primitives::array::array_len_prefix_len(
181 (self.forgotten_topics_data).len(),
182 flex,
183 );
184 let body: usize = (self.forgotten_topics_data)
185 .iter()
186 .map(|it| it.encoded_len(version))
187 .sum();
188 prefix + body
189 };
190 }
191 if flex {
192 let known_pairs: Vec<(u32, usize)> = Vec::new();
193 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
194 }
195 n
196 }
197}
198impl Decode<'_> for ShareFetchRequest {
199 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
200 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
201 return Err(ProtocolError::UnsupportedVersion {
202 api_key: API_KEY,
203 version,
204 });
205 }
206 let flex = is_flexible(version);
207 let mut out = Self::default();
208 if version >= 0 {
209 out.group_id = if flex {
210 get_compact_nullable_string_owned(buf)?
211 } else {
212 get_nullable_string_owned(buf)?
213 };
214 }
215 if version >= 0 {
216 out.member_id = if flex {
217 get_compact_nullable_string_owned(buf)?
218 } else {
219 get_nullable_string_owned(buf)?
220 };
221 }
222 if version >= 0 {
223 out.share_session_epoch = get_i32(buf)?;
224 }
225 if version >= 0 {
226 out.max_wait_ms = get_i32(buf)?;
227 }
228 if version >= 0 {
229 out.min_bytes = get_i32(buf)?;
230 }
231 if version >= 0 {
232 out.max_bytes = get_i32(buf)?;
233 }
234 if version >= 1 {
235 out.max_records = get_i32(buf)?;
236 }
237 if version >= 1 {
238 out.batch_size = get_i32(buf)?;
239 }
240 if version >= 2 {
241 out.share_acquire_mode = get_i8(buf)?;
242 }
243 if version >= 2 {
244 out.is_renew_ack = get_bool(buf)?;
245 }
246 if version >= 0 {
247 out.topics = {
248 let n = crate::primitives::array::get_array_len(buf, flex)?;
249 let mut v = Vec::with_capacity(n);
250 for _ in 0..n {
251 v.push(FetchTopic::decode(buf, version)?);
252 }
253 v
254 };
255 }
256 if version >= 0 {
257 out.forgotten_topics_data = {
258 let n = crate::primitives::array::get_array_len(buf, flex)?;
259 let mut v = Vec::with_capacity(n);
260 for _ in 0..n {
261 v.push(ForgottenTopic::decode(buf, version)?);
262 }
263 v
264 };
265 }
266 if flex {
267 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
268 }
269 Ok(out)
270 }
271}
272#[cfg(test)]
273impl ShareFetchRequest {
274 #[must_use]
275 pub fn populated(version: i16) -> Self {
276 let mut m = Self::default();
277 if version >= 0 {
278 m.group_id = Some("x".to_string());
279 }
280 if version >= 0 {
281 m.member_id = Some("x".to_string());
282 }
283 if version >= 0 {
284 m.share_session_epoch = 1i32;
285 }
286 if version >= 0 {
287 m.max_wait_ms = 1i32;
288 }
289 if version >= 0 {
290 m.min_bytes = 1i32;
291 }
292 if version >= 0 {
293 m.max_bytes = 1i32;
294 }
295 if version >= 1 {
296 m.max_records = 1i32;
297 }
298 if version >= 1 {
299 m.batch_size = 1i32;
300 }
301 if version >= 2 {
302 m.share_acquire_mode = 1i8;
303 }
304 if version >= 2 {
305 m.is_renew_ack = true;
306 }
307 if version >= 0 {
308 m.topics = vec![FetchTopic::populated(version)];
309 }
310 if version >= 0 {
311 m.forgotten_topics_data = vec![ForgottenTopic::populated(version)];
312 }
313 m
314 }
315}
316#[derive(Debug, Clone, PartialEq, Eq, Default)]
317pub struct FetchTopic {
318 pub topic_id: crate::primitives::uuid::Uuid,
319 pub partitions: Vec<FetchPartition>,
320 pub unknown_tagged_fields: UnknownTaggedFields,
321}
322impl Encode for FetchTopic {
323 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
324 let flex = version >= 0;
325 if version >= 0 {
326 crate::primitives::uuid::put_uuid(buf, self.topic_id);
327 }
328 if version >= 0 {
329 {
330 crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
331 for it in &self.partitions {
332 it.encode(buf, version)?;
333 }
334 }
335 }
336 if flex {
337 let tagged = WriteTaggedFields::new();
338 tagged.write(buf, &self.unknown_tagged_fields);
339 }
340 Ok(())
341 }
342 fn encoded_len(&self, version: i16) -> usize {
343 let flex = version >= 0;
344 let mut n: usize = 0;
345 if version >= 0 {
346 n += 16;
347 }
348 if version >= 0 {
349 n += {
350 let prefix =
351 crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
352 let body: usize = (self.partitions)
353 .iter()
354 .map(|it| it.encoded_len(version))
355 .sum();
356 prefix + body
357 };
358 }
359 if flex {
360 let known_pairs: Vec<(u32, usize)> = Vec::new();
361 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
362 }
363 n
364 }
365}
366impl Decode<'_> for FetchTopic {
367 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
368 let flex = version >= 0;
369 let mut out = Self::default();
370 if version >= 0 {
371 out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
372 }
373 if version >= 0 {
374 out.partitions = {
375 let n = crate::primitives::array::get_array_len(buf, flex)?;
376 let mut v = Vec::with_capacity(n);
377 for _ in 0..n {
378 v.push(FetchPartition::decode(buf, version)?);
379 }
380 v
381 };
382 }
383 if flex {
384 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
385 }
386 Ok(out)
387 }
388}
389#[cfg(test)]
390impl FetchTopic {
391 #[must_use]
392 pub fn populated(version: i16) -> Self {
393 let mut m = Self::default();
394 if version >= 0 {
395 m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
396 }
397 if version >= 0 {
398 m.partitions = vec![FetchPartition::populated(version)];
399 }
400 m
401 }
402}
403#[derive(Debug, Clone, PartialEq, Eq, Default)]
404pub struct FetchPartition {
405 pub partition_index: i32,
406 pub partition_max_bytes: i32,
407 pub acknowledgement_batches: Vec<AcknowledgementBatch>,
408 pub unknown_tagged_fields: UnknownTaggedFields,
409}
410impl Encode for FetchPartition {
411 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
412 let flex = version >= 0;
413 if version >= 0 {
414 put_i32(buf, self.partition_index);
415 }
416 if version == 0 {
417 put_i32(buf, self.partition_max_bytes);
418 }
419 if version >= 0 {
420 {
421 crate::primitives::array::put_array_len(
422 buf,
423 (self.acknowledgement_batches).len(),
424 flex,
425 );
426 for it in &self.acknowledgement_batches {
427 it.encode(buf, version)?;
428 }
429 }
430 }
431 if flex {
432 let tagged = WriteTaggedFields::new();
433 tagged.write(buf, &self.unknown_tagged_fields);
434 }
435 Ok(())
436 }
437 fn encoded_len(&self, version: i16) -> usize {
438 let flex = version >= 0;
439 let mut n: usize = 0;
440 if version >= 0 {
441 n += 4;
442 }
443 if version == 0 {
444 n += 4;
445 }
446 if version >= 0 {
447 n += {
448 let prefix = crate::primitives::array::array_len_prefix_len(
449 (self.acknowledgement_batches).len(),
450 flex,
451 );
452 let body: usize = (self.acknowledgement_batches)
453 .iter()
454 .map(|it| it.encoded_len(version))
455 .sum();
456 prefix + body
457 };
458 }
459 if flex {
460 let known_pairs: Vec<(u32, usize)> = Vec::new();
461 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
462 }
463 n
464 }
465}
466impl Decode<'_> for FetchPartition {
467 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
468 let flex = version >= 0;
469 let mut out = Self::default();
470 if version >= 0 {
471 out.partition_index = get_i32(buf)?;
472 }
473 if version == 0 {
474 out.partition_max_bytes = get_i32(buf)?;
475 }
476 if version >= 0 {
477 out.acknowledgement_batches = {
478 let n = crate::primitives::array::get_array_len(buf, flex)?;
479 let mut v = Vec::with_capacity(n);
480 for _ in 0..n {
481 v.push(AcknowledgementBatch::decode(buf, version)?);
482 }
483 v
484 };
485 }
486 if flex {
487 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
488 }
489 Ok(out)
490 }
491}
492#[cfg(test)]
493impl FetchPartition {
494 #[must_use]
495 pub fn populated(version: i16) -> Self {
496 let mut m = Self::default();
497 if version >= 0 {
498 m.partition_index = 1i32;
499 }
500 if version == 0 {
501 m.partition_max_bytes = 1i32;
502 }
503 if version >= 0 {
504 m.acknowledgement_batches = vec![AcknowledgementBatch::populated(version)];
505 }
506 m
507 }
508}
509#[derive(Debug, Clone, PartialEq, Eq, Default)]
510pub struct AcknowledgementBatch {
511 pub first_offset: i64,
512 pub last_offset: i64,
513 pub acknowledge_types: Vec<i8>,
514 pub unknown_tagged_fields: UnknownTaggedFields,
515}
516impl Encode for AcknowledgementBatch {
517 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
518 let flex = version >= 0;
519 if version >= 0 {
520 put_i64(buf, self.first_offset);
521 }
522 if version >= 0 {
523 put_i64(buf, self.last_offset);
524 }
525 if version >= 0 {
526 {
527 crate::primitives::array::put_array_len(buf, (self.acknowledge_types).len(), flex);
528 for it in &self.acknowledge_types {
529 put_i8(buf, *it);
530 }
531 }
532 }
533 if flex {
534 let tagged = WriteTaggedFields::new();
535 tagged.write(buf, &self.unknown_tagged_fields);
536 }
537 Ok(())
538 }
539 fn encoded_len(&self, version: i16) -> usize {
540 let flex = version >= 0;
541 let mut n: usize = 0;
542 if version >= 0 {
543 n += 8;
544 }
545 if version >= 0 {
546 n += 8;
547 }
548 if version >= 0 {
549 n += {
550 let prefix = crate::primitives::array::array_len_prefix_len(
551 (self.acknowledge_types).len(),
552 flex,
553 );
554 let body: usize = (self.acknowledge_types).iter().map(|_| 1).sum();
555 prefix + body
556 };
557 }
558 if flex {
559 let known_pairs: Vec<(u32, usize)> = Vec::new();
560 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
561 }
562 n
563 }
564}
565impl Decode<'_> for AcknowledgementBatch {
566 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
567 let flex = version >= 0;
568 let mut out = Self::default();
569 if version >= 0 {
570 out.first_offset = get_i64(buf)?;
571 }
572 if version >= 0 {
573 out.last_offset = get_i64(buf)?;
574 }
575 if version >= 0 {
576 out.acknowledge_types = {
577 let n = crate::primitives::array::get_array_len(buf, flex)?;
578 let mut v = Vec::with_capacity(n);
579 for _ in 0..n {
580 v.push(get_i8(buf)?);
581 }
582 v
583 };
584 }
585 if flex {
586 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
587 }
588 Ok(out)
589 }
590}
591#[cfg(test)]
592impl AcknowledgementBatch {
593 #[must_use]
594 pub fn populated(version: i16) -> Self {
595 let mut m = Self::default();
596 if version >= 0 {
597 m.first_offset = 1i64;
598 }
599 if version >= 0 {
600 m.last_offset = 1i64;
601 }
602 if version >= 0 {
603 m.acknowledge_types = vec![1i8];
604 }
605 m
606 }
607}
608#[derive(Debug, Clone, PartialEq, Eq, Default)]
609pub struct ForgottenTopic {
610 pub topic_id: crate::primitives::uuid::Uuid,
611 pub partitions: Vec<i32>,
612 pub unknown_tagged_fields: UnknownTaggedFields,
613}
614impl Encode for ForgottenTopic {
615 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
616 let flex = version >= 0;
617 if version >= 0 {
618 crate::primitives::uuid::put_uuid(buf, self.topic_id);
619 }
620 if version >= 0 {
621 {
622 crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
623 for it in &self.partitions {
624 put_i32(buf, *it);
625 }
626 }
627 }
628 if flex {
629 let tagged = WriteTaggedFields::new();
630 tagged.write(buf, &self.unknown_tagged_fields);
631 }
632 Ok(())
633 }
634 fn encoded_len(&self, version: i16) -> usize {
635 let flex = version >= 0;
636 let mut n: usize = 0;
637 if version >= 0 {
638 n += 16;
639 }
640 if version >= 0 {
641 n += {
642 let prefix =
643 crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
644 let body: usize = (self.partitions).iter().map(|_| 4).sum();
645 prefix + body
646 };
647 }
648 if flex {
649 let known_pairs: Vec<(u32, usize)> = Vec::new();
650 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
651 }
652 n
653 }
654}
655impl Decode<'_> for ForgottenTopic {
656 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
657 let flex = version >= 0;
658 let mut out = Self::default();
659 if version >= 0 {
660 out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
661 }
662 if version >= 0 {
663 out.partitions = {
664 let n = crate::primitives::array::get_array_len(buf, flex)?;
665 let mut v = Vec::with_capacity(n);
666 for _ in 0..n {
667 v.push(get_i32(buf)?);
668 }
669 v
670 };
671 }
672 if flex {
673 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
674 }
675 Ok(out)
676 }
677}
678#[cfg(test)]
679impl ForgottenTopic {
680 #[must_use]
681 pub fn populated(version: i16) -> Self {
682 let mut m = Self::default();
683 if version >= 0 {
684 m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
685 }
686 if version >= 0 {
687 m.partitions = vec![1i32];
688 }
689 m
690 }
691}
692#[must_use]
695#[allow(unused_comparisons)]
696pub fn default_json(version: i16) -> ::serde_json::Value {
697 let mut obj = ::serde_json::Map::new();
698 obj.insert("groupId".to_string(), ::serde_json::Value::Null);
699 obj.insert("memberId".to_string(), ::serde_json::Value::Null);
700 obj.insert("shareSessionEpoch".to_string(), ::serde_json::json!(0));
701 obj.insert("maxWaitMs".to_string(), ::serde_json::json!(0));
702 obj.insert("minBytes".to_string(), ::serde_json::json!(0));
703 obj.insert("maxBytes".to_string(), ::serde_json::json!(2147483647));
704 if version >= 1 {
705 obj.insert("maxRecords".to_string(), ::serde_json::json!(0));
706 }
707 if version >= 1 {
708 obj.insert("batchSize".to_string(), ::serde_json::json!(0));
709 }
710 if version >= 2 {
711 obj.insert("shareAcquireMode".to_string(), ::serde_json::json!(0));
712 }
713 if version >= 2 {
714 obj.insert("isRenewAck".to_string(), ::serde_json::Value::Bool(false));
715 }
716 obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
717 obj.insert(
718 "forgottenTopicsData".to_string(),
719 ::serde_json::Value::Array(vec![]),
720 );
721 ::serde_json::Value::Object(obj)
722}
723impl crate::ProtocolRequest for ShareFetchRequest {
724 const API_KEY: i16 = API_KEY;
725 const MIN_VERSION: i16 = MIN_VERSION;
726 const MAX_VERSION: i16 = MAX_VERSION;
727 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
728 type Response = super::share_fetch_response::ShareFetchResponse;
729}