1use bytes::BufMut;
3
4use crate::{
5 Decode, DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6 codegen_helpers::is_default,
7 owned::produce_response,
8 primitives::{
9 array::{array_len_prefix_len, get_array_len, put_array_len},
10 fixed::{get_i16, get_i32, get_i64, put_i16, put_i32, put_i64},
11 string_bytes::{
12 compact_nullable_string_len, compact_string_len, nullable_string_len,
13 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
14 string_len,
15 },
16 string_bytes_borrowed::{
17 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
18 get_nullable_string_borrowed, get_string_borrowed,
19 },
20 uuid::{Uuid, get_uuid, put_uuid},
21 },
22 tagged_fields::{WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len},
23};
24pub const API_KEY: i16 = 0;
25pub const MIN_VERSION: i16 = 3;
26pub const MAX_VERSION: i16 = 13;
27pub const FLEXIBLE_MIN: i16 = 9;
28#[inline]
29#[must_use]
30pub fn is_flexible(version: i16) -> bool {
31 version >= FLEXIBLE_MIN
32}
33#[derive(Debug, Clone, PartialEq, Eq, Default)]
34pub struct ProduceResponse<'a> {
35 pub responses: Vec<TopicProduceResponse<'a>>,
36 pub throttle_time_ms: i32,
37 pub node_endpoints: Vec<produce_response::NodeEndpoint>,
38 pub unknown_tagged_fields: UnknownTaggedFields,
39}
40impl ProduceResponse<'_> {
41 #[must_use]
42 pub fn to_owned(&self) -> produce_response::ProduceResponse {
43 produce_response::ProduceResponse {
44 responses: (self.responses)
45 .iter()
46 .map(TopicProduceResponse::to_owned)
47 .collect(),
48 throttle_time_ms: (self.throttle_time_ms),
49 node_endpoints: self.node_endpoints.clone(),
50 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
51 }
52 }
53}
54impl Encode for ProduceResponse<'_> {
55 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
56 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
57 return Err(ProtocolError::UnsupportedVersion {
58 api_key: API_KEY,
59 version,
60 });
61 }
62 let flex = is_flexible(version);
63 if version >= 0 {
64 {
65 put_array_len(buf, (self.responses).len(), flex)?;
66 for it in &self.responses {
67 it.encode(buf, version)?;
68 }
69 }
70 }
71 if version >= 1 {
72 put_i32(buf, self.throttle_time_ms);
73 }
74 if flex {
75 let mut tagged = WriteTaggedFields::new();
76 if version >= 10 && !(is_default(&self.node_endpoints)) {
77 let payload = encode_to_bytes(
78 {
79 let prefix = array_len_prefix_len((self.node_endpoints).len(), flex);
80 let body: usize = (self.node_endpoints)
81 .iter()
82 .map(|it| it.encoded_len(version))
83 .sum();
84 prefix + body
85 },
86 |b| {
87 {
88 put_array_len(b, (self.node_endpoints).len(), flex)?;
89 for it in &self.node_endpoints {
90 it.encode(b, version)?;
91 }
92 }
93 Ok(())
94 },
95 )?;
96 tagged.add(0, payload);
97 }
98 tagged.write(buf, &self.unknown_tagged_fields)?;
99 }
100 Ok(())
101 }
102 fn encoded_len(&self, version: i16) -> usize {
103 let flex = is_flexible(version);
104 let mut n: usize = 0;
105 if version >= 0 {
106 n += {
107 let prefix = array_len_prefix_len((self.responses).len(), flex);
108 let body: usize = (self.responses)
109 .iter()
110 .map(|it| it.encoded_len(version))
111 .sum();
112 prefix + body
113 };
114 }
115 if version >= 1 {
116 n += 4;
117 }
118 if flex {
119 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
120 if version >= 10 && !(is_default(&self.node_endpoints)) {
121 known_pairs.push((0, {
122 let prefix = array_len_prefix_len((self.node_endpoints).len(), flex);
123 let body: usize = (self.node_endpoints)
124 .iter()
125 .map(|it| it.encoded_len(version))
126 .sum();
127 prefix + body
128 }));
129 }
130 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
131 }
132 n
133 }
134}
135impl<'de> DecodeBorrow<'de> for ProduceResponse<'de> {
136 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
137 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
138 return Err(ProtocolError::UnsupportedVersion {
139 api_key: API_KEY,
140 version,
141 });
142 }
143 let flex = is_flexible(version);
144 let mut out = Self::default();
145 if version >= 0 {
146 out.responses = {
147 let n = get_array_len(buf, flex)?;
148 let mut v = Vec::with_capacity(n);
149 for _ in 0..n {
150 v.push(TopicProduceResponse::decode_borrow(buf, version)?);
151 }
152 v
153 };
154 }
155 if version >= 1 {
156 out.throttle_time_ms = get_i32(buf)?;
157 }
158 if flex {
159 let mut tag_node_endpoints = None;
160 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
161 0 if version >= 10 => {
162 tag_node_endpoints = Some({
163 let b: &mut &[u8] = payload;
164 {
165 let n = get_array_len(b, flex)?;
166 let mut v = Vec::with_capacity(n);
167 for _ in 0..n {
168 v.push(produce_response::NodeEndpoint::decode(b, version)?);
169 }
170 v
171 }
172 });
173 Ok(true)
174 }
175 0 => Err(ProtocolError::TagNotValidForVersion { tag: 0, version }),
176 _ => Ok(false),
177 })?;
178 if let Some(v) = tag_node_endpoints {
179 out.node_endpoints = v;
180 }
181 }
182 Ok(out)
183 }
184}
185#[cfg(test)]
186impl ProduceResponse<'_> {
187 #[must_use]
188 pub fn populated(version: i16) -> Self {
189 let mut m = Self::default();
190 if version >= 0 {
191 m.responses = vec![TopicProduceResponse::populated(version)];
192 }
193 if version >= 1 {
194 m.throttle_time_ms = 1i32;
195 }
196 if version >= 10 {
197 m.node_endpoints = vec![produce_response::NodeEndpoint::populated(version)];
198 }
199 m
200 }
201}
202#[derive(Debug, Clone, PartialEq, Eq, Default)]
203pub struct TopicProduceResponse<'a> {
204 pub name: &'a str,
205 pub topic_id: Uuid,
206 pub partition_responses: Vec<PartitionProduceResponse<'a>>,
207 pub unknown_tagged_fields: UnknownTaggedFields,
208}
209impl TopicProduceResponse<'_> {
210 #[must_use]
211 pub fn to_owned(&self) -> produce_response::TopicProduceResponse {
212 produce_response::TopicProduceResponse {
213 name: (self.name).to_owned(),
214 topic_id: (self.topic_id),
215 partition_responses: (self.partition_responses)
216 .iter()
217 .map(PartitionProduceResponse::to_owned)
218 .collect(),
219 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
220 }
221 }
222}
223impl Encode for TopicProduceResponse<'_> {
224 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
225 let flex = version >= 9;
226 if (0..=12).contains(&version) {
227 if flex {
228 put_compact_string(buf, self.name)?;
229 } else {
230 put_string(buf, self.name)?;
231 }
232 }
233 if version >= 13 {
234 put_uuid(buf, self.topic_id);
235 }
236 if version >= 0 {
237 {
238 put_array_len(buf, (self.partition_responses).len(), flex)?;
239 for it in &self.partition_responses {
240 it.encode(buf, version)?;
241 }
242 }
243 }
244 if flex {
245 let tagged = WriteTaggedFields::new();
246 tagged.write(buf, &self.unknown_tagged_fields)?;
247 }
248 Ok(())
249 }
250 fn encoded_len(&self, version: i16) -> usize {
251 let flex = version >= 9;
252 let mut n: usize = 0;
253 if (0..=12).contains(&version) {
254 n += if flex {
255 compact_string_len(self.name)
256 } else {
257 string_len(self.name)
258 };
259 }
260 if version >= 13 {
261 n += 16;
262 }
263 if version >= 0 {
264 n += {
265 let prefix = array_len_prefix_len((self.partition_responses).len(), flex);
266 let body: usize = (self.partition_responses)
267 .iter()
268 .map(|it| it.encoded_len(version))
269 .sum();
270 prefix + body
271 };
272 }
273 if flex {
274 let known_pairs: Vec<(u32, usize)> = Vec::new();
275 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
276 }
277 n
278 }
279}
280impl<'de> DecodeBorrow<'de> for TopicProduceResponse<'de> {
281 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
282 let flex = version >= 9;
283 let mut out = Self::default();
284 if (0..=12).contains(&version) {
285 out.name = if flex {
286 get_compact_string_borrowed(buf)?
287 } else {
288 get_string_borrowed(buf)?
289 };
290 }
291 if version >= 13 {
292 out.topic_id = get_uuid(buf)?;
293 }
294 if version >= 0 {
295 out.partition_responses = {
296 let n = get_array_len(buf, flex)?;
297 let mut v = Vec::with_capacity(n);
298 for _ in 0..n {
299 v.push(PartitionProduceResponse::decode_borrow(buf, version)?);
300 }
301 v
302 };
303 }
304 if flex {
305 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
306 }
307 Ok(out)
308 }
309}
310#[cfg(test)]
311impl TopicProduceResponse<'_> {
312 #[must_use]
313 pub fn populated(version: i16) -> Self {
314 let mut m = Self::default();
315 if (0..=12).contains(&version) {
316 m.name = "x";
317 }
318 if version >= 13 {
319 m.topic_id = Uuid([1u8; 16]);
320 }
321 if version >= 0 {
322 m.partition_responses = vec![PartitionProduceResponse::populated(version)];
323 }
324 m
325 }
326}
327#[derive(Debug, Clone, PartialEq, Eq)]
328pub struct PartitionProduceResponse<'a> {
329 pub index: i32,
330 pub error_code: i16,
331 pub base_offset: i64,
332 pub log_append_time_ms: i64,
333 pub log_start_offset: i64,
334 pub record_errors: Vec<BatchIndexAndErrorMessage<'a>>,
335 pub error_message: Option<&'a str>,
336 pub current_leader: LeaderIdAndEpoch,
337 pub unknown_tagged_fields: UnknownTaggedFields,
338}
339impl Default for PartitionProduceResponse<'_> {
340 fn default() -> Self {
341 Self {
342 index: 0i32,
343 error_code: 0i16,
344 base_offset: 0i64,
345 log_append_time_ms: -1i64,
346 log_start_offset: -1i64,
347 record_errors: Vec::new(),
348 error_message: None,
349 current_leader: <LeaderIdAndEpoch>::default(),
350 unknown_tagged_fields: UnknownTaggedFields::default(),
351 }
352 }
353}
354impl<'a> PartitionProduceResponse<'a> {
355 #[must_use]
356 pub fn to_owned(&self) -> produce_response::PartitionProduceResponse {
357 produce_response::PartitionProduceResponse {
358 index: (self.index),
359 error_code: (self.error_code),
360 base_offset: (self.base_offset),
361 log_append_time_ms: (self.log_append_time_ms),
362 log_start_offset: (self.log_start_offset),
363 record_errors: (self.record_errors)
364 .iter()
365 .map(BatchIndexAndErrorMessage::to_owned)
366 .collect(),
367 error_message: (self.error_message).map(str::to_owned),
368 current_leader: (self.current_leader).to_owned(),
369 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
370 }
371 }
372 fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
373 if version >= 0 {
374 put_i32(buf, self.index);
375 }
376 }
377 fn encode_field_1<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
378 if version >= 0 {
379 put_i16(buf, self.error_code);
380 }
381 }
382 fn encode_field_2<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
383 if version >= 0 {
384 put_i64(buf, self.base_offset);
385 }
386 }
387 fn encode_field_3<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
388 if version >= 2 {
389 put_i64(buf, self.log_append_time_ms);
390 }
391 }
392 fn encode_field_4<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
393 if version >= 5 {
394 put_i64(buf, self.log_start_offset);
395 }
396 }
397 fn encode_field_5<B: BufMut>(
398 &self,
399 buf: &mut B,
400 version: i16,
401 flex: bool,
402 ) -> Result<(), ProtocolError> {
403 if version >= 8 {
404 {
405 put_array_len(buf, (self.record_errors).len(), flex)?;
406 for it in &self.record_errors {
407 it.encode(buf, version)?;
408 }
409 }
410 }
411 Ok(())
412 }
413 fn encode_field_6<B: BufMut>(
414 &self,
415 buf: &mut B,
416 version: i16,
417 flex: bool,
418 ) -> Result<(), ProtocolError> {
419 if version >= 8 {
420 if flex {
421 put_compact_nullable_string(buf, self.error_message)?;
422 } else {
423 put_nullable_string(buf, self.error_message)?;
424 }
425 }
426 Ok(())
427 }
428 fn encode_tagged_fields<B: BufMut>(
429 &self,
430 buf: &mut B,
431 version: i16,
432 flex: bool,
433 ) -> Result<(), ProtocolError> {
434 if flex {
435 let mut tagged = WriteTaggedFields::new();
436 if version >= 10 && !(is_default(&self.current_leader)) {
437 let payload = encode_to_bytes(self.current_leader.encoded_len(version), |b| {
438 self.current_leader.encode(b, version)?;
439 Ok(())
440 })?;
441 tagged.add(0, payload);
442 }
443 tagged.write(buf, &self.unknown_tagged_fields)?;
444 }
445 Ok(())
446 }
447 fn decode_field_0(
448 out: &mut Self,
449 buf: &mut &'a [u8],
450 version: i16,
451 _flex: bool,
452 ) -> Result<(), ProtocolError> {
453 if version >= 0 {
454 out.index = get_i32(buf)?;
455 }
456 Ok(())
457 }
458 fn decode_field_1(
459 out: &mut Self,
460 buf: &mut &'a [u8],
461 version: i16,
462 _flex: bool,
463 ) -> Result<(), ProtocolError> {
464 if version >= 0 {
465 out.error_code = get_i16(buf)?;
466 }
467 Ok(())
468 }
469 fn decode_field_2(
470 out: &mut Self,
471 buf: &mut &'a [u8],
472 version: i16,
473 _flex: bool,
474 ) -> Result<(), ProtocolError> {
475 if version >= 0 {
476 out.base_offset = get_i64(buf)?;
477 }
478 Ok(())
479 }
480 fn decode_field_3(
481 out: &mut Self,
482 buf: &mut &'a [u8],
483 version: i16,
484 _flex: bool,
485 ) -> Result<(), ProtocolError> {
486 if version >= 2 {
487 out.log_append_time_ms = get_i64(buf)?;
488 }
489 Ok(())
490 }
491 fn decode_field_4(
492 out: &mut Self,
493 buf: &mut &'a [u8],
494 version: i16,
495 _flex: bool,
496 ) -> Result<(), ProtocolError> {
497 if version >= 5 {
498 out.log_start_offset = get_i64(buf)?;
499 }
500 Ok(())
501 }
502 fn decode_field_5(
503 out: &mut Self,
504 buf: &mut &'a [u8],
505 version: i16,
506 flex: bool,
507 ) -> Result<(), ProtocolError> {
508 if version >= 8 {
509 out.record_errors = {
510 let n = get_array_len(buf, flex)?;
511 let mut v = Vec::with_capacity(n);
512 for _ in 0..n {
513 v.push(BatchIndexAndErrorMessage::decode_borrow(buf, version)?);
514 }
515 v
516 };
517 }
518 Ok(())
519 }
520 fn decode_field_6(
521 out: &mut Self,
522 buf: &mut &'a [u8],
523 version: i16,
524 flex: bool,
525 ) -> Result<(), ProtocolError> {
526 if version >= 8 {
527 out.error_message = if flex {
528 get_compact_nullable_string_borrowed(buf)?
529 } else {
530 get_nullable_string_borrowed(buf)?
531 };
532 }
533 Ok(())
534 }
535 fn decode_tagged_fields(
536 out: &mut Self,
537 buf: &mut &'a [u8],
538 version: i16,
539 flex: bool,
540 ) -> Result<(), ProtocolError> {
541 if flex {
542 let mut tag_current_leader = None;
543 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
544 0 if version >= 10 => {
545 tag_current_leader = Some({
546 let b: &mut &[u8] = payload;
547 LeaderIdAndEpoch::decode_borrow(b, version)?
548 });
549 Ok(true)
550 }
551 0 => Err(ProtocolError::TagNotValidForVersion { tag: 0, version }),
552 _ => Ok(false),
553 })?;
554 if let Some(v) = tag_current_leader {
555 out.current_leader = v;
556 }
557 }
558 Ok(())
559 }
560}
561impl Encode for PartitionProduceResponse<'_> {
562 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
563 let flex = version >= 9;
564 self.encode_field_0(buf, version, flex);
565 self.encode_field_1(buf, version, flex);
566 self.encode_field_2(buf, version, flex);
567 self.encode_field_3(buf, version, flex);
568 self.encode_field_4(buf, version, flex);
569 self.encode_field_5(buf, version, flex)?;
570 self.encode_field_6(buf, version, flex)?;
571 self.encode_tagged_fields(buf, version, flex)?;
572 Ok(())
573 }
574 fn encoded_len(&self, version: i16) -> usize {
575 let flex = version >= 9;
576 let mut n: usize = 0;
577 if version >= 0 {
578 n += 4;
579 }
580 if version >= 0 {
581 n += 2;
582 }
583 if version >= 0 {
584 n += 8;
585 }
586 if version >= 2 {
587 n += 8;
588 }
589 if version >= 5 {
590 n += 8;
591 }
592 if version >= 8 {
593 n += {
594 let prefix = array_len_prefix_len((self.record_errors).len(), flex);
595 let body: usize = (self.record_errors)
596 .iter()
597 .map(|it| it.encoded_len(version))
598 .sum();
599 prefix + body
600 };
601 }
602 if version >= 8 {
603 n += if flex {
604 compact_nullable_string_len(self.error_message)
605 } else {
606 nullable_string_len(self.error_message)
607 };
608 }
609 if flex {
610 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
611 if version >= 10 && !(is_default(&self.current_leader)) {
612 known_pairs.push((0, self.current_leader.encoded_len(version)));
613 }
614 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
615 }
616 n
617 }
618}
619impl<'de> DecodeBorrow<'de> for PartitionProduceResponse<'de> {
620 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
621 let flex = version >= 9;
622 let mut out = Self::default();
623 Self::decode_field_0(&mut out, buf, version, flex)?;
624 Self::decode_field_1(&mut out, buf, version, flex)?;
625 Self::decode_field_2(&mut out, buf, version, flex)?;
626 Self::decode_field_3(&mut out, buf, version, flex)?;
627 Self::decode_field_4(&mut out, buf, version, flex)?;
628 Self::decode_field_5(&mut out, buf, version, flex)?;
629 Self::decode_field_6(&mut out, buf, version, flex)?;
630 Self::decode_tagged_fields(&mut out, buf, version, flex)?;
631 Ok(out)
632 }
633}
634#[cfg(test)]
635impl PartitionProduceResponse<'_> {
636 #[must_use]
637 pub fn populated(version: i16) -> Self {
638 let mut m = Self::default();
639 if version >= 0 {
640 m.index = 1i32;
641 }
642 if version >= 0 {
643 m.error_code = 1i16;
644 }
645 if version >= 0 {
646 m.base_offset = 1i64;
647 }
648 if version >= 2 {
649 m.log_append_time_ms = 1i64;
650 }
651 if version >= 5 {
652 m.log_start_offset = 1i64;
653 }
654 if version >= 8 {
655 m.record_errors = vec![BatchIndexAndErrorMessage::populated(version)];
656 }
657 if version >= 8 {
658 m.error_message = Some("x");
659 }
660 if version >= 10 {
661 m.current_leader = LeaderIdAndEpoch::populated(version);
662 }
663 m
664 }
665}
666#[derive(Debug, Clone, PartialEq, Eq, Default)]
667pub struct BatchIndexAndErrorMessage<'a> {
668 pub batch_index: i32,
669 pub batch_index_error_message: Option<&'a str>,
670 pub unknown_tagged_fields: UnknownTaggedFields,
671}
672impl BatchIndexAndErrorMessage<'_> {
673 #[must_use]
674 pub fn to_owned(&self) -> produce_response::BatchIndexAndErrorMessage {
675 produce_response::BatchIndexAndErrorMessage {
676 batch_index: (self.batch_index),
677 batch_index_error_message: (self.batch_index_error_message).map(str::to_owned),
678 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
679 }
680 }
681}
682impl Encode for BatchIndexAndErrorMessage<'_> {
683 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
684 let flex = version >= 9;
685 if version >= 8 {
686 put_i32(buf, self.batch_index);
687 }
688 if version >= 8 {
689 if flex {
690 put_compact_nullable_string(buf, self.batch_index_error_message)?;
691 } else {
692 put_nullable_string(buf, self.batch_index_error_message)?;
693 }
694 }
695 if flex {
696 let tagged = WriteTaggedFields::new();
697 tagged.write(buf, &self.unknown_tagged_fields)?;
698 }
699 Ok(())
700 }
701 fn encoded_len(&self, version: i16) -> usize {
702 let flex = version >= 9;
703 let mut n: usize = 0;
704 if version >= 8 {
705 n += 4;
706 }
707 if version >= 8 {
708 n += if flex {
709 compact_nullable_string_len(self.batch_index_error_message)
710 } else {
711 nullable_string_len(self.batch_index_error_message)
712 };
713 }
714 if flex {
715 let known_pairs: Vec<(u32, usize)> = Vec::new();
716 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
717 }
718 n
719 }
720}
721impl<'de> DecodeBorrow<'de> for BatchIndexAndErrorMessage<'de> {
722 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
723 let flex = version >= 9;
724 let mut out = Self::default();
725 if version >= 8 {
726 out.batch_index = get_i32(buf)?;
727 }
728 if version >= 8 {
729 out.batch_index_error_message = if flex {
730 get_compact_nullable_string_borrowed(buf)?
731 } else {
732 get_nullable_string_borrowed(buf)?
733 };
734 }
735 if flex {
736 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
737 }
738 Ok(out)
739 }
740}
741#[cfg(test)]
742impl BatchIndexAndErrorMessage<'_> {
743 #[must_use]
744 pub fn populated(version: i16) -> Self {
745 let mut m = Self::default();
746 if version >= 8 {
747 m.batch_index = 1i32;
748 }
749 if version >= 8 {
750 m.batch_index_error_message = Some("x");
751 }
752 m
753 }
754}
755#[derive(Debug, Clone, PartialEq, Eq)]
756pub struct LeaderIdAndEpoch {
757 pub leader_id: i32,
758 pub leader_epoch: i32,
759 pub unknown_tagged_fields: UnknownTaggedFields,
760}
761impl Default for LeaderIdAndEpoch {
762 fn default() -> Self {
763 Self {
764 leader_id: -1i32,
765 leader_epoch: -1i32,
766 unknown_tagged_fields: UnknownTaggedFields::default(),
767 }
768 }
769}
770impl LeaderIdAndEpoch {
771 #[must_use]
772 pub fn to_owned(&self) -> produce_response::LeaderIdAndEpoch {
773 produce_response::LeaderIdAndEpoch {
774 leader_id: (self.leader_id),
775 leader_epoch: (self.leader_epoch),
776 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
777 }
778 }
779}
780impl Encode for LeaderIdAndEpoch {
781 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
782 let flex = version >= 9;
783 if version >= 10 {
784 put_i32(buf, self.leader_id);
785 }
786 if version >= 10 {
787 put_i32(buf, self.leader_epoch);
788 }
789 if flex {
790 let tagged = WriteTaggedFields::new();
791 tagged.write(buf, &self.unknown_tagged_fields)?;
792 }
793 Ok(())
794 }
795 fn encoded_len(&self, version: i16) -> usize {
796 let flex = version >= 9;
797 let mut n: usize = 0;
798 if version >= 10 {
799 n += 4;
800 }
801 if version >= 10 {
802 n += 4;
803 }
804 if flex {
805 let known_pairs: Vec<(u32, usize)> = Vec::new();
806 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
807 }
808 n
809 }
810}
811impl<'de> DecodeBorrow<'de> for LeaderIdAndEpoch {
812 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
813 let flex = version >= 9;
814 let mut out = Self::default();
815 if version >= 10 {
816 out.leader_id = get_i32(buf)?;
817 }
818 if version >= 10 {
819 out.leader_epoch = get_i32(buf)?;
820 }
821 if flex {
822 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
823 }
824 Ok(out)
825 }
826}
827#[cfg(test)]
828impl LeaderIdAndEpoch {
829 #[must_use]
830 pub fn populated(version: i16) -> Self {
831 let mut m = Self::default();
832 if version >= 10 {
833 m.leader_id = 1i32;
834 }
835 if version >= 10 {
836 m.leader_epoch = 1i32;
837 }
838 m
839 }
840}
841#[derive(Debug, Clone, PartialEq, Eq, Default)]
842pub struct NodeEndpoint<'a> {
843 pub node_id: i32,
844 pub host: &'a str,
845 pub port: i32,
846 pub rack: Option<&'a str>,
847 pub unknown_tagged_fields: UnknownTaggedFields,
848}
849impl NodeEndpoint<'_> {
850 #[must_use]
851 pub fn to_owned(&self) -> produce_response::NodeEndpoint {
852 produce_response::NodeEndpoint {
853 node_id: (self.node_id),
854 host: (self.host).to_owned(),
855 port: (self.port),
856 rack: (self.rack).map(str::to_owned),
857 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
858 }
859 }
860}
861impl Encode for NodeEndpoint<'_> {
862 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
863 let flex = version >= 9;
864 if version >= 10 {
865 put_i32(buf, self.node_id);
866 }
867 if version >= 10 {
868 if flex {
869 put_compact_string(buf, self.host)?;
870 } else {
871 put_string(buf, self.host)?;
872 }
873 }
874 if version >= 10 {
875 put_i32(buf, self.port);
876 }
877 if version >= 10 {
878 if flex {
879 put_compact_nullable_string(buf, self.rack)?;
880 } else {
881 put_nullable_string(buf, self.rack)?;
882 }
883 }
884 if flex {
885 let tagged = WriteTaggedFields::new();
886 tagged.write(buf, &self.unknown_tagged_fields)?;
887 }
888 Ok(())
889 }
890 fn encoded_len(&self, version: i16) -> usize {
891 let flex = version >= 9;
892 let mut n: usize = 0;
893 if version >= 10 {
894 n += 4;
895 }
896 if version >= 10 {
897 n += if flex {
898 compact_string_len(self.host)
899 } else {
900 string_len(self.host)
901 };
902 }
903 if version >= 10 {
904 n += 4;
905 }
906 if version >= 10 {
907 n += if flex {
908 compact_nullable_string_len(self.rack)
909 } else {
910 nullable_string_len(self.rack)
911 };
912 }
913 if flex {
914 let known_pairs: Vec<(u32, usize)> = Vec::new();
915 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
916 }
917 n
918 }
919}
920impl<'de> DecodeBorrow<'de> for NodeEndpoint<'de> {
921 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
922 let flex = version >= 9;
923 let mut out = Self::default();
924 if version >= 10 {
925 out.node_id = get_i32(buf)?;
926 }
927 if version >= 10 {
928 out.host = if flex {
929 get_compact_string_borrowed(buf)?
930 } else {
931 get_string_borrowed(buf)?
932 };
933 }
934 if version >= 10 {
935 out.port = get_i32(buf)?;
936 }
937 if version >= 10 {
938 out.rack = if flex {
939 get_compact_nullable_string_borrowed(buf)?
940 } else {
941 get_nullable_string_borrowed(buf)?
942 };
943 }
944 if flex {
945 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
946 }
947 Ok(out)
948 }
949}
950#[cfg(test)]
951impl NodeEndpoint<'_> {
952 #[must_use]
953 pub fn populated(version: i16) -> Self {
954 let mut m = Self::default();
955 if version >= 10 {
956 m.node_id = 1i32;
957 }
958 if version >= 10 {
959 m.host = "x";
960 }
961 if version >= 10 {
962 m.port = 1i32;
963 }
964 if version >= 10 {
965 m.rack = Some("x");
966 }
967 m
968 }
969}