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 DeletableTopicResult {
24 pub name: Option<super::TopicName>,
28
29 pub topic_id: Uuid,
33
34 pub error_code: i16,
38
39 pub error_message: Option<StrBytes>,
43
44 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl DeletableTopicResult {
49 pub fn with_name(mut self, value: Option<super::TopicName>) -> Self {
55 self.name = value;
56 self
57 }
58 pub fn with_topic_id(mut self, value: Uuid) -> Self {
64 self.topic_id = value;
65 self
66 }
67 pub fn with_error_code(mut self, value: i16) -> Self {
73 self.error_code = value;
74 self
75 }
76 pub fn with_error_message(mut self, value: Option<StrBytes>) -> Self {
82 self.error_message = 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 DeletableTopicResult {
99 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100 if version >= 4 {
101 types::CompactString.encode(buf, &self.name)?;
102 } else {
103 types::String.encode(buf, &self.name)?;
104 }
105 if version >= 6 {
106 types::Uuid.encode(buf, &self.topic_id)?;
107 }
108 types::Int16.encode(buf, &self.error_code)?;
109 if version >= 5 {
110 types::CompactString.encode(buf, &self.error_message)?;
111 }
112 if version >= 4 {
113 let num_tagged_fields = self.unknown_tagged_fields.len();
114 if num_tagged_fields > std::u32::MAX as usize {
115 bail!(
116 "Too many tagged fields to encode ({} fields)",
117 num_tagged_fields
118 );
119 }
120 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
121
122 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
123 }
124 Ok(())
125 }
126 fn compute_size(&self, version: i16) -> Result<usize> {
127 let mut total_size = 0;
128 if version >= 4 {
129 total_size += types::CompactString.compute_size(&self.name)?;
130 } else {
131 total_size += types::String.compute_size(&self.name)?;
132 }
133 if version >= 6 {
134 total_size += types::Uuid.compute_size(&self.topic_id)?;
135 }
136 total_size += types::Int16.compute_size(&self.error_code)?;
137 if version >= 5 {
138 total_size += types::CompactString.compute_size(&self.error_message)?;
139 }
140 if version >= 4 {
141 let num_tagged_fields = self.unknown_tagged_fields.len();
142 if num_tagged_fields > std::u32::MAX as usize {
143 bail!(
144 "Too many tagged fields to encode ({} fields)",
145 num_tagged_fields
146 );
147 }
148 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
149
150 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
151 }
152 Ok(total_size)
153 }
154}
155
156#[cfg(feature = "client")]
157impl Decodable for DeletableTopicResult {
158 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
159 let name = if version >= 4 {
160 types::CompactString.decode(buf)?
161 } else {
162 types::String.decode(buf)?
163 };
164 let topic_id = if version >= 6 {
165 types::Uuid.decode(buf)?
166 } else {
167 Uuid::nil()
168 };
169 let error_code = types::Int16.decode(buf)?;
170 let error_message = if version >= 5 {
171 types::CompactString.decode(buf)?
172 } else {
173 None
174 };
175 let mut unknown_tagged_fields = BTreeMap::new();
176 if version >= 4 {
177 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
178 for _ in 0..num_tagged_fields {
179 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
180 let size: u32 = types::UnsignedVarInt.decode(buf)?;
181 let unknown_value = buf.try_get_bytes(size as usize)?;
182 unknown_tagged_fields.insert(tag as i32, unknown_value);
183 }
184 }
185 Ok(Self {
186 name,
187 topic_id,
188 error_code,
189 error_message,
190 unknown_tagged_fields,
191 })
192 }
193}
194
195impl Default for DeletableTopicResult {
196 fn default() -> Self {
197 Self {
198 name: Some(Default::default()),
199 topic_id: Uuid::nil(),
200 error_code: 0,
201 error_message: None,
202 unknown_tagged_fields: BTreeMap::new(),
203 }
204 }
205}
206
207impl Message for DeletableTopicResult {
208 const VERSIONS: VersionRange = VersionRange { min: 0, max: 6 };
209 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
210}
211
212#[non_exhaustive]
214#[derive(Debug, Clone, PartialEq)]
215pub struct DeleteTopicsResponse {
216 pub throttle_time_ms: i32,
220
221 pub responses: Vec<DeletableTopicResult>,
225
226 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
228}
229
230impl DeleteTopicsResponse {
231 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
237 self.throttle_time_ms = value;
238 self
239 }
240 pub fn with_responses(mut self, value: Vec<DeletableTopicResult>) -> Self {
246 self.responses = value;
247 self
248 }
249 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
251 self.unknown_tagged_fields = value;
252 self
253 }
254 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
256 self.unknown_tagged_fields.insert(key, value);
257 self
258 }
259}
260
261#[cfg(feature = "broker")]
262impl Encodable for DeleteTopicsResponse {
263 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
264 if version >= 1 {
265 types::Int32.encode(buf, &self.throttle_time_ms)?;
266 }
267 if version >= 4 {
268 types::CompactArray(types::Struct { version }).encode(buf, &self.responses)?;
269 } else {
270 types::Array(types::Struct { version }).encode(buf, &self.responses)?;
271 }
272 if version >= 4 {
273 let num_tagged_fields = self.unknown_tagged_fields.len();
274 if num_tagged_fields > std::u32::MAX as usize {
275 bail!(
276 "Too many tagged fields to encode ({} fields)",
277 num_tagged_fields
278 );
279 }
280 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
281
282 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
283 }
284 Ok(())
285 }
286 fn compute_size(&self, version: i16) -> Result<usize> {
287 let mut total_size = 0;
288 if version >= 1 {
289 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
290 }
291 if version >= 4 {
292 total_size +=
293 types::CompactArray(types::Struct { version }).compute_size(&self.responses)?;
294 } else {
295 total_size += types::Array(types::Struct { version }).compute_size(&self.responses)?;
296 }
297 if version >= 4 {
298 let num_tagged_fields = self.unknown_tagged_fields.len();
299 if num_tagged_fields > std::u32::MAX as usize {
300 bail!(
301 "Too many tagged fields to encode ({} fields)",
302 num_tagged_fields
303 );
304 }
305 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
306
307 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
308 }
309 Ok(total_size)
310 }
311}
312
313#[cfg(feature = "client")]
314impl Decodable for DeleteTopicsResponse {
315 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
316 let throttle_time_ms = if version >= 1 {
317 types::Int32.decode(buf)?
318 } else {
319 0
320 };
321 let responses = if version >= 4 {
322 types::CompactArray(types::Struct { version }).decode(buf)?
323 } else {
324 types::Array(types::Struct { version }).decode(buf)?
325 };
326 let mut unknown_tagged_fields = BTreeMap::new();
327 if version >= 4 {
328 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
329 for _ in 0..num_tagged_fields {
330 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
331 let size: u32 = types::UnsignedVarInt.decode(buf)?;
332 let unknown_value = buf.try_get_bytes(size as usize)?;
333 unknown_tagged_fields.insert(tag as i32, unknown_value);
334 }
335 }
336 Ok(Self {
337 throttle_time_ms,
338 responses,
339 unknown_tagged_fields,
340 })
341 }
342}
343
344impl Default for DeleteTopicsResponse {
345 fn default() -> Self {
346 Self {
347 throttle_time_ms: 0,
348 responses: Default::default(),
349 unknown_tagged_fields: BTreeMap::new(),
350 }
351 }
352}
353
354impl Message for DeleteTopicsResponse {
355 const VERSIONS: VersionRange = VersionRange { min: 0, max: 6 };
356 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
357}
358
359impl HeaderVersion for DeleteTopicsResponse {
360 fn header_version(version: i16) -> i16 {
361 if version >= 4 {
362 1
363 } else {
364 0
365 }
366 }
367}