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 DeleteTopicState {
24 pub name: Option<super::TopicName>,
28
29 pub topic_id: Uuid,
33
34 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl DeleteTopicState {
39 pub fn with_name(mut self, value: Option<super::TopicName>) -> Self {
45 self.name = value;
46 self
47 }
48 pub fn with_topic_id(mut self, value: Uuid) -> Self {
54 self.topic_id = 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 = "client")]
70impl Encodable for DeleteTopicState {
71 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
72 if version < 0 || version > 6 {
73 bail!("specified version not supported by this message type");
74 }
75 if version >= 6 {
76 types::CompactString.encode(buf, &self.name)?;
77 } else {
78 if !self.name.is_none() {
79 bail!("A field is set that is not available on the selected protocol version");
80 }
81 }
82 if version >= 6 {
83 types::Uuid.encode(buf, &self.topic_id)?;
84 } else {
85 if &self.topic_id != &Uuid::nil() {
86 bail!("A field is set that is not available on the selected protocol version");
87 }
88 }
89 if version >= 4 {
90 let num_tagged_fields = self.unknown_tagged_fields.len();
91 if num_tagged_fields > std::u32::MAX as usize {
92 bail!(
93 "Too many tagged fields to encode ({} fields)",
94 num_tagged_fields
95 );
96 }
97 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
98
99 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
100 }
101 Ok(())
102 }
103 fn compute_size(&self, version: i16) -> Result<usize> {
104 let mut total_size = 0;
105 if version >= 6 {
106 total_size += types::CompactString.compute_size(&self.name)?;
107 } else {
108 if !self.name.is_none() {
109 bail!("A field is set that is not available on the selected protocol version");
110 }
111 }
112 if version >= 6 {
113 total_size += types::Uuid.compute_size(&self.topic_id)?;
114 } else {
115 if &self.topic_id != &Uuid::nil() {
116 bail!("A field is set that is not available on the selected protocol version");
117 }
118 }
119 if version >= 4 {
120 let num_tagged_fields = self.unknown_tagged_fields.len();
121 if num_tagged_fields > std::u32::MAX as usize {
122 bail!(
123 "Too many tagged fields to encode ({} fields)",
124 num_tagged_fields
125 );
126 }
127 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
128
129 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
130 }
131 Ok(total_size)
132 }
133}
134
135#[cfg(feature = "broker")]
136impl Decodable for DeleteTopicState {
137 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
138 if version < 0 || version > 6 {
139 bail!("specified version not supported by this message type");
140 }
141 let name = if version >= 6 {
142 types::CompactString.decode(buf)?
143 } else {
144 None
145 };
146 let topic_id = if version >= 6 {
147 types::Uuid.decode(buf)?
148 } else {
149 Uuid::nil()
150 };
151 let mut unknown_tagged_fields = BTreeMap::new();
152 if version >= 4 {
153 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
154 for _ in 0..num_tagged_fields {
155 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
156 let size: u32 = types::UnsignedVarInt.decode(buf)?;
157 let unknown_value = buf.try_get_bytes(size as usize)?;
158 unknown_tagged_fields.insert(tag as i32, unknown_value);
159 }
160 }
161 Ok(Self {
162 name,
163 topic_id,
164 unknown_tagged_fields,
165 })
166 }
167}
168
169impl Default for DeleteTopicState {
170 fn default() -> Self {
171 Self {
172 name: None,
173 topic_id: Uuid::nil(),
174 unknown_tagged_fields: BTreeMap::new(),
175 }
176 }
177}
178
179impl Message for DeleteTopicState {
180 const VERSIONS: VersionRange = VersionRange { min: 0, max: 6 };
181 const DEPRECATED_VERSIONS: Option<VersionRange> = Some(VersionRange { min: 0, max: 0 });
182}
183
184#[non_exhaustive]
186#[derive(Debug, Clone, PartialEq)]
187pub struct DeleteTopicsRequest {
188 pub topics: Vec<DeleteTopicState>,
192
193 pub topic_names: Vec<super::TopicName>,
197
198 pub timeout_ms: i32,
202
203 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
205}
206
207impl DeleteTopicsRequest {
208 pub fn with_topics(mut self, value: Vec<DeleteTopicState>) -> Self {
214 self.topics = value;
215 self
216 }
217 pub fn with_topic_names(mut self, value: Vec<super::TopicName>) -> Self {
223 self.topic_names = value;
224 self
225 }
226 pub fn with_timeout_ms(mut self, value: i32) -> Self {
232 self.timeout_ms = value;
233 self
234 }
235 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
237 self.unknown_tagged_fields = value;
238 self
239 }
240 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
242 self.unknown_tagged_fields.insert(key, value);
243 self
244 }
245}
246
247#[cfg(feature = "client")]
248impl Encodable for DeleteTopicsRequest {
249 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
250 if version < 0 || version > 6 {
251 bail!("specified version not supported by this message type");
252 }
253 if version >= 6 {
254 types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
255 } else {
256 if !self.topics.is_empty() {
257 bail!("A field is set that is not available on the selected protocol version");
258 }
259 }
260 if version <= 5 {
261 if version >= 4 {
262 types::CompactArray(types::CompactString).encode(buf, &self.topic_names)?;
263 } else {
264 types::Array(types::String).encode(buf, &self.topic_names)?;
265 }
266 }
267 types::Int32.encode(buf, &self.timeout_ms)?;
268 if version >= 4 {
269 let num_tagged_fields = self.unknown_tagged_fields.len();
270 if num_tagged_fields > std::u32::MAX as usize {
271 bail!(
272 "Too many tagged fields to encode ({} fields)",
273 num_tagged_fields
274 );
275 }
276 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
277
278 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
279 }
280 Ok(())
281 }
282 fn compute_size(&self, version: i16) -> Result<usize> {
283 let mut total_size = 0;
284 if version >= 6 {
285 total_size +=
286 types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
287 } else {
288 if !self.topics.is_empty() {
289 bail!("A field is set that is not available on the selected protocol version");
290 }
291 }
292 if version <= 5 {
293 if version >= 4 {
294 total_size +=
295 types::CompactArray(types::CompactString).compute_size(&self.topic_names)?;
296 } else {
297 total_size += types::Array(types::String).compute_size(&self.topic_names)?;
298 }
299 }
300 total_size += types::Int32.compute_size(&self.timeout_ms)?;
301 if version >= 4 {
302 let num_tagged_fields = self.unknown_tagged_fields.len();
303 if num_tagged_fields > std::u32::MAX as usize {
304 bail!(
305 "Too many tagged fields to encode ({} fields)",
306 num_tagged_fields
307 );
308 }
309 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
310
311 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
312 }
313 Ok(total_size)
314 }
315}
316
317#[cfg(feature = "broker")]
318impl Decodable for DeleteTopicsRequest {
319 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
320 if version < 0 || version > 6 {
321 bail!("specified version not supported by this message type");
322 }
323 let topics = if version >= 6 {
324 types::CompactArray(types::Struct { version }).decode(buf)?
325 } else {
326 Default::default()
327 };
328 let topic_names = if version <= 5 {
329 if version >= 4 {
330 types::CompactArray(types::CompactString).decode(buf)?
331 } else {
332 types::Array(types::String).decode(buf)?
333 }
334 } else {
335 Default::default()
336 };
337 let timeout_ms = types::Int32.decode(buf)?;
338 let mut unknown_tagged_fields = BTreeMap::new();
339 if version >= 4 {
340 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
341 for _ in 0..num_tagged_fields {
342 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
343 let size: u32 = types::UnsignedVarInt.decode(buf)?;
344 let unknown_value = buf.try_get_bytes(size as usize)?;
345 unknown_tagged_fields.insert(tag as i32, unknown_value);
346 }
347 }
348 Ok(Self {
349 topics,
350 topic_names,
351 timeout_ms,
352 unknown_tagged_fields,
353 })
354 }
355}
356
357impl Default for DeleteTopicsRequest {
358 fn default() -> Self {
359 Self {
360 topics: Default::default(),
361 topic_names: Default::default(),
362 timeout_ms: 0,
363 unknown_tagged_fields: BTreeMap::new(),
364 }
365 }
366}
367
368impl Message for DeleteTopicsRequest {
369 const VERSIONS: VersionRange = VersionRange { min: 0, max: 6 };
370 const DEPRECATED_VERSIONS: Option<VersionRange> = Some(VersionRange { min: 0, max: 0 });
371}
372
373impl HeaderVersion for DeleteTopicsRequest {
374 fn header_version(version: i16) -> i16 {
375 if version >= 4 {
376 2
377 } else {
378 1
379 }
380 }
381}