kafka_protocol/messages/
add_offsets_to_txn_response.rs1#![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 AddOffsetsToTxnResponse {
24 pub throttle_time_ms: i32,
28
29 pub error_code: i16,
33
34 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl AddOffsetsToTxnResponse {
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_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 AddOffsetsToTxnResponse {
71 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
72 if version < 0 || version > 4 {
73 bail!("specified version not supported by this message type");
74 }
75 types::Int32.encode(buf, &self.throttle_time_ms)?;
76 types::Int16.encode(buf, &self.error_code)?;
77 if version >= 3 {
78 let num_tagged_fields = self.unknown_tagged_fields.len();
79 if num_tagged_fields > std::u32::MAX as usize {
80 bail!(
81 "Too many tagged fields to encode ({} fields)",
82 num_tagged_fields
83 );
84 }
85 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
86
87 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
88 }
89 Ok(())
90 }
91 fn compute_size(&self, version: i16) -> Result<usize> {
92 let mut total_size = 0;
93 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
94 total_size += types::Int16.compute_size(&self.error_code)?;
95 if version >= 3 {
96 let num_tagged_fields = self.unknown_tagged_fields.len();
97 if num_tagged_fields > std::u32::MAX as usize {
98 bail!(
99 "Too many tagged fields to encode ({} fields)",
100 num_tagged_fields
101 );
102 }
103 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
104
105 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
106 }
107 Ok(total_size)
108 }
109}
110
111#[cfg(feature = "client")]
112impl Decodable for AddOffsetsToTxnResponse {
113 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
114 if version < 0 || version > 4 {
115 bail!("specified version not supported by this message type");
116 }
117 let throttle_time_ms = types::Int32.decode(buf)?;
118 let error_code = types::Int16.decode(buf)?;
119 let mut unknown_tagged_fields = BTreeMap::new();
120 if version >= 3 {
121 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
122 for _ in 0..num_tagged_fields {
123 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
124 let size: u32 = types::UnsignedVarInt.decode(buf)?;
125 let unknown_value = buf.try_get_bytes(size as usize)?;
126 unknown_tagged_fields.insert(tag as i32, unknown_value);
127 }
128 }
129 Ok(Self {
130 throttle_time_ms,
131 error_code,
132 unknown_tagged_fields,
133 })
134 }
135}
136
137impl Default for AddOffsetsToTxnResponse {
138 fn default() -> Self {
139 Self {
140 throttle_time_ms: 0,
141 error_code: 0,
142 unknown_tagged_fields: BTreeMap::new(),
143 }
144 }
145}
146
147impl Message for AddOffsetsToTxnResponse {
148 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
149 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
150}
151
152impl HeaderVersion for AddOffsetsToTxnResponse {
153 fn header_version(version: i16) -> i16 {
154 if version >= 3 {
155 1
156 } else {
157 0
158 }
159 }
160}