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