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