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 TxnOffsetCommitResponse {
24 pub throttle_time_ms: i32,
28
29 pub topics: Vec<TxnOffsetCommitResponseTopic>,
33
34 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl TxnOffsetCommitResponse {
39 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
45 self.throttle_time_ms = value;
46 self
47 }
48 pub fn with_topics(mut self, value: Vec<TxnOffsetCommitResponseTopic>) -> Self {
54 self.topics = value;
55 self
56 }
57 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
59 self.unknown_tagged_fields = value;
60 self
61 }
62 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
64 self.unknown_tagged_fields.insert(key, value);
65 self
66 }
67}
68
69#[cfg(feature = "broker")]
70impl Encodable for TxnOffsetCommitResponse {
71 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
72 types::Int32.encode(buf, &self.throttle_time_ms)?;
73 if version >= 3 {
74 types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
75 } else {
76 types::Array(types::Struct { version }).encode(buf, &self.topics)?;
77 }
78 if version >= 3 {
79 let num_tagged_fields = self.unknown_tagged_fields.len();
80 if num_tagged_fields > std::u32::MAX as usize {
81 bail!(
82 "Too many tagged fields to encode ({} fields)",
83 num_tagged_fields
84 );
85 }
86 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
87
88 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
89 }
90 Ok(())
91 }
92 fn compute_size(&self, version: i16) -> Result<usize> {
93 let mut total_size = 0;
94 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
95 if version >= 3 {
96 total_size +=
97 types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
98 } else {
99 total_size += types::Array(types::Struct { version }).compute_size(&self.topics)?;
100 }
101 if version >= 3 {
102 let num_tagged_fields = self.unknown_tagged_fields.len();
103 if num_tagged_fields > std::u32::MAX as usize {
104 bail!(
105 "Too many tagged fields to encode ({} fields)",
106 num_tagged_fields
107 );
108 }
109 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
110
111 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
112 }
113 Ok(total_size)
114 }
115}
116
117#[cfg(feature = "client")]
118impl Decodable for TxnOffsetCommitResponse {
119 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
120 let throttle_time_ms = types::Int32.decode(buf)?;
121 let topics = if version >= 3 {
122 types::CompactArray(types::Struct { version }).decode(buf)?
123 } else {
124 types::Array(types::Struct { version }).decode(buf)?
125 };
126 let mut unknown_tagged_fields = BTreeMap::new();
127 if version >= 3 {
128 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
129 for _ in 0..num_tagged_fields {
130 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
131 let size: u32 = types::UnsignedVarInt.decode(buf)?;
132 let unknown_value = buf.try_get_bytes(size as usize)?;
133 unknown_tagged_fields.insert(tag as i32, unknown_value);
134 }
135 }
136 Ok(Self {
137 throttle_time_ms,
138 topics,
139 unknown_tagged_fields,
140 })
141 }
142}
143
144impl Default for TxnOffsetCommitResponse {
145 fn default() -> Self {
146 Self {
147 throttle_time_ms: 0,
148 topics: Default::default(),
149 unknown_tagged_fields: BTreeMap::new(),
150 }
151 }
152}
153
154impl Message for TxnOffsetCommitResponse {
155 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
156 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
157}
158
159#[non_exhaustive]
161#[derive(Debug, Clone, PartialEq)]
162pub struct TxnOffsetCommitResponsePartition {
163 pub partition_index: i32,
167
168 pub error_code: i16,
172
173 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
175}
176
177impl TxnOffsetCommitResponsePartition {
178 pub fn with_partition_index(mut self, value: i32) -> Self {
184 self.partition_index = value;
185 self
186 }
187 pub fn with_error_code(mut self, value: i16) -> Self {
193 self.error_code = value;
194 self
195 }
196 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
198 self.unknown_tagged_fields = value;
199 self
200 }
201 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
203 self.unknown_tagged_fields.insert(key, value);
204 self
205 }
206}
207
208#[cfg(feature = "broker")]
209impl Encodable for TxnOffsetCommitResponsePartition {
210 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
211 types::Int32.encode(buf, &self.partition_index)?;
212 types::Int16.encode(buf, &self.error_code)?;
213 if version >= 3 {
214 let num_tagged_fields = self.unknown_tagged_fields.len();
215 if num_tagged_fields > std::u32::MAX as usize {
216 bail!(
217 "Too many tagged fields to encode ({} fields)",
218 num_tagged_fields
219 );
220 }
221 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
222
223 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
224 }
225 Ok(())
226 }
227 fn compute_size(&self, version: i16) -> Result<usize> {
228 let mut total_size = 0;
229 total_size += types::Int32.compute_size(&self.partition_index)?;
230 total_size += types::Int16.compute_size(&self.error_code)?;
231 if version >= 3 {
232 let num_tagged_fields = self.unknown_tagged_fields.len();
233 if num_tagged_fields > std::u32::MAX as usize {
234 bail!(
235 "Too many tagged fields to encode ({} fields)",
236 num_tagged_fields
237 );
238 }
239 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
240
241 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
242 }
243 Ok(total_size)
244 }
245}
246
247#[cfg(feature = "client")]
248impl Decodable for TxnOffsetCommitResponsePartition {
249 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
250 let partition_index = types::Int32.decode(buf)?;
251 let error_code = types::Int16.decode(buf)?;
252 let mut unknown_tagged_fields = BTreeMap::new();
253 if version >= 3 {
254 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
255 for _ in 0..num_tagged_fields {
256 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
257 let size: u32 = types::UnsignedVarInt.decode(buf)?;
258 let unknown_value = buf.try_get_bytes(size as usize)?;
259 unknown_tagged_fields.insert(tag as i32, unknown_value);
260 }
261 }
262 Ok(Self {
263 partition_index,
264 error_code,
265 unknown_tagged_fields,
266 })
267 }
268}
269
270impl Default for TxnOffsetCommitResponsePartition {
271 fn default() -> Self {
272 Self {
273 partition_index: 0,
274 error_code: 0,
275 unknown_tagged_fields: BTreeMap::new(),
276 }
277 }
278}
279
280impl Message for TxnOffsetCommitResponsePartition {
281 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
282 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
283}
284
285#[non_exhaustive]
287#[derive(Debug, Clone, PartialEq)]
288pub struct TxnOffsetCommitResponseTopic {
289 pub name: super::TopicName,
293
294 pub partitions: Vec<TxnOffsetCommitResponsePartition>,
298
299 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
301}
302
303impl TxnOffsetCommitResponseTopic {
304 pub fn with_name(mut self, value: super::TopicName) -> Self {
310 self.name = value;
311 self
312 }
313 pub fn with_partitions(mut self, value: Vec<TxnOffsetCommitResponsePartition>) -> Self {
319 self.partitions = value;
320 self
321 }
322 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
324 self.unknown_tagged_fields = value;
325 self
326 }
327 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
329 self.unknown_tagged_fields.insert(key, value);
330 self
331 }
332}
333
334#[cfg(feature = "broker")]
335impl Encodable for TxnOffsetCommitResponseTopic {
336 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
337 if version >= 3 {
338 types::CompactString.encode(buf, &self.name)?;
339 } else {
340 types::String.encode(buf, &self.name)?;
341 }
342 if version >= 3 {
343 types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
344 } else {
345 types::Array(types::Struct { version }).encode(buf, &self.partitions)?;
346 }
347 if version >= 3 {
348 let num_tagged_fields = self.unknown_tagged_fields.len();
349 if num_tagged_fields > std::u32::MAX as usize {
350 bail!(
351 "Too many tagged fields to encode ({} fields)",
352 num_tagged_fields
353 );
354 }
355 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
356
357 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
358 }
359 Ok(())
360 }
361 fn compute_size(&self, version: i16) -> Result<usize> {
362 let mut total_size = 0;
363 if version >= 3 {
364 total_size += types::CompactString.compute_size(&self.name)?;
365 } else {
366 total_size += types::String.compute_size(&self.name)?;
367 }
368 if version >= 3 {
369 total_size +=
370 types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
371 } else {
372 total_size += types::Array(types::Struct { version }).compute_size(&self.partitions)?;
373 }
374 if version >= 3 {
375 let num_tagged_fields = self.unknown_tagged_fields.len();
376 if num_tagged_fields > std::u32::MAX as usize {
377 bail!(
378 "Too many tagged fields to encode ({} fields)",
379 num_tagged_fields
380 );
381 }
382 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
383
384 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
385 }
386 Ok(total_size)
387 }
388}
389
390#[cfg(feature = "client")]
391impl Decodable for TxnOffsetCommitResponseTopic {
392 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
393 let name = if version >= 3 {
394 types::CompactString.decode(buf)?
395 } else {
396 types::String.decode(buf)?
397 };
398 let partitions = if version >= 3 {
399 types::CompactArray(types::Struct { version }).decode(buf)?
400 } else {
401 types::Array(types::Struct { version }).decode(buf)?
402 };
403 let mut unknown_tagged_fields = BTreeMap::new();
404 if version >= 3 {
405 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
406 for _ in 0..num_tagged_fields {
407 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
408 let size: u32 = types::UnsignedVarInt.decode(buf)?;
409 let unknown_value = buf.try_get_bytes(size as usize)?;
410 unknown_tagged_fields.insert(tag as i32, unknown_value);
411 }
412 }
413 Ok(Self {
414 name,
415 partitions,
416 unknown_tagged_fields,
417 })
418 }
419}
420
421impl Default for TxnOffsetCommitResponseTopic {
422 fn default() -> Self {
423 Self {
424 name: Default::default(),
425 partitions: Default::default(),
426 unknown_tagged_fields: BTreeMap::new(),
427 }
428 }
429}
430
431impl Message for TxnOffsetCommitResponseTopic {
432 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
433 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
434}
435
436impl HeaderVersion for TxnOffsetCommitResponse {
437 fn header_version(version: i16) -> i16 {
438 if version >= 3 {
439 1
440 } else {
441 0
442 }
443 }
444}