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