1use bytes::BufMut;
3
4use crate::{
5 DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6 primitives::{
7 fixed::{get_i16, get_i32, get_i64, put_i16, put_i32, put_i64},
8 string_bytes::{
9 compact_nullable_string_len, compact_string_len, nullable_string_len,
10 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
11 string_len,
12 },
13 string_bytes_borrowed::{
14 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
15 get_nullable_string_borrowed, get_string_borrowed,
16 },
17 },
18 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
19};
20pub const API_KEY: i16 = 61;
21pub const MIN_VERSION: i16 = 0;
22pub const MAX_VERSION: i16 = 0;
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 DescribeProducersResponse<'a> {
31 pub throttle_time_ms: i32,
32 pub topics: Vec<TopicResponse<'a>>,
33 pub unknown_tagged_fields: UnknownTaggedFields,
34}
35impl DescribeProducersResponse<'_> {
36 #[must_use]
40 pub fn to_owned(&self) -> crate::owned::describe_producers_response::DescribeProducersResponse {
41 crate::owned::describe_producers_response::DescribeProducersResponse {
42 throttle_time_ms: (self.throttle_time_ms),
43 topics: (self.topics).iter().map(TopicResponse::to_owned).collect(),
44 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
45 }
46 }
47}
48impl Encode for DescribeProducersResponse<'_> {
49 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
50 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
51 return Err(ProtocolError::UnsupportedVersion {
52 api_key: API_KEY,
53 version,
54 });
55 }
56 let flex = is_flexible(version);
57 if version >= 0 {
58 put_i32(buf, self.throttle_time_ms);
59 }
60 if version >= 0 {
61 {
62 crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
63 for it in &self.topics {
64 it.encode(buf, version)?;
65 }
66 };
67 }
68 if flex {
69 let tagged = WriteTaggedFields::new();
70 tagged.write(buf, &self.unknown_tagged_fields);
71 }
72 Ok(())
73 }
74 fn encoded_len(&self, version: i16) -> usize {
75 let flex = is_flexible(version);
76 let mut n: usize = 0;
77 if version >= 0 {
78 n += 4;
79 }
80 if version >= 0 {
81 n += {
82 let prefix =
83 crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
84 let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
85 prefix + body
86 };
87 }
88 if flex {
89 let known_pairs: Vec<(u32, usize)> = Vec::new();
90 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
91 }
92 n
93 }
94}
95impl<'de> DecodeBorrow<'de> for DescribeProducersResponse<'de> {
96 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
97 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
98 return Err(ProtocolError::UnsupportedVersion {
99 api_key: API_KEY,
100 version,
101 });
102 }
103 let flex = is_flexible(version);
104 let mut out = Self::default();
105 if version >= 0 {
106 out.throttle_time_ms = get_i32(buf)?;
107 }
108 if version >= 0 {
109 out.topics = {
110 let n = crate::primitives::array::get_array_len(buf, flex)?;
111 let mut v = Vec::with_capacity(n);
112 for _ in 0..n {
113 v.push(TopicResponse::decode_borrow(buf, version)?);
114 }
115 v
116 };
117 }
118 if flex {
119 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
120 }
121 Ok(out)
122 }
123}
124#[cfg(test)]
125impl DescribeProducersResponse<'_> {
126 #[must_use]
127 pub fn populated(version: i16) -> Self {
128 let mut m = Self::default();
129 if version >= 0 {
130 m.throttle_time_ms = 1i32;
131 }
132 if version >= 0 {
133 m.topics = vec![TopicResponse::populated(version)];
134 }
135 m
136 }
137}
138#[derive(Debug, Clone, PartialEq, Eq, Default)]
139pub struct TopicResponse<'a> {
140 pub name: &'a str,
141 pub partitions: Vec<PartitionResponse<'a>>,
142 pub unknown_tagged_fields: UnknownTaggedFields,
143}
144impl TopicResponse<'_> {
145 #[must_use]
149 pub fn to_owned(&self) -> crate::owned::describe_producers_response::TopicResponse {
150 crate::owned::describe_producers_response::TopicResponse {
151 name: (self.name).to_string(),
152 partitions: (self.partitions)
153 .iter()
154 .map(PartitionResponse::to_owned)
155 .collect(),
156 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
157 }
158 }
159}
160impl Encode for TopicResponse<'_> {
161 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
162 let flex = version >= 0;
163 if version >= 0 {
164 if flex {
165 let () = put_compact_string(buf, self.name);
166 } else {
167 let () = put_string(buf, self.name);
168 }
169 }
170 if version >= 0 {
171 {
172 crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
173 for it in &self.partitions {
174 it.encode(buf, version)?;
175 }
176 };
177 }
178 if flex {
179 let tagged = WriteTaggedFields::new();
180 tagged.write(buf, &self.unknown_tagged_fields);
181 }
182 Ok(())
183 }
184 fn encoded_len(&self, version: i16) -> usize {
185 let flex = version >= 0;
186 let mut n: usize = 0;
187 if version >= 0 {
188 n += if flex {
189 compact_string_len(self.name)
190 } else {
191 string_len(self.name)
192 };
193 }
194 if version >= 0 {
195 n += {
196 let prefix =
197 crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
198 let body: usize = (self.partitions)
199 .iter()
200 .map(|it| it.encoded_len(version))
201 .sum();
202 prefix + body
203 };
204 }
205 if flex {
206 let known_pairs: Vec<(u32, usize)> = Vec::new();
207 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
208 }
209 n
210 }
211}
212impl<'de> DecodeBorrow<'de> for TopicResponse<'de> {
213 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
214 let flex = version >= 0;
215 let mut out = Self::default();
216 if version >= 0 {
217 out.name = if flex {
218 get_compact_string_borrowed(buf)?
219 } else {
220 get_string_borrowed(buf)?
221 };
222 }
223 if version >= 0 {
224 out.partitions = {
225 let n = crate::primitives::array::get_array_len(buf, flex)?;
226 let mut v = Vec::with_capacity(n);
227 for _ in 0..n {
228 v.push(PartitionResponse::decode_borrow(buf, version)?);
229 }
230 v
231 };
232 }
233 if flex {
234 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
235 }
236 Ok(out)
237 }
238}
239#[cfg(test)]
240impl TopicResponse<'_> {
241 #[must_use]
242 pub fn populated(version: i16) -> Self {
243 let mut m = Self::default();
244 if version >= 0 {
245 m.name = "x";
246 }
247 if version >= 0 {
248 m.partitions = vec![PartitionResponse::populated(version)];
249 }
250 m
251 }
252}
253#[derive(Debug, Clone, PartialEq, Eq, Default)]
254pub struct PartitionResponse<'a> {
255 pub partition_index: i32,
256 pub error_code: i16,
257 pub error_message: Option<&'a str>,
258 pub active_producers: Vec<ProducerState>,
259 pub unknown_tagged_fields: UnknownTaggedFields,
260}
261impl PartitionResponse<'_> {
262 #[must_use]
266 pub fn to_owned(&self) -> crate::owned::describe_producers_response::PartitionResponse {
267 crate::owned::describe_producers_response::PartitionResponse {
268 partition_index: (self.partition_index),
269 error_code: (self.error_code),
270 error_message: (self.error_message).map(std::string::ToString::to_string),
271 active_producers: (self.active_producers)
272 .iter()
273 .map(ProducerState::to_owned)
274 .collect(),
275 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
276 }
277 }
278}
279impl Encode for PartitionResponse<'_> {
280 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
281 let flex = version >= 0;
282 if version >= 0 {
283 put_i32(buf, self.partition_index);
284 }
285 if version >= 0 {
286 put_i16(buf, self.error_code);
287 }
288 if version >= 0 {
289 if flex {
290 let () = put_compact_nullable_string(buf, self.error_message);
291 } else {
292 let () = put_nullable_string(buf, self.error_message);
293 }
294 }
295 if version >= 0 {
296 {
297 crate::primitives::array::put_array_len(buf, (self.active_producers).len(), flex);
298 for it in &self.active_producers {
299 it.encode(buf, version)?;
300 }
301 };
302 }
303 if flex {
304 let tagged = WriteTaggedFields::new();
305 tagged.write(buf, &self.unknown_tagged_fields);
306 }
307 Ok(())
308 }
309 fn encoded_len(&self, version: i16) -> usize {
310 let flex = version >= 0;
311 let mut n: usize = 0;
312 if version >= 0 {
313 n += 4;
314 }
315 if version >= 0 {
316 n += 2;
317 }
318 if version >= 0 {
319 n += if flex {
320 compact_nullable_string_len(self.error_message)
321 } else {
322 nullable_string_len(self.error_message)
323 };
324 }
325 if version >= 0 {
326 n += {
327 let prefix = crate::primitives::array::array_len_prefix_len(
328 (self.active_producers).len(),
329 flex,
330 );
331 let body: usize = (self.active_producers)
332 .iter()
333 .map(|it| it.encoded_len(version))
334 .sum();
335 prefix + body
336 };
337 }
338 if flex {
339 let known_pairs: Vec<(u32, usize)> = Vec::new();
340 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
341 }
342 n
343 }
344}
345impl<'de> DecodeBorrow<'de> for PartitionResponse<'de> {
346 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
347 let flex = version >= 0;
348 let mut out = Self::default();
349 if version >= 0 {
350 out.partition_index = get_i32(buf)?;
351 }
352 if version >= 0 {
353 out.error_code = get_i16(buf)?;
354 }
355 if version >= 0 {
356 out.error_message = if flex {
357 get_compact_nullable_string_borrowed(buf)?
358 } else {
359 get_nullable_string_borrowed(buf)?
360 };
361 }
362 if version >= 0 {
363 out.active_producers = {
364 let n = crate::primitives::array::get_array_len(buf, flex)?;
365 let mut v = Vec::with_capacity(n);
366 for _ in 0..n {
367 v.push(ProducerState::decode_borrow(buf, version)?);
368 }
369 v
370 };
371 }
372 if flex {
373 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
374 }
375 Ok(out)
376 }
377}
378#[cfg(test)]
379impl PartitionResponse<'_> {
380 #[must_use]
381 pub fn populated(version: i16) -> Self {
382 let mut m = Self::default();
383 if version >= 0 {
384 m.partition_index = 1i32;
385 }
386 if version >= 0 {
387 m.error_code = 1i16;
388 }
389 if version >= 0 {
390 m.error_message = Some("x");
391 }
392 if version >= 0 {
393 m.active_producers = vec![ProducerState::populated(version)];
394 }
395 m
396 }
397}
398#[derive(Debug, Clone, PartialEq, Eq)]
399pub struct ProducerState {
400 pub producer_id: i64,
401 pub producer_epoch: i32,
402 pub last_sequence: i32,
403 pub last_timestamp: i64,
404 pub coordinator_epoch: i32,
405 pub current_txn_start_offset: i64,
406 pub unknown_tagged_fields: UnknownTaggedFields,
407}
408impl Default for ProducerState {
409 fn default() -> Self {
410 Self {
411 producer_id: 0i64,
412 producer_epoch: 0i32,
413 last_sequence: -1i32,
414 last_timestamp: -1i64,
415 coordinator_epoch: 0i32,
416 current_txn_start_offset: -1i64,
417 unknown_tagged_fields: UnknownTaggedFields::default(),
418 }
419 }
420}
421impl ProducerState {
422 #[must_use]
426 pub fn to_owned(&self) -> crate::owned::describe_producers_response::ProducerState {
427 crate::owned::describe_producers_response::ProducerState {
428 producer_id: (self.producer_id),
429 producer_epoch: (self.producer_epoch),
430 last_sequence: (self.last_sequence),
431 last_timestamp: (self.last_timestamp),
432 coordinator_epoch: (self.coordinator_epoch),
433 current_txn_start_offset: (self.current_txn_start_offset),
434 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
435 }
436 }
437}
438impl Encode for ProducerState {
439 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
440 let flex = version >= 0;
441 if version >= 0 {
442 put_i64(buf, self.producer_id);
443 }
444 if version >= 0 {
445 put_i32(buf, self.producer_epoch);
446 }
447 if version >= 0 {
448 put_i32(buf, self.last_sequence);
449 }
450 if version >= 0 {
451 put_i64(buf, self.last_timestamp);
452 }
453 if version >= 0 {
454 put_i32(buf, self.coordinator_epoch);
455 }
456 if version >= 0 {
457 put_i64(buf, self.current_txn_start_offset);
458 }
459 if flex {
460 let tagged = WriteTaggedFields::new();
461 tagged.write(buf, &self.unknown_tagged_fields);
462 }
463 Ok(())
464 }
465 fn encoded_len(&self, version: i16) -> usize {
466 let flex = version >= 0;
467 let mut n: usize = 0;
468 if version >= 0 {
469 n += 8;
470 }
471 if version >= 0 {
472 n += 4;
473 }
474 if version >= 0 {
475 n += 4;
476 }
477 if version >= 0 {
478 n += 8;
479 }
480 if version >= 0 {
481 n += 4;
482 }
483 if version >= 0 {
484 n += 8;
485 }
486 if flex {
487 let known_pairs: Vec<(u32, usize)> = Vec::new();
488 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
489 }
490 n
491 }
492}
493impl<'de> DecodeBorrow<'de> for ProducerState {
494 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
495 let flex = version >= 0;
496 let mut out = Self::default();
497 if version >= 0 {
498 out.producer_id = get_i64(buf)?;
499 }
500 if version >= 0 {
501 out.producer_epoch = get_i32(buf)?;
502 }
503 if version >= 0 {
504 out.last_sequence = get_i32(buf)?;
505 }
506 if version >= 0 {
507 out.last_timestamp = get_i64(buf)?;
508 }
509 if version >= 0 {
510 out.coordinator_epoch = get_i32(buf)?;
511 }
512 if version >= 0 {
513 out.current_txn_start_offset = get_i64(buf)?;
514 }
515 if flex {
516 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
517 }
518 Ok(out)
519 }
520}
521#[cfg(test)]
522impl ProducerState {
523 #[must_use]
524 pub fn populated(version: i16) -> Self {
525 let mut m = Self::default();
526 if version >= 0 {
527 m.producer_id = 1i64;
528 }
529 if version >= 0 {
530 m.producer_epoch = 1i32;
531 }
532 if version >= 0 {
533 m.last_sequence = 1i32;
534 }
535 if version >= 0 {
536 m.last_timestamp = 1i64;
537 }
538 if version >= 0 {
539 m.coordinator_epoch = 1i32;
540 }
541 if version >= 0 {
542 m.current_txn_start_offset = 1i64;
543 }
544 m
545 }
546}