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