1#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15 buf::{ByteBuf, ByteBufMut},
16 compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17 Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct ListTransactionsResponse {
24 pub throttle_time_ms: i32,
28
29 pub error_code: i16,
33
34 pub unknown_state_filters: Vec<StrBytes>,
38
39 pub transaction_states: Vec<TransactionState>,
43
44 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl ListTransactionsResponse {
49 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
55 self.throttle_time_ms = value;
56 self
57 }
58 pub fn with_error_code(mut self, value: i16) -> Self {
64 self.error_code = value;
65 self
66 }
67 pub fn with_unknown_state_filters(mut self, value: Vec<StrBytes>) -> Self {
73 self.unknown_state_filters = value;
74 self
75 }
76 pub fn with_transaction_states(mut self, value: Vec<TransactionState>) -> Self {
82 self.transaction_states = value;
83 self
84 }
85 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
87 self.unknown_tagged_fields = value;
88 self
89 }
90 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
92 self.unknown_tagged_fields.insert(key, value);
93 self
94 }
95}
96
97#[cfg(feature = "broker")]
98impl Encodable for ListTransactionsResponse {
99 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100 types::Int32.encode(buf, &self.throttle_time_ms)?;
101 types::Int16.encode(buf, &self.error_code)?;
102 types::CompactArray(types::CompactString).encode(buf, &self.unknown_state_filters)?;
103 types::CompactArray(types::Struct { version }).encode(buf, &self.transaction_states)?;
104 let num_tagged_fields = self.unknown_tagged_fields.len();
105 if num_tagged_fields > std::u32::MAX as usize {
106 bail!(
107 "Too many tagged fields to encode ({} fields)",
108 num_tagged_fields
109 );
110 }
111 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
112
113 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
114 Ok(())
115 }
116 fn compute_size(&self, version: i16) -> Result<usize> {
117 let mut total_size = 0;
118 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
119 total_size += types::Int16.compute_size(&self.error_code)?;
120 total_size +=
121 types::CompactArray(types::CompactString).compute_size(&self.unknown_state_filters)?;
122 total_size += types::CompactArray(types::Struct { version })
123 .compute_size(&self.transaction_states)?;
124 let num_tagged_fields = self.unknown_tagged_fields.len();
125 if num_tagged_fields > std::u32::MAX as usize {
126 bail!(
127 "Too many tagged fields to encode ({} fields)",
128 num_tagged_fields
129 );
130 }
131 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
132
133 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
134 Ok(total_size)
135 }
136}
137
138#[cfg(feature = "client")]
139impl Decodable for ListTransactionsResponse {
140 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
141 let throttle_time_ms = types::Int32.decode(buf)?;
142 let error_code = types::Int16.decode(buf)?;
143 let unknown_state_filters = types::CompactArray(types::CompactString).decode(buf)?;
144 let transaction_states = types::CompactArray(types::Struct { version }).decode(buf)?;
145 let mut unknown_tagged_fields = BTreeMap::new();
146 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
147 for _ in 0..num_tagged_fields {
148 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
149 let size: u32 = types::UnsignedVarInt.decode(buf)?;
150 let unknown_value = buf.try_get_bytes(size as usize)?;
151 unknown_tagged_fields.insert(tag as i32, unknown_value);
152 }
153 Ok(Self {
154 throttle_time_ms,
155 error_code,
156 unknown_state_filters,
157 transaction_states,
158 unknown_tagged_fields,
159 })
160 }
161}
162
163impl Default for ListTransactionsResponse {
164 fn default() -> Self {
165 Self {
166 throttle_time_ms: 0,
167 error_code: 0,
168 unknown_state_filters: Default::default(),
169 transaction_states: Default::default(),
170 unknown_tagged_fields: BTreeMap::new(),
171 }
172 }
173}
174
175impl Message for ListTransactionsResponse {
176 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
177 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
178}
179
180#[non_exhaustive]
182#[derive(Debug, Clone, PartialEq)]
183pub struct TransactionState {
184 pub transactional_id: super::TransactionalId,
188
189 pub producer_id: super::ProducerId,
193
194 pub transaction_state: StrBytes,
198
199 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
201}
202
203impl TransactionState {
204 pub fn with_transactional_id(mut self, value: super::TransactionalId) -> Self {
210 self.transactional_id = value;
211 self
212 }
213 pub fn with_producer_id(mut self, value: super::ProducerId) -> Self {
219 self.producer_id = value;
220 self
221 }
222 pub fn with_transaction_state(mut self, value: StrBytes) -> Self {
228 self.transaction_state = value;
229 self
230 }
231 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
233 self.unknown_tagged_fields = value;
234 self
235 }
236 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
238 self.unknown_tagged_fields.insert(key, value);
239 self
240 }
241}
242
243#[cfg(feature = "broker")]
244impl Encodable for TransactionState {
245 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
246 types::CompactString.encode(buf, &self.transactional_id)?;
247 types::Int64.encode(buf, &self.producer_id)?;
248 types::CompactString.encode(buf, &self.transaction_state)?;
249 let num_tagged_fields = self.unknown_tagged_fields.len();
250 if num_tagged_fields > std::u32::MAX as usize {
251 bail!(
252 "Too many tagged fields to encode ({} fields)",
253 num_tagged_fields
254 );
255 }
256 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
257
258 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
259 Ok(())
260 }
261 fn compute_size(&self, version: i16) -> Result<usize> {
262 let mut total_size = 0;
263 total_size += types::CompactString.compute_size(&self.transactional_id)?;
264 total_size += types::Int64.compute_size(&self.producer_id)?;
265 total_size += types::CompactString.compute_size(&self.transaction_state)?;
266 let num_tagged_fields = self.unknown_tagged_fields.len();
267 if num_tagged_fields > std::u32::MAX as usize {
268 bail!(
269 "Too many tagged fields to encode ({} fields)",
270 num_tagged_fields
271 );
272 }
273 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
274
275 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
276 Ok(total_size)
277 }
278}
279
280#[cfg(feature = "client")]
281impl Decodable for TransactionState {
282 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
283 let transactional_id = types::CompactString.decode(buf)?;
284 let producer_id = types::Int64.decode(buf)?;
285 let transaction_state = types::CompactString.decode(buf)?;
286 let mut unknown_tagged_fields = BTreeMap::new();
287 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
288 for _ in 0..num_tagged_fields {
289 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
290 let size: u32 = types::UnsignedVarInt.decode(buf)?;
291 let unknown_value = buf.try_get_bytes(size as usize)?;
292 unknown_tagged_fields.insert(tag as i32, unknown_value);
293 }
294 Ok(Self {
295 transactional_id,
296 producer_id,
297 transaction_state,
298 unknown_tagged_fields,
299 })
300 }
301}
302
303impl Default for TransactionState {
304 fn default() -> Self {
305 Self {
306 transactional_id: Default::default(),
307 producer_id: (0).into(),
308 transaction_state: Default::default(),
309 unknown_tagged_fields: BTreeMap::new(),
310 }
311 }
312}
313
314impl Message for TransactionState {
315 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
316 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
317}
318
319impl HeaderVersion for ListTransactionsResponse {
320 fn header_version(version: i16) -> i16 {
321 1
322 }
323}