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