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