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