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