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