kafka_protocol/messages/
init_producer_id_request.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 InitProducerIdRequest {
24 pub transactional_id: Option<super::TransactionalId>,
28
29 pub transaction_timeout_ms: i32,
33
34 pub producer_id: super::ProducerId,
38
39 pub producer_epoch: i16,
43
44 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl InitProducerIdRequest {
49 pub fn with_transactional_id(mut self, value: Option<super::TransactionalId>) -> Self {
55 self.transactional_id = value;
56 self
57 }
58 pub fn with_transaction_timeout_ms(mut self, value: i32) -> Self {
64 self.transaction_timeout_ms = value;
65 self
66 }
67 pub fn with_producer_id(mut self, value: super::ProducerId) -> Self {
73 self.producer_id = value;
74 self
75 }
76 pub fn with_producer_epoch(mut self, value: i16) -> Self {
82 self.producer_epoch = 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 = "client")]
98impl Encodable for InitProducerIdRequest {
99 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100 if version < 0 || version > 5 {
101 bail!("specified version not supported by this message type");
102 }
103 if version >= 2 {
104 types::CompactString.encode(buf, &self.transactional_id)?;
105 } else {
106 types::String.encode(buf, &self.transactional_id)?;
107 }
108 types::Int32.encode(buf, &self.transaction_timeout_ms)?;
109 if version >= 3 {
110 types::Int64.encode(buf, &self.producer_id)?;
111 } else {
112 if self.producer_id != -1 {
113 bail!("A field is set that is not available on the selected protocol version");
114 }
115 }
116 if version >= 3 {
117 types::Int16.encode(buf, &self.producer_epoch)?;
118 } else {
119 if self.producer_epoch != -1 {
120 bail!("A field is set that is not available on the selected protocol version");
121 }
122 }
123 if version >= 2 {
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 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
132
133 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
134 }
135 Ok(())
136 }
137 fn compute_size(&self, version: i16) -> Result<usize> {
138 let mut total_size = 0;
139 if version >= 2 {
140 total_size += types::CompactString.compute_size(&self.transactional_id)?;
141 } else {
142 total_size += types::String.compute_size(&self.transactional_id)?;
143 }
144 total_size += types::Int32.compute_size(&self.transaction_timeout_ms)?;
145 if version >= 3 {
146 total_size += types::Int64.compute_size(&self.producer_id)?;
147 } else {
148 if self.producer_id != -1 {
149 bail!("A field is set that is not available on the selected protocol version");
150 }
151 }
152 if version >= 3 {
153 total_size += types::Int16.compute_size(&self.producer_epoch)?;
154 } else {
155 if self.producer_epoch != -1 {
156 bail!("A field is set that is not available on the selected protocol version");
157 }
158 }
159 if version >= 2 {
160 let num_tagged_fields = self.unknown_tagged_fields.len();
161 if num_tagged_fields > std::u32::MAX as usize {
162 bail!(
163 "Too many tagged fields to encode ({} fields)",
164 num_tagged_fields
165 );
166 }
167 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
168
169 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
170 }
171 Ok(total_size)
172 }
173}
174
175#[cfg(feature = "broker")]
176impl Decodable for InitProducerIdRequest {
177 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
178 if version < 0 || version > 5 {
179 bail!("specified version not supported by this message type");
180 }
181 let transactional_id = if version >= 2 {
182 types::CompactString.decode(buf)?
183 } else {
184 types::String.decode(buf)?
185 };
186 let transaction_timeout_ms = types::Int32.decode(buf)?;
187 let producer_id = if version >= 3 {
188 types::Int64.decode(buf)?
189 } else {
190 (-1).into()
191 };
192 let producer_epoch = if version >= 3 {
193 types::Int16.decode(buf)?
194 } else {
195 -1
196 };
197 let mut unknown_tagged_fields = BTreeMap::new();
198 if version >= 2 {
199 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
200 for _ in 0..num_tagged_fields {
201 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
202 let size: u32 = types::UnsignedVarInt.decode(buf)?;
203 let unknown_value = buf.try_get_bytes(size as usize)?;
204 unknown_tagged_fields.insert(tag as i32, unknown_value);
205 }
206 }
207 Ok(Self {
208 transactional_id,
209 transaction_timeout_ms,
210 producer_id,
211 producer_epoch,
212 unknown_tagged_fields,
213 })
214 }
215}
216
217impl Default for InitProducerIdRequest {
218 fn default() -> Self {
219 Self {
220 transactional_id: Some(Default::default()),
221 transaction_timeout_ms: 0,
222 producer_id: (-1).into(),
223 producer_epoch: -1,
224 unknown_tagged_fields: BTreeMap::new(),
225 }
226 }
227}
228
229impl Message for InitProducerIdRequest {
230 const VERSIONS: VersionRange = VersionRange { min: 0, max: 5 };
231 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
232}
233
234impl HeaderVersion for InitProducerIdRequest {
235 fn header_version(version: i16) -> i16 {
236 if version >= 2 {
237 2
238 } else {
239 1
240 }
241 }
242}