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 CreatePartitionsResponse {
24 pub throttle_time_ms: i32,
28
29 pub results: Vec<CreatePartitionsTopicResult>,
33
34 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl CreatePartitionsResponse {
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_results(mut self, value: Vec<CreatePartitionsTopicResult>) -> Self {
54 self.results = 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 CreatePartitionsResponse {
71 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
72 types::Int32.encode(buf, &self.throttle_time_ms)?;
73 if version >= 2 {
74 types::CompactArray(types::Struct { version }).encode(buf, &self.results)?;
75 } else {
76 types::Array(types::Struct { version }).encode(buf, &self.results)?;
77 }
78 if version >= 2 {
79 let num_tagged_fields = self.unknown_tagged_fields.len();
80 if num_tagged_fields > std::u32::MAX as usize {
81 bail!(
82 "Too many tagged fields to encode ({} fields)",
83 num_tagged_fields
84 );
85 }
86 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
87
88 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
89 }
90 Ok(())
91 }
92 fn compute_size(&self, version: i16) -> Result<usize> {
93 let mut total_size = 0;
94 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
95 if version >= 2 {
96 total_size +=
97 types::CompactArray(types::Struct { version }).compute_size(&self.results)?;
98 } else {
99 total_size += types::Array(types::Struct { version }).compute_size(&self.results)?;
100 }
101 if version >= 2 {
102 let num_tagged_fields = self.unknown_tagged_fields.len();
103 if num_tagged_fields > std::u32::MAX as usize {
104 bail!(
105 "Too many tagged fields to encode ({} fields)",
106 num_tagged_fields
107 );
108 }
109 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
110
111 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
112 }
113 Ok(total_size)
114 }
115}
116
117#[cfg(feature = "client")]
118impl Decodable for CreatePartitionsResponse {
119 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
120 let throttle_time_ms = types::Int32.decode(buf)?;
121 let results = if version >= 2 {
122 types::CompactArray(types::Struct { version }).decode(buf)?
123 } else {
124 types::Array(types::Struct { version }).decode(buf)?
125 };
126 let mut unknown_tagged_fields = BTreeMap::new();
127 if version >= 2 {
128 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
129 for _ in 0..num_tagged_fields {
130 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
131 let size: u32 = types::UnsignedVarInt.decode(buf)?;
132 let unknown_value = buf.try_get_bytes(size as usize)?;
133 unknown_tagged_fields.insert(tag as i32, unknown_value);
134 }
135 }
136 Ok(Self {
137 throttle_time_ms,
138 results,
139 unknown_tagged_fields,
140 })
141 }
142}
143
144impl Default for CreatePartitionsResponse {
145 fn default() -> Self {
146 Self {
147 throttle_time_ms: 0,
148 results: Default::default(),
149 unknown_tagged_fields: BTreeMap::new(),
150 }
151 }
152}
153
154impl Message for CreatePartitionsResponse {
155 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
156 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
157}
158
159#[non_exhaustive]
161#[derive(Debug, Clone, PartialEq)]
162pub struct CreatePartitionsTopicResult {
163 pub name: super::TopicName,
167
168 pub error_code: i16,
172
173 pub error_message: Option<StrBytes>,
177
178 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
180}
181
182impl CreatePartitionsTopicResult {
183 pub fn with_name(mut self, value: super::TopicName) -> Self {
189 self.name = value;
190 self
191 }
192 pub fn with_error_code(mut self, value: i16) -> Self {
198 self.error_code = value;
199 self
200 }
201 pub fn with_error_message(mut self, value: Option<StrBytes>) -> Self {
207 self.error_message = value;
208 self
209 }
210 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
212 self.unknown_tagged_fields = value;
213 self
214 }
215 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
217 self.unknown_tagged_fields.insert(key, value);
218 self
219 }
220}
221
222#[cfg(feature = "broker")]
223impl Encodable for CreatePartitionsTopicResult {
224 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
225 if version >= 2 {
226 types::CompactString.encode(buf, &self.name)?;
227 } else {
228 types::String.encode(buf, &self.name)?;
229 }
230 types::Int16.encode(buf, &self.error_code)?;
231 if version >= 2 {
232 types::CompactString.encode(buf, &self.error_message)?;
233 } else {
234 types::String.encode(buf, &self.error_message)?;
235 }
236 if version >= 2 {
237 let num_tagged_fields = self.unknown_tagged_fields.len();
238 if num_tagged_fields > std::u32::MAX as usize {
239 bail!(
240 "Too many tagged fields to encode ({} fields)",
241 num_tagged_fields
242 );
243 }
244 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
245
246 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
247 }
248 Ok(())
249 }
250 fn compute_size(&self, version: i16) -> Result<usize> {
251 let mut total_size = 0;
252 if version >= 2 {
253 total_size += types::CompactString.compute_size(&self.name)?;
254 } else {
255 total_size += types::String.compute_size(&self.name)?;
256 }
257 total_size += types::Int16.compute_size(&self.error_code)?;
258 if version >= 2 {
259 total_size += types::CompactString.compute_size(&self.error_message)?;
260 } else {
261 total_size += types::String.compute_size(&self.error_message)?;
262 }
263 if version >= 2 {
264 let num_tagged_fields = self.unknown_tagged_fields.len();
265 if num_tagged_fields > std::u32::MAX as usize {
266 bail!(
267 "Too many tagged fields to encode ({} fields)",
268 num_tagged_fields
269 );
270 }
271 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
272
273 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
274 }
275 Ok(total_size)
276 }
277}
278
279#[cfg(feature = "client")]
280impl Decodable for CreatePartitionsTopicResult {
281 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
282 let name = if version >= 2 {
283 types::CompactString.decode(buf)?
284 } else {
285 types::String.decode(buf)?
286 };
287 let error_code = types::Int16.decode(buf)?;
288 let error_message = if version >= 2 {
289 types::CompactString.decode(buf)?
290 } else {
291 types::String.decode(buf)?
292 };
293 let mut unknown_tagged_fields = BTreeMap::new();
294 if version >= 2 {
295 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
296 for _ in 0..num_tagged_fields {
297 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
298 let size: u32 = types::UnsignedVarInt.decode(buf)?;
299 let unknown_value = buf.try_get_bytes(size as usize)?;
300 unknown_tagged_fields.insert(tag as i32, unknown_value);
301 }
302 }
303 Ok(Self {
304 name,
305 error_code,
306 error_message,
307 unknown_tagged_fields,
308 })
309 }
310}
311
312impl Default for CreatePartitionsTopicResult {
313 fn default() -> Self {
314 Self {
315 name: Default::default(),
316 error_code: 0,
317 error_message: None,
318 unknown_tagged_fields: BTreeMap::new(),
319 }
320 }
321}
322
323impl Message for CreatePartitionsTopicResult {
324 const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
325 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
326}
327
328impl HeaderVersion for CreatePartitionsResponse {
329 fn header_version(version: i16) -> i16 {
330 if version >= 2 {
331 1
332 } else {
333 0
334 }
335 }
336}