1#![allow(
9 clippy::field_reassign_with_default,
10 clippy::nonminimal_bool,
11 clippy::derivable_impls,
12 clippy::borrow_deref_ref,
13 clippy::double_parens,
14 unused_parens
15)]
16
17use bytes::{Buf, BufMut};
18
19use crate::budget::{Budget, Limits};
20#[allow(unused_imports)]
21use crate::error::{DecodeError, EncodeError};
22use crate::wire::{self, RawTaggedField};
23
24pub fn is_flexible(version: i16) -> bool {
26 let _ = version;
27 version >= 9
28}
29
30#[derive(Debug, Clone, PartialEq)]
32#[non_exhaustive]
33pub struct ProduceResponse {
34 pub responses: Vec<TopicProduceResponse>,
36 pub throttle_time_ms: i32,
38 pub node_endpoints: Option<Vec<NodeEndpoint>>,
41 pub unknown_tagged_fields: Vec<RawTaggedField>,
43}
44
45impl Default for ProduceResponse {
46 fn default() -> Self {
47 Self {
48 responses: Vec::new(),
49 throttle_time_ms: 0,
50 node_endpoints: None,
51 unknown_tagged_fields: Vec::new(),
52 }
53 }
54}
55
56impl ProduceResponse {
57 pub const API_KEY: i16 = 0;
59 pub const MIN_VERSION: i16 = 3;
61 pub const MAX_VERSION: i16 = 13;
63
64 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
66 if is_flexible(version) {
67 wire::put_compact_array_len(buf, Some(self.responses.len()));
68 } else {
69 wire::put_array_len(buf, Some(self.responses.len()))?;
70 }
71 for item in &self.responses {
72 item.encode(buf, version)?;
73 }
74 buf.put_i32(self.throttle_time_ms);
75 if is_flexible(version) {
76 let mut tagged = self.unknown_tagged_fields.clone();
77 if version >= 10 {
78 if let Some(v) = &self.node_endpoints {
79 let mut tag_data = bytes::BytesMut::new();
80 {
81 let buf = &mut tag_data;
82 if is_flexible(version) {
83 wire::put_compact_array_len(buf, Some((*v).len()));
84 } else {
85 wire::put_array_len(buf, Some((*v).len()))?;
86 }
87 for item in &(*v) {
88 item.encode(buf, version)?;
89 }
90 }
91 tagged.push(RawTaggedField {
92 tag: 0,
93 data: tag_data.freeze(),
94 });
95 }
96 }
97 tagged.sort_by_key(|f| f.tag);
98 wire::put_tagged_fields(buf, &tagged);
99 }
100 Ok(())
101 }
102
103 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
106 Self::decode_with_limits(buf, version, Limits::default())
107 }
108
109 pub fn decode_with_limits(
111 buf: &mut impl Buf,
112 version: i16,
113 limits: Limits,
114 ) -> Result<Self, DecodeError> {
115 let mut budget = limits.budget(buf.remaining());
116 Self::decode_with_budget(buf, version, &mut budget)
117 }
118
119 pub fn decode_with_budget(
121 buf: &mut impl Buf,
122 version: i16,
123 budget: &mut Budget,
124 ) -> Result<Self, DecodeError> {
125 let mut this = Self::default();
126 this.responses = {
127 let len = if is_flexible(version) {
128 wire::get_compact_array_len(buf)?
129 } else {
130 wire::get_array_len(buf)?
131 };
132 match len {
133 None => return Err(DecodeError::InvalidLength(-1)),
134 Some(n) => {
135 let mut items = Vec::new();
136 for _ in 0..n {
137 let mark = buf.remaining();
138 let item = TopicProduceResponse::decode_with_budget(buf, version, budget)?;
139 budget.progress(mark, buf.remaining())?;
140 budget.push(&mut items, item)?;
141 }
142 items
143 }
144 }
145 };
146 this.throttle_time_ms = wire::get_i32(buf)?;
147 if is_flexible(version) {
148 for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
149 if raw.tag == 0 && (version >= 10) {
150 let mut tag_data = raw.data;
151 let buf = &mut tag_data;
152 this.node_endpoints = Some({
153 let len = if is_flexible(version) {
154 wire::get_compact_array_len(buf)?
155 } else {
156 wire::get_array_len(buf)?
157 };
158 match len {
159 None => return Err(DecodeError::InvalidLength(-1)),
160 Some(n) => {
161 let mut items = Vec::new();
162 for _ in 0..n {
163 let mark = buf.remaining();
164 let item =
165 NodeEndpoint::decode_with_budget(buf, version, budget)?;
166 budget.progress(mark, buf.remaining())?;
167 budget.push(&mut items, item)?;
168 }
169 items
170 }
171 }
172 });
173 if buf.has_remaining() {
174 return Err(DecodeError::InvalidLength(
175 i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
176 ));
177 }
178 } else {
179 budget.push(&mut this.unknown_tagged_fields, raw)?;
180 }
181 }
182 }
183 Ok(this)
184 }
185}
186
187impl crate::Message for ProduceResponse {
188 const API_KEY: i16 = 0;
189 const MIN_VERSION: i16 = 3;
190 const MAX_VERSION: i16 = 13;
191
192 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
193 ProduceResponse::encode(self, buf, version)
194 }
195 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
196 ProduceResponse::decode(buf, version)
197 }
198 fn decode_with_limits(
199 buf: &mut impl Buf,
200 version: i16,
201 limits: Limits,
202 ) -> Result<Self, DecodeError> {
203 ProduceResponse::decode_with_limits(buf, version, limits)
204 }
205 fn throttle_time_ms(&self) -> Option<i32> {
206 Some(self.throttle_time_ms)
207 }
208}
209
210#[derive(Debug, Clone, PartialEq)]
212#[non_exhaustive]
213pub struct TopicProduceResponse {
214 pub name: String,
216 pub topic_id: [u8; 16],
218 pub partition_responses: Vec<PartitionProduceResponse>,
220 pub unknown_tagged_fields: Vec<RawTaggedField>,
222}
223
224impl Default for TopicProduceResponse {
225 fn default() -> Self {
226 Self {
227 name: String::new(),
228 topic_id: [0u8; 16],
229 partition_responses: Vec::new(),
230 unknown_tagged_fields: Vec::new(),
231 }
232 }
233}
234
235impl TopicProduceResponse {
236 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
238 if version <= 12 {
239 if is_flexible(version) {
240 wire::put_compact_string(buf, &self.name);
241 } else {
242 wire::put_string(buf, &self.name)?;
243 }
244 }
245 if version >= 13 {
246 wire::put_uuid(buf, self.topic_id);
247 }
248 if is_flexible(version) {
249 wire::put_compact_array_len(buf, Some(self.partition_responses.len()));
250 } else {
251 wire::put_array_len(buf, Some(self.partition_responses.len()))?;
252 }
253 for item in &self.partition_responses {
254 item.encode(buf, version)?;
255 }
256 if is_flexible(version) {
257 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
258 }
259 Ok(())
260 }
261
262 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
265 Self::decode_with_limits(buf, version, Limits::default())
266 }
267
268 pub fn decode_with_limits(
270 buf: &mut impl Buf,
271 version: i16,
272 limits: Limits,
273 ) -> Result<Self, DecodeError> {
274 let mut budget = limits.budget(buf.remaining());
275 Self::decode_with_budget(buf, version, &mut budget)
276 }
277
278 pub fn decode_with_budget(
280 buf: &mut impl Buf,
281 version: i16,
282 budget: &mut Budget,
283 ) -> Result<Self, DecodeError> {
284 let mut this = Self::default();
285 if version <= 12 {
286 this.name = if is_flexible(version) {
287 wire::get_compact_string(buf)?
288 } else {
289 wire::get_string(buf)?
290 };
291 }
292 if version >= 13 {
293 this.topic_id = wire::get_uuid(buf)?;
294 }
295 this.partition_responses = {
296 let len = if is_flexible(version) {
297 wire::get_compact_array_len(buf)?
298 } else {
299 wire::get_array_len(buf)?
300 };
301 match len {
302 None => return Err(DecodeError::InvalidLength(-1)),
303 Some(n) => {
304 let mut items = Vec::new();
305 for _ in 0..n {
306 let mark = buf.remaining();
307 let item =
308 PartitionProduceResponse::decode_with_budget(buf, version, budget)?;
309 budget.progress(mark, buf.remaining())?;
310 budget.push(&mut items, item)?;
311 }
312 items
313 }
314 }
315 };
316 if is_flexible(version) {
317 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
318 }
319 Ok(this)
320 }
321}
322
323#[derive(Debug, Clone, PartialEq)]
325#[non_exhaustive]
326pub struct PartitionProduceResponse {
327 pub index: i32,
329 pub error_code: i16,
331 pub base_offset: i64,
333 pub log_append_time_ms: i64,
335 pub log_start_offset: i64,
337 pub record_errors: Vec<BatchIndexAndErrorMessage>,
339 pub error_message: Option<String>,
341 pub current_leader: Option<LeaderIdAndEpoch>,
344 pub unknown_tagged_fields: Vec<RawTaggedField>,
346}
347
348impl Default for PartitionProduceResponse {
349 fn default() -> Self {
350 Self {
351 index: 0,
352 error_code: 0,
353 base_offset: 0,
354 log_append_time_ms: -1,
355 log_start_offset: -1,
356 record_errors: Vec::new(),
357 error_message: None,
358 current_leader: None,
359 unknown_tagged_fields: Vec::new(),
360 }
361 }
362}
363
364impl PartitionProduceResponse {
365 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
367 buf.put_i32(self.index);
368 buf.put_i16(self.error_code);
369 buf.put_i64(self.base_offset);
370 buf.put_i64(self.log_append_time_ms);
371 if version >= 5 {
372 buf.put_i64(self.log_start_offset);
373 }
374 if version >= 8 {
375 if is_flexible(version) {
376 wire::put_compact_array_len(buf, Some(self.record_errors.len()));
377 } else {
378 wire::put_array_len(buf, Some(self.record_errors.len()))?;
379 }
380 for item in &self.record_errors {
381 item.encode(buf, version)?;
382 }
383 }
384 if version >= 8 {
385 if is_flexible(version) {
386 wire::put_compact_nullable_string(buf, self.error_message.as_deref());
387 } else {
388 wire::put_nullable_string(buf, self.error_message.as_deref())?;
389 }
390 }
391 if is_flexible(version) {
392 let mut tagged = self.unknown_tagged_fields.clone();
393 if version >= 10 {
394 if let Some(v) = &self.current_leader {
395 let mut tag_data = bytes::BytesMut::new();
396 {
397 let buf = &mut tag_data;
398 (*v).encode(buf, version)?;
399 }
400 tagged.push(RawTaggedField {
401 tag: 0,
402 data: tag_data.freeze(),
403 });
404 }
405 }
406 tagged.sort_by_key(|f| f.tag);
407 wire::put_tagged_fields(buf, &tagged);
408 }
409 Ok(())
410 }
411
412 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
415 Self::decode_with_limits(buf, version, Limits::default())
416 }
417
418 pub fn decode_with_limits(
420 buf: &mut impl Buf,
421 version: i16,
422 limits: Limits,
423 ) -> Result<Self, DecodeError> {
424 let mut budget = limits.budget(buf.remaining());
425 Self::decode_with_budget(buf, version, &mut budget)
426 }
427
428 pub fn decode_with_budget(
430 buf: &mut impl Buf,
431 version: i16,
432 budget: &mut Budget,
433 ) -> Result<Self, DecodeError> {
434 let mut this = Self::default();
435 this.index = wire::get_i32(buf)?;
436 this.error_code = wire::get_i16(buf)?;
437 this.base_offset = wire::get_i64(buf)?;
438 this.log_append_time_ms = wire::get_i64(buf)?;
439 if version >= 5 {
440 this.log_start_offset = wire::get_i64(buf)?;
441 }
442 if version >= 8 {
443 this.record_errors = {
444 let len = if is_flexible(version) {
445 wire::get_compact_array_len(buf)?
446 } else {
447 wire::get_array_len(buf)?
448 };
449 match len {
450 None => return Err(DecodeError::InvalidLength(-1)),
451 Some(n) => {
452 let mut items = Vec::new();
453 for _ in 0..n {
454 let mark = buf.remaining();
455 let item = BatchIndexAndErrorMessage::decode_with_budget(
456 buf, version, budget,
457 )?;
458 budget.progress(mark, buf.remaining())?;
459 budget.push(&mut items, item)?;
460 }
461 items
462 }
463 }
464 };
465 }
466 if version >= 8 {
467 this.error_message = if is_flexible(version) {
468 wire::get_compact_nullable_string(buf)?
469 } else {
470 wire::get_nullable_string(buf)?
471 };
472 }
473 if is_flexible(version) {
474 for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
475 if raw.tag == 0 && (version >= 10) {
476 let mut tag_data = raw.data;
477 let buf = &mut tag_data;
478 this.current_leader =
479 Some(LeaderIdAndEpoch::decode_with_budget(buf, version, budget)?);
480 if buf.has_remaining() {
481 return Err(DecodeError::InvalidLength(
482 i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
483 ));
484 }
485 } else {
486 budget.push(&mut this.unknown_tagged_fields, raw)?;
487 }
488 }
489 }
490 Ok(this)
491 }
492}
493
494#[derive(Debug, Clone, PartialEq)]
496#[non_exhaustive]
497pub struct BatchIndexAndErrorMessage {
498 pub batch_index: i32,
500 pub batch_index_error_message: Option<String>,
502 pub unknown_tagged_fields: Vec<RawTaggedField>,
504}
505
506impl Default for BatchIndexAndErrorMessage {
507 fn default() -> Self {
508 Self {
509 batch_index: 0,
510 batch_index_error_message: None,
511 unknown_tagged_fields: Vec::new(),
512 }
513 }
514}
515
516impl BatchIndexAndErrorMessage {
517 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
519 if version >= 8 {
520 buf.put_i32(self.batch_index);
521 }
522 if version >= 8 {
523 if is_flexible(version) {
524 wire::put_compact_nullable_string(buf, self.batch_index_error_message.as_deref());
525 } else {
526 wire::put_nullable_string(buf, self.batch_index_error_message.as_deref())?;
527 }
528 }
529 if is_flexible(version) {
530 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
531 }
532 Ok(())
533 }
534
535 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
538 Self::decode_with_limits(buf, version, Limits::default())
539 }
540
541 pub fn decode_with_limits(
543 buf: &mut impl Buf,
544 version: i16,
545 limits: Limits,
546 ) -> Result<Self, DecodeError> {
547 let mut budget = limits.budget(buf.remaining());
548 Self::decode_with_budget(buf, version, &mut budget)
549 }
550
551 pub fn decode_with_budget(
553 buf: &mut impl Buf,
554 version: i16,
555 budget: &mut Budget,
556 ) -> Result<Self, DecodeError> {
557 let mut this = Self::default();
558 if version >= 8 {
559 this.batch_index = wire::get_i32(buf)?;
560 }
561 if version >= 8 {
562 this.batch_index_error_message = if is_flexible(version) {
563 wire::get_compact_nullable_string(buf)?
564 } else {
565 wire::get_nullable_string(buf)?
566 };
567 }
568 if is_flexible(version) {
569 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
570 }
571 Ok(this)
572 }
573}
574
575#[derive(Debug, Clone, PartialEq)]
577#[non_exhaustive]
578pub struct LeaderIdAndEpoch {
579 pub leader_id: i32,
581 pub leader_epoch: i32,
583 pub unknown_tagged_fields: Vec<RawTaggedField>,
585}
586
587impl Default for LeaderIdAndEpoch {
588 fn default() -> Self {
589 Self {
590 leader_id: -1,
591 leader_epoch: -1,
592 unknown_tagged_fields: Vec::new(),
593 }
594 }
595}
596
597impl LeaderIdAndEpoch {
598 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
600 if version >= 10 {
601 buf.put_i32(self.leader_id);
602 }
603 if version >= 10 {
604 buf.put_i32(self.leader_epoch);
605 }
606 if is_flexible(version) {
607 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
608 }
609 Ok(())
610 }
611
612 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
615 Self::decode_with_limits(buf, version, Limits::default())
616 }
617
618 pub fn decode_with_limits(
620 buf: &mut impl Buf,
621 version: i16,
622 limits: Limits,
623 ) -> Result<Self, DecodeError> {
624 let mut budget = limits.budget(buf.remaining());
625 Self::decode_with_budget(buf, version, &mut budget)
626 }
627
628 pub fn decode_with_budget(
630 buf: &mut impl Buf,
631 version: i16,
632 budget: &mut Budget,
633 ) -> Result<Self, DecodeError> {
634 let mut this = Self::default();
635 if version >= 10 {
636 this.leader_id = wire::get_i32(buf)?;
637 }
638 if version >= 10 {
639 this.leader_epoch = wire::get_i32(buf)?;
640 }
641 if is_flexible(version) {
642 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
643 }
644 Ok(this)
645 }
646}
647
648#[derive(Debug, Clone, PartialEq)]
650#[non_exhaustive]
651pub struct NodeEndpoint {
652 pub node_id: i32,
654 pub host: String,
656 pub port: i32,
658 pub rack: Option<String>,
660 pub unknown_tagged_fields: Vec<RawTaggedField>,
662}
663
664impl Default for NodeEndpoint {
665 fn default() -> Self {
666 Self {
667 node_id: 0,
668 host: String::new(),
669 port: 0,
670 rack: None,
671 unknown_tagged_fields: Vec::new(),
672 }
673 }
674}
675
676impl NodeEndpoint {
677 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
679 if version >= 10 {
680 buf.put_i32(self.node_id);
681 }
682 if version >= 10 {
683 if is_flexible(version) {
684 wire::put_compact_string(buf, &self.host);
685 } else {
686 wire::put_string(buf, &self.host)?;
687 }
688 }
689 if version >= 10 {
690 buf.put_i32(self.port);
691 }
692 if version >= 10 {
693 if is_flexible(version) {
694 wire::put_compact_nullable_string(buf, self.rack.as_deref());
695 } else {
696 wire::put_nullable_string(buf, self.rack.as_deref())?;
697 }
698 }
699 if is_flexible(version) {
700 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
701 }
702 Ok(())
703 }
704
705 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
708 Self::decode_with_limits(buf, version, Limits::default())
709 }
710
711 pub fn decode_with_limits(
713 buf: &mut impl Buf,
714 version: i16,
715 limits: Limits,
716 ) -> Result<Self, DecodeError> {
717 let mut budget = limits.budget(buf.remaining());
718 Self::decode_with_budget(buf, version, &mut budget)
719 }
720
721 pub fn decode_with_budget(
723 buf: &mut impl Buf,
724 version: i16,
725 budget: &mut Budget,
726 ) -> Result<Self, DecodeError> {
727 let mut this = Self::default();
728 if version >= 10 {
729 this.node_id = wire::get_i32(buf)?;
730 }
731 if version >= 10 {
732 this.host = if is_flexible(version) {
733 wire::get_compact_string(buf)?
734 } else {
735 wire::get_string(buf)?
736 };
737 }
738 if version >= 10 {
739 this.port = wire::get_i32(buf)?;
740 }
741 if version >= 10 {
742 this.rack = if is_flexible(version) {
743 wire::get_compact_nullable_string(buf)?
744 } else {
745 wire::get_nullable_string(buf)?
746 };
747 }
748 if is_flexible(version) {
749 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
750 }
751 Ok(this)
752 }
753}