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