kafka_protocol/messages/
init_producer_id_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 InitProducerIdResponse {
24 pub throttle_time_ms: i32,
28
29 pub error_code: i16,
33
34 pub producer_id: super::ProducerId,
38
39 pub producer_epoch: i16,
43
44 pub ongoing_txn_producer_id: super::ProducerId,
48
49 pub ongoing_txn_producer_epoch: i16,
53
54 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
56}
57
58impl InitProducerIdResponse {
59 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
65 self.throttle_time_ms = value;
66 self
67 }
68 pub fn with_error_code(mut self, value: i16) -> Self {
74 self.error_code = value;
75 self
76 }
77 pub fn with_producer_id(mut self, value: super::ProducerId) -> Self {
83 self.producer_id = value;
84 self
85 }
86 pub fn with_producer_epoch(mut self, value: i16) -> Self {
92 self.producer_epoch = value;
93 self
94 }
95 pub fn with_ongoing_txn_producer_id(mut self, value: super::ProducerId) -> Self {
101 self.ongoing_txn_producer_id = value;
102 self
103 }
104 pub fn with_ongoing_txn_producer_epoch(mut self, value: i16) -> Self {
110 self.ongoing_txn_producer_epoch = value;
111 self
112 }
113 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
115 self.unknown_tagged_fields = value;
116 self
117 }
118 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
120 self.unknown_tagged_fields.insert(key, value);
121 self
122 }
123}
124
125#[cfg(feature = "broker")]
126impl Encodable for InitProducerIdResponse {
127 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
128 if version < 0 || version > 6 {
129 bail!("specified version not supported by this message type");
130 }
131 types::Int32.encode(buf, &self.throttle_time_ms)?;
132 types::Int16.encode(buf, &self.error_code)?;
133 types::Int64.encode(buf, &self.producer_id)?;
134 types::Int16.encode(buf, &self.producer_epoch)?;
135 if version >= 6 {
136 types::Int64.encode(buf, &self.ongoing_txn_producer_id)?;
137 } else {
138 if self.ongoing_txn_producer_id != -1 {
139 bail!("A field is set that is not available on the selected protocol version");
140 }
141 }
142 if version >= 6 {
143 types::Int16.encode(buf, &self.ongoing_txn_producer_epoch)?;
144 } else {
145 if self.ongoing_txn_producer_epoch != -1 {
146 bail!("A field is set that is not available on the selected protocol version");
147 }
148 }
149 if version >= 2 {
150 let num_tagged_fields = self.unknown_tagged_fields.len();
151 if num_tagged_fields > std::u32::MAX as usize {
152 bail!(
153 "Too many tagged fields to encode ({} fields)",
154 num_tagged_fields
155 );
156 }
157 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
158
159 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
160 }
161 Ok(())
162 }
163 fn compute_size(&self, version: i16) -> Result<usize> {
164 let mut total_size = 0;
165 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
166 total_size += types::Int16.compute_size(&self.error_code)?;
167 total_size += types::Int64.compute_size(&self.producer_id)?;
168 total_size += types::Int16.compute_size(&self.producer_epoch)?;
169 if version >= 6 {
170 total_size += types::Int64.compute_size(&self.ongoing_txn_producer_id)?;
171 } else {
172 if self.ongoing_txn_producer_id != -1 {
173 bail!("A field is set that is not available on the selected protocol version");
174 }
175 }
176 if version >= 6 {
177 total_size += types::Int16.compute_size(&self.ongoing_txn_producer_epoch)?;
178 } else {
179 if self.ongoing_txn_producer_epoch != -1 {
180 bail!("A field is set that is not available on the selected protocol version");
181 }
182 }
183 if version >= 2 {
184 let num_tagged_fields = self.unknown_tagged_fields.len();
185 if num_tagged_fields > std::u32::MAX as usize {
186 bail!(
187 "Too many tagged fields to encode ({} fields)",
188 num_tagged_fields
189 );
190 }
191 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
192
193 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
194 }
195 Ok(total_size)
196 }
197}
198
199#[cfg(feature = "client")]
200impl Decodable for InitProducerIdResponse {
201 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
202 if version < 0 || version > 6 {
203 bail!("specified version not supported by this message type");
204 }
205 let throttle_time_ms = types::Int32.decode(buf)?;
206 let error_code = types::Int16.decode(buf)?;
207 let producer_id = types::Int64.decode(buf)?;
208 let producer_epoch = types::Int16.decode(buf)?;
209 let ongoing_txn_producer_id = if version >= 6 {
210 types::Int64.decode(buf)?
211 } else {
212 (-1).into()
213 };
214 let ongoing_txn_producer_epoch = if version >= 6 {
215 types::Int16.decode(buf)?
216 } else {
217 -1
218 };
219 let mut unknown_tagged_fields = BTreeMap::new();
220 if version >= 2 {
221 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
222 for _ in 0..num_tagged_fields {
223 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
224 let size: u32 = types::UnsignedVarInt.decode(buf)?;
225 let unknown_value = buf.try_get_bytes(size as usize)?;
226 unknown_tagged_fields.insert(tag as i32, unknown_value);
227 }
228 }
229 Ok(Self {
230 throttle_time_ms,
231 error_code,
232 producer_id,
233 producer_epoch,
234 ongoing_txn_producer_id,
235 ongoing_txn_producer_epoch,
236 unknown_tagged_fields,
237 })
238 }
239}
240
241impl Default for InitProducerIdResponse {
242 fn default() -> Self {
243 Self {
244 throttle_time_ms: 0,
245 error_code: 0,
246 producer_id: (-1).into(),
247 producer_epoch: 0,
248 ongoing_txn_producer_id: (-1).into(),
249 ongoing_txn_producer_epoch: -1,
250 unknown_tagged_fields: BTreeMap::new(),
251 }
252 }
253}
254
255impl Message for InitProducerIdResponse {
256 const VERSIONS: VersionRange = VersionRange { min: 0, max: 6 };
257 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
258}
259
260impl HeaderVersion for InitProducerIdResponse {
261 fn header_version(version: i16) -> i16 {
262 if version >= 2 {
263 1
264 } else {
265 0
266 }
267 }
268}