kafka_protocol/messages/
allocate_producer_ids_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 AllocateProducerIdsResponse {
24 pub throttle_time_ms: i32,
28
29 pub error_code: i16,
33
34 pub producer_id_start: super::ProducerId,
38
39 pub producer_id_len: i32,
43
44 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl AllocateProducerIdsResponse {
49 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
55 self.throttle_time_ms = value;
56 self
57 }
58 pub fn with_error_code(mut self, value: i16) -> Self {
64 self.error_code = value;
65 self
66 }
67 pub fn with_producer_id_start(mut self, value: super::ProducerId) -> Self {
73 self.producer_id_start = value;
74 self
75 }
76 pub fn with_producer_id_len(mut self, value: i32) -> Self {
82 self.producer_id_len = 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 = "broker")]
98impl Encodable for AllocateProducerIdsResponse {
99 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100 if version != 0 {
101 bail!("specified version not supported by this message type");
102 }
103 types::Int32.encode(buf, &self.throttle_time_ms)?;
104 types::Int16.encode(buf, &self.error_code)?;
105 types::Int64.encode(buf, &self.producer_id_start)?;
106 types::Int32.encode(buf, &self.producer_id_len)?;
107 let num_tagged_fields = self.unknown_tagged_fields.len();
108 if num_tagged_fields > std::u32::MAX as usize {
109 bail!(
110 "Too many tagged fields to encode ({} fields)",
111 num_tagged_fields
112 );
113 }
114 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
115
116 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
117 Ok(())
118 }
119 fn compute_size(&self, version: i16) -> Result<usize> {
120 let mut total_size = 0;
121 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
122 total_size += types::Int16.compute_size(&self.error_code)?;
123 total_size += types::Int64.compute_size(&self.producer_id_start)?;
124 total_size += types::Int32.compute_size(&self.producer_id_len)?;
125 let num_tagged_fields = self.unknown_tagged_fields.len();
126 if num_tagged_fields > std::u32::MAX as usize {
127 bail!(
128 "Too many tagged fields to encode ({} fields)",
129 num_tagged_fields
130 );
131 }
132 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
133
134 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
135 Ok(total_size)
136 }
137}
138
139#[cfg(feature = "client")]
140impl Decodable for AllocateProducerIdsResponse {
141 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
142 if version != 0 {
143 bail!("specified version not supported by this message type");
144 }
145 let throttle_time_ms = types::Int32.decode(buf)?;
146 let error_code = types::Int16.decode(buf)?;
147 let producer_id_start = types::Int64.decode(buf)?;
148 let producer_id_len = types::Int32.decode(buf)?;
149 let mut unknown_tagged_fields = BTreeMap::new();
150 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
151 for _ in 0..num_tagged_fields {
152 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
153 let size: u32 = types::UnsignedVarInt.decode(buf)?;
154 let unknown_value = buf.try_get_bytes(size as usize)?;
155 unknown_tagged_fields.insert(tag as i32, unknown_value);
156 }
157 Ok(Self {
158 throttle_time_ms,
159 error_code,
160 producer_id_start,
161 producer_id_len,
162 unknown_tagged_fields,
163 })
164 }
165}
166
167impl Default for AllocateProducerIdsResponse {
168 fn default() -> Self {
169 Self {
170 throttle_time_ms: 0,
171 error_code: 0,
172 producer_id_start: (0).into(),
173 producer_id_len: 0,
174 unknown_tagged_fields: BTreeMap::new(),
175 }
176 }
177}
178
179impl Message for AllocateProducerIdsResponse {
180 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
181 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
182}
183
184impl HeaderVersion for AllocateProducerIdsResponse {
185 fn header_version(version: i16) -> i16 {
186 1
187 }
188}