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 WritableTxnMarkerPartitionResult {
24 pub partition_index: i32,
28
29 pub error_code: i16,
33
34 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl WritableTxnMarkerPartitionResult {
39 pub fn with_partition_index(mut self, value: i32) -> Self {
45 self.partition_index = value;
46 self
47 }
48 pub fn with_error_code(mut self, value: i16) -> Self {
54 self.error_code = 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 WritableTxnMarkerPartitionResult {
71 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
72 types::Int32.encode(buf, &self.partition_index)?;
73 types::Int16.encode(buf, &self.error_code)?;
74 if version >= 1 {
75 let num_tagged_fields = self.unknown_tagged_fields.len();
76 if num_tagged_fields > std::u32::MAX as usize {
77 bail!(
78 "Too many tagged fields to encode ({} fields)",
79 num_tagged_fields
80 );
81 }
82 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
83
84 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
85 }
86 Ok(())
87 }
88 fn compute_size(&self, version: i16) -> Result<usize> {
89 let mut total_size = 0;
90 total_size += types::Int32.compute_size(&self.partition_index)?;
91 total_size += types::Int16.compute_size(&self.error_code)?;
92 if version >= 1 {
93 let num_tagged_fields = self.unknown_tagged_fields.len();
94 if num_tagged_fields > std::u32::MAX as usize {
95 bail!(
96 "Too many tagged fields to encode ({} fields)",
97 num_tagged_fields
98 );
99 }
100 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
101
102 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
103 }
104 Ok(total_size)
105 }
106}
107
108#[cfg(feature = "client")]
109impl Decodable for WritableTxnMarkerPartitionResult {
110 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
111 let partition_index = types::Int32.decode(buf)?;
112 let error_code = types::Int16.decode(buf)?;
113 let mut unknown_tagged_fields = BTreeMap::new();
114 if version >= 1 {
115 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
116 for _ in 0..num_tagged_fields {
117 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
118 let size: u32 = types::UnsignedVarInt.decode(buf)?;
119 let unknown_value = buf.try_get_bytes(size as usize)?;
120 unknown_tagged_fields.insert(tag as i32, unknown_value);
121 }
122 }
123 Ok(Self {
124 partition_index,
125 error_code,
126 unknown_tagged_fields,
127 })
128 }
129}
130
131impl Default for WritableTxnMarkerPartitionResult {
132 fn default() -> Self {
133 Self {
134 partition_index: 0,
135 error_code: 0,
136 unknown_tagged_fields: BTreeMap::new(),
137 }
138 }
139}
140
141impl Message for WritableTxnMarkerPartitionResult {
142 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
143 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
144}
145
146#[non_exhaustive]
148#[derive(Debug, Clone, PartialEq)]
149pub struct WritableTxnMarkerResult {
150 pub producer_id: super::ProducerId,
154
155 pub topics: Vec<WritableTxnMarkerTopicResult>,
159
160 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
162}
163
164impl WritableTxnMarkerResult {
165 pub fn with_producer_id(mut self, value: super::ProducerId) -> Self {
171 self.producer_id = value;
172 self
173 }
174 pub fn with_topics(mut self, value: Vec<WritableTxnMarkerTopicResult>) -> Self {
180 self.topics = value;
181 self
182 }
183 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
185 self.unknown_tagged_fields = value;
186 self
187 }
188 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
190 self.unknown_tagged_fields.insert(key, value);
191 self
192 }
193}
194
195#[cfg(feature = "broker")]
196impl Encodable for WritableTxnMarkerResult {
197 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
198 types::Int64.encode(buf, &self.producer_id)?;
199 if version >= 1 {
200 types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
201 } else {
202 types::Array(types::Struct { version }).encode(buf, &self.topics)?;
203 }
204 if version >= 1 {
205 let num_tagged_fields = self.unknown_tagged_fields.len();
206 if num_tagged_fields > std::u32::MAX as usize {
207 bail!(
208 "Too many tagged fields to encode ({} fields)",
209 num_tagged_fields
210 );
211 }
212 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
213
214 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
215 }
216 Ok(())
217 }
218 fn compute_size(&self, version: i16) -> Result<usize> {
219 let mut total_size = 0;
220 total_size += types::Int64.compute_size(&self.producer_id)?;
221 if version >= 1 {
222 total_size +=
223 types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
224 } else {
225 total_size += types::Array(types::Struct { version }).compute_size(&self.topics)?;
226 }
227 if version >= 1 {
228 let num_tagged_fields = self.unknown_tagged_fields.len();
229 if num_tagged_fields > std::u32::MAX as usize {
230 bail!(
231 "Too many tagged fields to encode ({} fields)",
232 num_tagged_fields
233 );
234 }
235 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
236
237 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
238 }
239 Ok(total_size)
240 }
241}
242
243#[cfg(feature = "client")]
244impl Decodable for WritableTxnMarkerResult {
245 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
246 let producer_id = types::Int64.decode(buf)?;
247 let topics = if version >= 1 {
248 types::CompactArray(types::Struct { version }).decode(buf)?
249 } else {
250 types::Array(types::Struct { version }).decode(buf)?
251 };
252 let mut unknown_tagged_fields = BTreeMap::new();
253 if version >= 1 {
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 producer_id,
264 topics,
265 unknown_tagged_fields,
266 })
267 }
268}
269
270impl Default for WritableTxnMarkerResult {
271 fn default() -> Self {
272 Self {
273 producer_id: (0).into(),
274 topics: Default::default(),
275 unknown_tagged_fields: BTreeMap::new(),
276 }
277 }
278}
279
280impl Message for WritableTxnMarkerResult {
281 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
282 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
283}
284
285#[non_exhaustive]
287#[derive(Debug, Clone, PartialEq)]
288pub struct WritableTxnMarkerTopicResult {
289 pub name: super::TopicName,
293
294 pub partitions: Vec<WritableTxnMarkerPartitionResult>,
298
299 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
301}
302
303impl WritableTxnMarkerTopicResult {
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<WritableTxnMarkerPartitionResult>) -> 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 WritableTxnMarkerTopicResult {
336 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
337 if version >= 1 {
338 types::CompactString.encode(buf, &self.name)?;
339 } else {
340 types::String.encode(buf, &self.name)?;
341 }
342 if version >= 1 {
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 >= 1 {
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 >= 1 {
364 total_size += types::CompactString.compute_size(&self.name)?;
365 } else {
366 total_size += types::String.compute_size(&self.name)?;
367 }
368 if version >= 1 {
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 >= 1 {
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 WritableTxnMarkerTopicResult {
392 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
393 let name = if version >= 1 {
394 types::CompactString.decode(buf)?
395 } else {
396 types::String.decode(buf)?
397 };
398 let partitions = if version >= 1 {
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 >= 1 {
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 WritableTxnMarkerTopicResult {
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 WritableTxnMarkerTopicResult {
432 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
433 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
434}
435
436#[non_exhaustive]
438#[derive(Debug, Clone, PartialEq)]
439pub struct WriteTxnMarkersResponse {
440 pub markers: Vec<WritableTxnMarkerResult>,
444
445 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
447}
448
449impl WriteTxnMarkersResponse {
450 pub fn with_markers(mut self, value: Vec<WritableTxnMarkerResult>) -> Self {
456 self.markers = value;
457 self
458 }
459 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
461 self.unknown_tagged_fields = value;
462 self
463 }
464 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
466 self.unknown_tagged_fields.insert(key, value);
467 self
468 }
469}
470
471#[cfg(feature = "broker")]
472impl Encodable for WriteTxnMarkersResponse {
473 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
474 if version >= 1 {
475 types::CompactArray(types::Struct { version }).encode(buf, &self.markers)?;
476 } else {
477 types::Array(types::Struct { version }).encode(buf, &self.markers)?;
478 }
479 if version >= 1 {
480 let num_tagged_fields = self.unknown_tagged_fields.len();
481 if num_tagged_fields > std::u32::MAX as usize {
482 bail!(
483 "Too many tagged fields to encode ({} fields)",
484 num_tagged_fields
485 );
486 }
487 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
488
489 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
490 }
491 Ok(())
492 }
493 fn compute_size(&self, version: i16) -> Result<usize> {
494 let mut total_size = 0;
495 if version >= 1 {
496 total_size +=
497 types::CompactArray(types::Struct { version }).compute_size(&self.markers)?;
498 } else {
499 total_size += types::Array(types::Struct { version }).compute_size(&self.markers)?;
500 }
501 if version >= 1 {
502 let num_tagged_fields = self.unknown_tagged_fields.len();
503 if num_tagged_fields > std::u32::MAX as usize {
504 bail!(
505 "Too many tagged fields to encode ({} fields)",
506 num_tagged_fields
507 );
508 }
509 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
510
511 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
512 }
513 Ok(total_size)
514 }
515}
516
517#[cfg(feature = "client")]
518impl Decodable for WriteTxnMarkersResponse {
519 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
520 let markers = if version >= 1 {
521 types::CompactArray(types::Struct { version }).decode(buf)?
522 } else {
523 types::Array(types::Struct { version }).decode(buf)?
524 };
525 let mut unknown_tagged_fields = BTreeMap::new();
526 if version >= 1 {
527 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
528 for _ in 0..num_tagged_fields {
529 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
530 let size: u32 = types::UnsignedVarInt.decode(buf)?;
531 let unknown_value = buf.try_get_bytes(size as usize)?;
532 unknown_tagged_fields.insert(tag as i32, unknown_value);
533 }
534 }
535 Ok(Self {
536 markers,
537 unknown_tagged_fields,
538 })
539 }
540}
541
542impl Default for WriteTxnMarkersResponse {
543 fn default() -> Self {
544 Self {
545 markers: Default::default(),
546 unknown_tagged_fields: BTreeMap::new(),
547 }
548 }
549}
550
551impl Message for WriteTxnMarkersResponse {
552 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
553 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
554}
555
556impl HeaderVersion for WriteTxnMarkersResponse {
557 fn header_version(version: i16) -> i16 {
558 if version >= 1 {
559 1
560 } else {
561 0
562 }
563 }
564}