1use bytes::{Buf, BufMut};
4
5use crate::{
6 Decode, Encode, ProtocolError, UnknownTaggedFields,
7 primitives::{
8 array::{array_len_prefix_len, get_array_len, put_array_len},
9 fixed::{get_i16, get_i32, put_i16, put_i32},
10 string_bytes::{
11 compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
12 get_compact_string_owned, get_nullable_string_owned, get_string_owned,
13 nullable_string_len, put_compact_nullable_string, put_compact_string,
14 put_nullable_string, put_string, string_len,
15 },
16 uuid::{Uuid, get_uuid, put_uuid},
17 },
18 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
19};
20pub const API_KEY: i16 = 79;
21pub const MIN_VERSION: i16 = 1;
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, Default)]
30pub struct ShareAcknowledgeResponse {
31 pub throttle_time_ms: i32,
32 pub error_code: i16,
33 pub error_message: Option<String>,
34 pub acquisition_lock_timeout_ms: i32,
35 pub responses: Vec<ShareAcknowledgeTopicResponse>,
36 pub node_endpoints: Vec<NodeEndpoint>,
37 pub unknown_tagged_fields: UnknownTaggedFields,
38}
39impl Encode for ShareAcknowledgeResponse {
40 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
41 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
42 return Err(ProtocolError::UnsupportedVersion {
43 api_key: API_KEY,
44 version,
45 });
46 }
47 let flex = is_flexible(version);
48 if version >= 0 {
49 put_i32(buf, self.throttle_time_ms);
50 }
51 if version >= 0 {
52 put_i16(buf, self.error_code);
53 }
54 if version >= 0 {
55 if flex {
56 put_compact_nullable_string(buf, self.error_message.as_deref())?;
57 } else {
58 put_nullable_string(buf, self.error_message.as_deref())?;
59 }
60 }
61 if version >= 2 {
62 put_i32(buf, self.acquisition_lock_timeout_ms);
63 }
64 if version >= 0 {
65 {
66 put_array_len(buf, (self.responses).len(), flex)?;
67 for it in &self.responses {
68 it.encode(buf, version)?;
69 }
70 }
71 }
72 if version >= 0 {
73 {
74 put_array_len(buf, (self.node_endpoints).len(), flex)?;
75 for it in &self.node_endpoints {
76 it.encode(buf, version)?;
77 }
78 }
79 }
80 if flex {
81 let tagged = WriteTaggedFields::new();
82 tagged.write(buf, &self.unknown_tagged_fields)?;
83 }
84 Ok(())
85 }
86 fn encoded_len(&self, version: i16) -> usize {
87 let flex = is_flexible(version);
88 let mut n: usize = 0;
89 if version >= 0 {
90 n += 4;
91 }
92 if version >= 0 {
93 n += 2;
94 }
95 if version >= 0 {
96 n += if flex {
97 compact_nullable_string_len(self.error_message.as_deref())
98 } else {
99 nullable_string_len(self.error_message.as_deref())
100 };
101 }
102 if version >= 2 {
103 n += 4;
104 }
105 if version >= 0 {
106 n += {
107 let prefix = array_len_prefix_len((self.responses).len(), flex);
108 let body: usize = (self.responses)
109 .iter()
110 .map(|it| it.encoded_len(version))
111 .sum();
112 prefix + body
113 };
114 }
115 if version >= 0 {
116 n += {
117 let prefix = array_len_prefix_len((self.node_endpoints).len(), flex);
118 let body: usize = (self.node_endpoints)
119 .iter()
120 .map(|it| it.encoded_len(version))
121 .sum();
122 prefix + body
123 };
124 }
125 if flex {
126 let known_pairs: Vec<(u32, usize)> = Vec::new();
127 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
128 }
129 n
130 }
131}
132impl Decode<'_> for ShareAcknowledgeResponse {
133 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
134 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
135 return Err(ProtocolError::UnsupportedVersion {
136 api_key: API_KEY,
137 version,
138 });
139 }
140 let flex = is_flexible(version);
141 let mut out = Self::default();
142 if version >= 0 {
143 out.throttle_time_ms = get_i32(buf)?;
144 }
145 if version >= 0 {
146 out.error_code = get_i16(buf)?;
147 }
148 if version >= 0 {
149 out.error_message = if flex {
150 get_compact_nullable_string_owned(buf)?
151 } else {
152 get_nullable_string_owned(buf)?
153 };
154 }
155 if version >= 2 {
156 out.acquisition_lock_timeout_ms = get_i32(buf)?;
157 }
158 if version >= 0 {
159 out.responses = {
160 let n = get_array_len(buf, flex)?;
161 let mut v = Vec::with_capacity(n);
162 for _ in 0..n {
163 v.push(ShareAcknowledgeTopicResponse::decode(buf, version)?);
164 }
165 v
166 };
167 }
168 if version >= 0 {
169 out.node_endpoints = {
170 let n = get_array_len(buf, flex)?;
171 let mut v = Vec::with_capacity(n);
172 for _ in 0..n {
173 v.push(NodeEndpoint::decode(buf, version)?);
174 }
175 v
176 };
177 }
178 if flex {
179 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
180 }
181 Ok(out)
182 }
183}
184#[cfg(test)]
185impl ShareAcknowledgeResponse {
186 #[must_use]
187 pub fn populated(version: i16) -> Self {
188 let mut m = Self::default();
189 if version >= 0 {
190 m.throttle_time_ms = 1i32;
191 }
192 if version >= 0 {
193 m.error_code = 1i16;
194 }
195 if version >= 0 {
196 m.error_message = Some("x".to_owned());
197 }
198 if version >= 2 {
199 m.acquisition_lock_timeout_ms = 1i32;
200 }
201 if version >= 0 {
202 m.responses = vec![ShareAcknowledgeTopicResponse::populated(version)];
203 }
204 if version >= 0 {
205 m.node_endpoints = vec![NodeEndpoint::populated(version)];
206 }
207 m
208 }
209}
210#[derive(Debug, Clone, PartialEq, Eq, Default)]
211pub struct ShareAcknowledgeTopicResponse {
212 pub topic_id: Uuid,
213 pub partitions: Vec<PartitionData>,
214 pub unknown_tagged_fields: UnknownTaggedFields,
215}
216impl Encode for ShareAcknowledgeTopicResponse {
217 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
218 let flex = version >= 0;
219 if version >= 0 {
220 put_uuid(buf, self.topic_id);
221 }
222 if version >= 0 {
223 {
224 put_array_len(buf, (self.partitions).len(), flex)?;
225 for it in &self.partitions {
226 it.encode(buf, version)?;
227 }
228 }
229 }
230 if flex {
231 let tagged = WriteTaggedFields::new();
232 tagged.write(buf, &self.unknown_tagged_fields)?;
233 }
234 Ok(())
235 }
236 fn encoded_len(&self, version: i16) -> usize {
237 let flex = version >= 0;
238 let mut n: usize = 0;
239 if version >= 0 {
240 n += 16;
241 }
242 if version >= 0 {
243 n += {
244 let prefix = 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 = get_uuid(buf)?;
265 }
266 if version >= 0 {
267 out.partitions = {
268 let n = 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 = 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_owned());
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_owned();
575 }
576 if version >= 0 {
577 m.port = 1i32;
578 }
579 if version >= 0 {
580 m.rack = Some("x".to_owned());
581 }
582 m
583 }
584}
585#[must_use]
588#[allow(unused_comparisons)]
589pub fn default_json(version: i16) -> ::serde_json::Value {
590 let mut obj = ::serde_json::Map::new();
591 obj.insert("throttleTimeMs".to_owned(), ::serde_json::json!(0));
592 obj.insert("errorCode".to_owned(), ::serde_json::json!(0));
593 obj.insert("errorMessage".to_owned(), ::serde_json::Value::Null);
594 if version >= 2 {
595 obj.insert(
596 "acquisitionLockTimeoutMs".to_owned(),
597 ::serde_json::json!(0),
598 );
599 }
600 obj.insert("responses".to_owned(), ::serde_json::Value::Array(vec![]));
601 obj.insert(
602 "nodeEndpoints".to_owned(),
603 ::serde_json::Value::Array(vec![]),
604 );
605 ::serde_json::Value::Object(obj)
606}