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