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