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