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