1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{
6 get_i16, get_i32, get_i64, get_u16, put_i16, put_i32, put_i64, put_u16,
7};
8use crate::primitives::string_bytes::{
9 compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
10 get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
11 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
12};
13use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
14use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
15
16pub const API_KEY: i16 = 55;
17pub const MIN_VERSION: i16 = 0;
18pub const MAX_VERSION: i16 = 2;
19pub const FLEXIBLE_MIN: i16 = 0;
20
21#[inline]
22fn is_flexible(version: i16) -> bool {
23 version >= FLEXIBLE_MIN
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, Default)]
27pub struct DescribeQuorumResponse {
28 pub error_code: i16,
29 pub error_message: Option<String>,
30 pub topics: Vec<TopicData>,
31 pub nodes: Vec<Node>,
32 pub unknown_tagged_fields: UnknownTaggedFields,
33}
34impl Encode for DescribeQuorumResponse {
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 put_i16(buf, self.error_code);
45 }
46 if version >= 2 {
47 if flex {
48 put_compact_nullable_string(buf, self.error_message.as_deref());
49 } else {
50 put_nullable_string(buf, self.error_message.as_deref());
51 }
52 }
53 if version >= 0 {
54 {
55 crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
56 for it in &self.topics {
57 it.encode(buf, version)?;
58 }
59 }
60 }
61 if version >= 2 {
62 {
63 crate::primitives::array::put_array_len(buf, (self.nodes).len(), flex);
64 for it in &self.nodes {
65 it.encode(buf, version)?;
66 }
67 }
68 }
69 if flex {
70 let tagged = WriteTaggedFields::new();
71 tagged.write(buf, &self.unknown_tagged_fields);
72 }
73 Ok(())
74 }
75 fn encoded_len(&self, version: i16) -> usize {
76 let flex = is_flexible(version);
77 let mut n: usize = 0;
78 if version >= 0 {
79 n += 2;
80 }
81 if version >= 2 {
82 n += if flex {
83 compact_nullable_string_len(self.error_message.as_deref())
84 } else {
85 nullable_string_len(self.error_message.as_deref())
86 };
87 }
88 if version >= 0 {
89 n += {
90 let prefix =
91 crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
92 let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
93 prefix + body
94 };
95 }
96 if version >= 2 {
97 n += {
98 let prefix =
99 crate::primitives::array::array_len_prefix_len((self.nodes).len(), flex);
100 let body: usize = (self.nodes).iter().map(|it| it.encoded_len(version)).sum();
101 prefix + body
102 };
103 }
104 if flex {
105 let known_pairs: Vec<(u32, usize)> = Vec::new();
106 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
107 }
108 n
109 }
110}
111impl Decode<'_> for DescribeQuorumResponse {
112 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
113 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
114 return Err(ProtocolError::UnsupportedVersion {
115 api_key: API_KEY,
116 version,
117 });
118 }
119 let flex = is_flexible(version);
120 let mut out = Self::default();
121 if version >= 0 {
122 out.error_code = get_i16(buf)?;
123 }
124 if version >= 2 {
125 out.error_message = if flex {
126 get_compact_nullable_string_owned(buf)?
127 } else {
128 get_nullable_string_owned(buf)?
129 };
130 }
131 if version >= 0 {
132 out.topics = {
133 let n = crate::primitives::array::get_array_len(buf, flex)?;
134 let mut v = Vec::with_capacity(n);
135 for _ in 0..n {
136 v.push(TopicData::decode(buf, version)?);
137 }
138 v
139 };
140 }
141 if version >= 2 {
142 out.nodes = {
143 let n = crate::primitives::array::get_array_len(buf, flex)?;
144 let mut v = Vec::with_capacity(n);
145 for _ in 0..n {
146 v.push(Node::decode(buf, version)?);
147 }
148 v
149 };
150 }
151 if flex {
152 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
153 }
154 Ok(out)
155 }
156}
157#[cfg(test)]
158impl DescribeQuorumResponse {
159 #[must_use]
160 pub fn populated(version: i16) -> Self {
161 let mut m = Self::default();
162 if version >= 0 {
163 m.error_code = 1i16;
164 }
165 if version >= 2 {
166 m.error_message = Some("x".to_string());
167 }
168 if version >= 0 {
169 m.topics = vec![TopicData::populated(version)];
170 }
171 if version >= 2 {
172 m.nodes = vec![Node::populated(version)];
173 }
174 m
175 }
176}
177#[derive(Debug, Clone, PartialEq, Eq, Default)]
178pub struct TopicData {
179 pub topic_name: String,
180 pub partitions: Vec<PartitionData>,
181 pub unknown_tagged_fields: UnknownTaggedFields,
182}
183impl Encode for TopicData {
184 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
185 let flex = version >= 0;
186 if version >= 0 {
187 if flex {
188 put_compact_string(buf, &self.topic_name);
189 } else {
190 put_string(buf, &self.topic_name);
191 }
192 }
193 if version >= 0 {
194 {
195 crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
196 for it in &self.partitions {
197 it.encode(buf, version)?;
198 }
199 }
200 }
201 if flex {
202 let tagged = WriteTaggedFields::new();
203 tagged.write(buf, &self.unknown_tagged_fields);
204 }
205 Ok(())
206 }
207 fn encoded_len(&self, version: i16) -> usize {
208 let flex = version >= 0;
209 let mut n: usize = 0;
210 if version >= 0 {
211 n += if flex {
212 compact_string_len(&self.topic_name)
213 } else {
214 string_len(&self.topic_name)
215 };
216 }
217 if version >= 0 {
218 n += {
219 let prefix =
220 crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
221 let body: usize = (self.partitions)
222 .iter()
223 .map(|it| it.encoded_len(version))
224 .sum();
225 prefix + body
226 };
227 }
228 if flex {
229 let known_pairs: Vec<(u32, usize)> = Vec::new();
230 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
231 }
232 n
233 }
234}
235impl Decode<'_> for TopicData {
236 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
237 let flex = version >= 0;
238 let mut out = Self::default();
239 if version >= 0 {
240 out.topic_name = if flex {
241 get_compact_string_owned(buf)?
242 } else {
243 get_string_owned(buf)?
244 };
245 }
246 if version >= 0 {
247 out.partitions = {
248 let n = crate::primitives::array::get_array_len(buf, flex)?;
249 let mut v = Vec::with_capacity(n);
250 for _ in 0..n {
251 v.push(PartitionData::decode(buf, version)?);
252 }
253 v
254 };
255 }
256 if flex {
257 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
258 }
259 Ok(out)
260 }
261}
262#[cfg(test)]
263impl TopicData {
264 #[must_use]
265 pub fn populated(version: i16) -> Self {
266 let mut m = Self::default();
267 if version >= 0 {
268 m.topic_name = "x".to_string();
269 }
270 if version >= 0 {
271 m.partitions = vec![PartitionData::populated(version)];
272 }
273 m
274 }
275}
276#[derive(Debug, Clone, PartialEq, Eq, Default)]
277pub struct PartitionData {
278 pub partition_index: i32,
279 pub error_code: i16,
280 pub error_message: Option<String>,
281 pub leader_id: i32,
282 pub leader_epoch: i32,
283 pub high_watermark: i64,
284 pub current_voters: Vec<super::common::describe_quorum_response::replica_state::ReplicaState>,
285 pub observers: Vec<super::common::describe_quorum_response::replica_state::ReplicaState>,
286 pub unknown_tagged_fields: UnknownTaggedFields,
287}
288impl Encode for PartitionData {
289 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
290 let flex = version >= 0;
291 if version >= 0 {
292 put_i32(buf, self.partition_index);
293 }
294 if version >= 0 {
295 put_i16(buf, self.error_code);
296 }
297 if version >= 2 {
298 if flex {
299 put_compact_nullable_string(buf, self.error_message.as_deref());
300 } else {
301 put_nullable_string(buf, self.error_message.as_deref());
302 }
303 }
304 if version >= 0 {
305 put_i32(buf, self.leader_id);
306 }
307 if version >= 0 {
308 put_i32(buf, self.leader_epoch);
309 }
310 if version >= 0 {
311 put_i64(buf, self.high_watermark);
312 }
313 if version >= 0 {
314 {
315 crate::primitives::array::put_array_len(buf, (self.current_voters).len(), flex);
316 for it in &self.current_voters {
317 it.encode(buf, version)?;
318 }
319 }
320 }
321 if version >= 0 {
322 {
323 crate::primitives::array::put_array_len(buf, (self.observers).len(), flex);
324 for it in &self.observers {
325 it.encode(buf, version)?;
326 }
327 }
328 }
329 if flex {
330 let tagged = WriteTaggedFields::new();
331 tagged.write(buf, &self.unknown_tagged_fields);
332 }
333 Ok(())
334 }
335 fn encoded_len(&self, version: i16) -> usize {
336 let flex = version >= 0;
337 let mut n: usize = 0;
338 if version >= 0 {
339 n += 4;
340 }
341 if version >= 0 {
342 n += 2;
343 }
344 if version >= 2 {
345 n += if flex {
346 compact_nullable_string_len(self.error_message.as_deref())
347 } else {
348 nullable_string_len(self.error_message.as_deref())
349 };
350 }
351 if version >= 0 {
352 n += 4;
353 }
354 if version >= 0 {
355 n += 4;
356 }
357 if version >= 0 {
358 n += 8;
359 }
360 if version >= 0 {
361 n += {
362 let prefix = crate::primitives::array::array_len_prefix_len(
363 (self.current_voters).len(),
364 flex,
365 );
366 let body: usize = (self.current_voters)
367 .iter()
368 .map(|it| it.encoded_len(version))
369 .sum();
370 prefix + body
371 };
372 }
373 if version >= 0 {
374 n += {
375 let prefix =
376 crate::primitives::array::array_len_prefix_len((self.observers).len(), flex);
377 let body: usize = (self.observers)
378 .iter()
379 .map(|it| it.encoded_len(version))
380 .sum();
381 prefix + body
382 };
383 }
384 if flex {
385 let known_pairs: Vec<(u32, usize)> = Vec::new();
386 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
387 }
388 n
389 }
390}
391impl Decode<'_> for PartitionData {
392 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
393 let flex = version >= 0;
394 let mut out = Self::default();
395 if version >= 0 {
396 out.partition_index = get_i32(buf)?;
397 }
398 if version >= 0 {
399 out.error_code = get_i16(buf)?;
400 }
401 if version >= 2 {
402 out.error_message = if flex {
403 get_compact_nullable_string_owned(buf)?
404 } else {
405 get_nullable_string_owned(buf)?
406 };
407 }
408 if version >= 0 {
409 out.leader_id = get_i32(buf)?;
410 }
411 if version >= 0 {
412 out.leader_epoch = get_i32(buf)?;
413 }
414 if version >= 0 {
415 out.high_watermark = get_i64(buf)?;
416 }
417 if version >= 0 {
418 out.current_voters = {
419 let n = crate::primitives::array::get_array_len(buf, flex)?;
420 let mut v = Vec::with_capacity(n);
421 for _ in 0..n {
422 v . push (super :: common :: describe_quorum_response :: replica_state :: ReplicaState :: decode (buf , version) ?) ;
423 }
424 v
425 };
426 }
427 if version >= 0 {
428 out.observers = {
429 let n = crate::primitives::array::get_array_len(buf, flex)?;
430 let mut v = Vec::with_capacity(n);
431 for _ in 0..n {
432 v . push (super :: common :: describe_quorum_response :: replica_state :: ReplicaState :: decode (buf , version) ?) ;
433 }
434 v
435 };
436 }
437 if flex {
438 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
439 }
440 Ok(out)
441 }
442}
443#[cfg(test)]
444impl PartitionData {
445 #[must_use]
446 pub fn populated(version: i16) -> Self {
447 let mut m = Self::default();
448 if version >= 0 {
449 m.partition_index = 1i32;
450 }
451 if version >= 0 {
452 m.error_code = 1i16;
453 }
454 if version >= 2 {
455 m.error_message = Some("x".to_string());
456 }
457 if version >= 0 {
458 m.leader_id = 1i32;
459 }
460 if version >= 0 {
461 m.leader_epoch = 1i32;
462 }
463 if version >= 0 {
464 m.high_watermark = 1i64;
465 }
466 if version >= 0 {
467 m.current_voters = vec![
468 super::common::describe_quorum_response::replica_state::ReplicaState::populated(
469 version,
470 ),
471 ];
472 }
473 if version >= 0 {
474 m.observers = vec![
475 super::common::describe_quorum_response::replica_state::ReplicaState::populated(
476 version,
477 ),
478 ];
479 }
480 m
481 }
482}
483#[derive(Debug, Clone, PartialEq, Eq, Default)]
484pub struct Node {
485 pub node_id: i32,
486 pub listeners: Vec<Listener>,
487 pub unknown_tagged_fields: UnknownTaggedFields,
488}
489impl Encode for Node {
490 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
491 let flex = version >= 0;
492 if version >= 2 {
493 put_i32(buf, self.node_id);
494 }
495 if version >= 2 {
496 {
497 crate::primitives::array::put_array_len(buf, (self.listeners).len(), flex);
498 for it in &self.listeners {
499 it.encode(buf, version)?;
500 }
501 }
502 }
503 if flex {
504 let tagged = WriteTaggedFields::new();
505 tagged.write(buf, &self.unknown_tagged_fields);
506 }
507 Ok(())
508 }
509 fn encoded_len(&self, version: i16) -> usize {
510 let flex = version >= 0;
511 let mut n: usize = 0;
512 if version >= 2 {
513 n += 4;
514 }
515 if version >= 2 {
516 n += {
517 let prefix =
518 crate::primitives::array::array_len_prefix_len((self.listeners).len(), flex);
519 let body: usize = (self.listeners)
520 .iter()
521 .map(|it| it.encoded_len(version))
522 .sum();
523 prefix + body
524 };
525 }
526 if flex {
527 let known_pairs: Vec<(u32, usize)> = Vec::new();
528 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
529 }
530 n
531 }
532}
533impl Decode<'_> for Node {
534 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
535 let flex = version >= 0;
536 let mut out = Self::default();
537 if version >= 2 {
538 out.node_id = get_i32(buf)?;
539 }
540 if version >= 2 {
541 out.listeners = {
542 let n = crate::primitives::array::get_array_len(buf, flex)?;
543 let mut v = Vec::with_capacity(n);
544 for _ in 0..n {
545 v.push(Listener::decode(buf, version)?);
546 }
547 v
548 };
549 }
550 if flex {
551 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
552 }
553 Ok(out)
554 }
555}
556#[cfg(test)]
557impl Node {
558 #[must_use]
559 pub fn populated(version: i16) -> Self {
560 let mut m = Self::default();
561 if version >= 2 {
562 m.node_id = 1i32;
563 }
564 if version >= 2 {
565 m.listeners = vec![Listener::populated(version)];
566 }
567 m
568 }
569}
570#[derive(Debug, Clone, PartialEq, Eq, Default)]
571pub struct Listener {
572 pub name: String,
573 pub host: String,
574 pub port: u16,
575 pub unknown_tagged_fields: UnknownTaggedFields,
576}
577impl Encode for Listener {
578 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
579 let flex = version >= 0;
580 if version >= 2 {
581 if flex {
582 put_compact_string(buf, &self.name);
583 } else {
584 put_string(buf, &self.name);
585 }
586 }
587 if version >= 2 {
588 if flex {
589 put_compact_string(buf, &self.host);
590 } else {
591 put_string(buf, &self.host);
592 }
593 }
594 if version >= 2 {
595 put_u16(buf, self.port);
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 >= 0;
605 let mut n: usize = 0;
606 if version >= 2 {
607 n += if flex {
608 compact_string_len(&self.name)
609 } else {
610 string_len(&self.name)
611 };
612 }
613 if version >= 2 {
614 n += if flex {
615 compact_string_len(&self.host)
616 } else {
617 string_len(&self.host)
618 };
619 }
620 if version >= 2 {
621 n += 2;
622 }
623 if flex {
624 let known_pairs: Vec<(u32, usize)> = Vec::new();
625 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
626 }
627 n
628 }
629}
630impl Decode<'_> for Listener {
631 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
632 let flex = version >= 0;
633 let mut out = Self::default();
634 if version >= 2 {
635 out.name = if flex {
636 get_compact_string_owned(buf)?
637 } else {
638 get_string_owned(buf)?
639 };
640 }
641 if version >= 2 {
642 out.host = if flex {
643 get_compact_string_owned(buf)?
644 } else {
645 get_string_owned(buf)?
646 };
647 }
648 if version >= 2 {
649 out.port = get_u16(buf)?;
650 }
651 if flex {
652 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
653 }
654 Ok(out)
655 }
656}
657#[cfg(test)]
658impl Listener {
659 #[must_use]
660 pub fn populated(version: i16) -> Self {
661 let mut m = Self::default();
662 if version >= 2 {
663 m.name = "x".to_string();
664 }
665 if version >= 2 {
666 m.host = "x".to_string();
667 }
668 if version >= 2 {
669 m.port = 1u16;
670 }
671 m
672 }
673}
674
675#[must_use]
678#[allow(unused_comparisons)]
679pub fn default_json(version: i16) -> ::serde_json::Value {
680 let mut obj = ::serde_json::Map::new();
681 obj.insert("errorCode".to_string(), ::serde_json::json!(0));
682 if version >= 2 {
683 obj.insert("errorMessage".to_string(), ::serde_json::Value::Null);
684 }
685 obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
686 if version >= 2 {
687 obj.insert("nodes".to_string(), ::serde_json::Value::Array(vec![]));
688 }
689 ::serde_json::Value::Object(obj)
690}