kafka_protocol/messages/
delete_records_request.rs

1//! DeleteRecordsRequest
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/DeleteRecordsRequest.json).
4// WARNING: the items of this module are generated and should not be edited directly
5#![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/// Valid versions: 0-2
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct DeleteRecordsPartition {
24    /// The partition index.
25    ///
26    /// Supported API versions: 0-2
27    pub partition_index: i32,
28
29    /// The deletion offset.
30    ///
31    /// Supported API versions: 0-2
32    pub offset: i64,
33
34    /// Other tagged fields
35    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl DeleteRecordsPartition {
39    /// Sets `partition_index` to the passed value.
40    ///
41    /// The partition index.
42    ///
43    /// Supported API versions: 0-2
44    pub fn with_partition_index(mut self, value: i32) -> Self {
45        self.partition_index = value;
46        self
47    }
48    /// Sets `offset` to the passed value.
49    ///
50    /// The deletion offset.
51    ///
52    /// Supported API versions: 0-2
53    pub fn with_offset(mut self, value: i64) -> Self {
54        self.offset = value;
55        self
56    }
57    /// Sets unknown_tagged_fields to the passed value.
58    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
59        self.unknown_tagged_fields = value;
60        self
61    }
62    /// Inserts an entry into unknown_tagged_fields.
63    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 DeleteRecordsPartition {
71    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
72        if version < 0 || version > 2 {
73            bail!("specified version not supported by this message type");
74        }
75        types::Int32.encode(buf, &self.partition_index)?;
76        types::Int64.encode(buf, &self.offset)?;
77        if version >= 2 {
78            let num_tagged_fields = self.unknown_tagged_fields.len();
79            if num_tagged_fields > std::u32::MAX as usize {
80                bail!(
81                    "Too many tagged fields to encode ({} fields)",
82                    num_tagged_fields
83                );
84            }
85            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
86
87            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
88        }
89        Ok(())
90    }
91    fn compute_size(&self, version: i16) -> Result<usize> {
92        let mut total_size = 0;
93        total_size += types::Int32.compute_size(&self.partition_index)?;
94        total_size += types::Int64.compute_size(&self.offset)?;
95        if version >= 2 {
96            let num_tagged_fields = self.unknown_tagged_fields.len();
97            if num_tagged_fields > std::u32::MAX as usize {
98                bail!(
99                    "Too many tagged fields to encode ({} fields)",
100                    num_tagged_fields
101                );
102            }
103            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
104
105            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
106        }
107        Ok(total_size)
108    }
109}
110
111#[cfg(feature = "broker")]
112impl Decodable for DeleteRecordsPartition {
113    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
114        if version < 0 || version > 2 {
115            bail!("specified version not supported by this message type");
116        }
117        let partition_index = types::Int32.decode(buf)?;
118        let offset = types::Int64.decode(buf)?;
119        let mut unknown_tagged_fields = BTreeMap::new();
120        if version >= 2 {
121            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
122            for _ in 0..num_tagged_fields {
123                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
124                let size: u32 = types::UnsignedVarInt.decode(buf)?;
125                let unknown_value = buf.try_get_bytes(size as usize)?;
126                unknown_tagged_fields.insert(tag as i32, unknown_value);
127            }
128        }
129        Ok(Self {
130            partition_index,
131            offset,
132            unknown_tagged_fields,
133        })
134    }
135}
136
137impl Default for DeleteRecordsPartition {
138    fn default() -> Self {
139        Self {
140            partition_index: 0,
141            offset: 0,
142            unknown_tagged_fields: BTreeMap::new(),
143        }
144    }
145}
146
147impl Message for DeleteRecordsPartition {
148    const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
149    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
150}
151
152/// Valid versions: 0-2
153#[non_exhaustive]
154#[derive(Debug, Clone, PartialEq)]
155pub struct DeleteRecordsRequest {
156    /// Each topic that we want to delete records from.
157    ///
158    /// Supported API versions: 0-2
159    pub topics: Vec<DeleteRecordsTopic>,
160
161    /// How long to wait for the deletion to complete, in milliseconds.
162    ///
163    /// Supported API versions: 0-2
164    pub timeout_ms: i32,
165
166    /// Other tagged fields
167    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
168}
169
170impl DeleteRecordsRequest {
171    /// Sets `topics` to the passed value.
172    ///
173    /// Each topic that we want to delete records from.
174    ///
175    /// Supported API versions: 0-2
176    pub fn with_topics(mut self, value: Vec<DeleteRecordsTopic>) -> Self {
177        self.topics = value;
178        self
179    }
180    /// Sets `timeout_ms` to the passed value.
181    ///
182    /// How long to wait for the deletion to complete, in milliseconds.
183    ///
184    /// Supported API versions: 0-2
185    pub fn with_timeout_ms(mut self, value: i32) -> Self {
186        self.timeout_ms = value;
187        self
188    }
189    /// Sets unknown_tagged_fields to the passed value.
190    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
191        self.unknown_tagged_fields = value;
192        self
193    }
194    /// Inserts an entry into unknown_tagged_fields.
195    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
196        self.unknown_tagged_fields.insert(key, value);
197        self
198    }
199}
200
201#[cfg(feature = "client")]
202impl Encodable for DeleteRecordsRequest {
203    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
204        if version < 0 || version > 2 {
205            bail!("specified version not supported by this message type");
206        }
207        if version >= 2 {
208            types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
209        } else {
210            types::Array(types::Struct { version }).encode(buf, &self.topics)?;
211        }
212        types::Int32.encode(buf, &self.timeout_ms)?;
213        if version >= 2 {
214            let num_tagged_fields = self.unknown_tagged_fields.len();
215            if num_tagged_fields > std::u32::MAX as usize {
216                bail!(
217                    "Too many tagged fields to encode ({} fields)",
218                    num_tagged_fields
219                );
220            }
221            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
222
223            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
224        }
225        Ok(())
226    }
227    fn compute_size(&self, version: i16) -> Result<usize> {
228        let mut total_size = 0;
229        if version >= 2 {
230            total_size +=
231                types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
232        } else {
233            total_size += types::Array(types::Struct { version }).compute_size(&self.topics)?;
234        }
235        total_size += types::Int32.compute_size(&self.timeout_ms)?;
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            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
245
246            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
247        }
248        Ok(total_size)
249    }
250}
251
252#[cfg(feature = "broker")]
253impl Decodable for DeleteRecordsRequest {
254    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
255        if version < 0 || version > 2 {
256            bail!("specified version not supported by this message type");
257        }
258        let topics = if version >= 2 {
259            types::CompactArray(types::Struct { version }).decode(buf)?
260        } else {
261            types::Array(types::Struct { version }).decode(buf)?
262        };
263        let timeout_ms = types::Int32.decode(buf)?;
264        let mut unknown_tagged_fields = BTreeMap::new();
265        if version >= 2 {
266            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
267            for _ in 0..num_tagged_fields {
268                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
269                let size: u32 = types::UnsignedVarInt.decode(buf)?;
270                let unknown_value = buf.try_get_bytes(size as usize)?;
271                unknown_tagged_fields.insert(tag as i32, unknown_value);
272            }
273        }
274        Ok(Self {
275            topics,
276            timeout_ms,
277            unknown_tagged_fields,
278        })
279    }
280}
281
282impl Default for DeleteRecordsRequest {
283    fn default() -> Self {
284        Self {
285            topics: Default::default(),
286            timeout_ms: 0,
287            unknown_tagged_fields: BTreeMap::new(),
288        }
289    }
290}
291
292impl Message for DeleteRecordsRequest {
293    const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
294    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
295}
296
297/// Valid versions: 0-2
298#[non_exhaustive]
299#[derive(Debug, Clone, PartialEq)]
300pub struct DeleteRecordsTopic {
301    /// The topic name.
302    ///
303    /// Supported API versions: 0-2
304    pub name: super::TopicName,
305
306    /// Each partition that we want to delete records from.
307    ///
308    /// Supported API versions: 0-2
309    pub partitions: Vec<DeleteRecordsPartition>,
310
311    /// Other tagged fields
312    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
313}
314
315impl DeleteRecordsTopic {
316    /// Sets `name` to the passed value.
317    ///
318    /// The topic name.
319    ///
320    /// Supported API versions: 0-2
321    pub fn with_name(mut self, value: super::TopicName) -> Self {
322        self.name = value;
323        self
324    }
325    /// Sets `partitions` to the passed value.
326    ///
327    /// Each partition that we want to delete records from.
328    ///
329    /// Supported API versions: 0-2
330    pub fn with_partitions(mut self, value: Vec<DeleteRecordsPartition>) -> Self {
331        self.partitions = value;
332        self
333    }
334    /// Sets unknown_tagged_fields to the passed value.
335    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
336        self.unknown_tagged_fields = value;
337        self
338    }
339    /// Inserts an entry into unknown_tagged_fields.
340    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
341        self.unknown_tagged_fields.insert(key, value);
342        self
343    }
344}
345
346#[cfg(feature = "client")]
347impl Encodable for DeleteRecordsTopic {
348    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
349        if version < 0 || version > 2 {
350            bail!("specified version not supported by this message type");
351        }
352        if version >= 2 {
353            types::CompactString.encode(buf, &self.name)?;
354        } else {
355            types::String.encode(buf, &self.name)?;
356        }
357        if version >= 2 {
358            types::CompactArray(types::Struct { version }).encode(buf, &self.partitions)?;
359        } else {
360            types::Array(types::Struct { version }).encode(buf, &self.partitions)?;
361        }
362        if version >= 2 {
363            let num_tagged_fields = self.unknown_tagged_fields.len();
364            if num_tagged_fields > std::u32::MAX as usize {
365                bail!(
366                    "Too many tagged fields to encode ({} fields)",
367                    num_tagged_fields
368                );
369            }
370            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
371
372            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
373        }
374        Ok(())
375    }
376    fn compute_size(&self, version: i16) -> Result<usize> {
377        let mut total_size = 0;
378        if version >= 2 {
379            total_size += types::CompactString.compute_size(&self.name)?;
380        } else {
381            total_size += types::String.compute_size(&self.name)?;
382        }
383        if version >= 2 {
384            total_size +=
385                types::CompactArray(types::Struct { version }).compute_size(&self.partitions)?;
386        } else {
387            total_size += types::Array(types::Struct { version }).compute_size(&self.partitions)?;
388        }
389        if version >= 2 {
390            let num_tagged_fields = self.unknown_tagged_fields.len();
391            if num_tagged_fields > std::u32::MAX as usize {
392                bail!(
393                    "Too many tagged fields to encode ({} fields)",
394                    num_tagged_fields
395                );
396            }
397            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
398
399            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
400        }
401        Ok(total_size)
402    }
403}
404
405#[cfg(feature = "broker")]
406impl Decodable for DeleteRecordsTopic {
407    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
408        if version < 0 || version > 2 {
409            bail!("specified version not supported by this message type");
410        }
411        let name = if version >= 2 {
412            types::CompactString.decode(buf)?
413        } else {
414            types::String.decode(buf)?
415        };
416        let partitions = if version >= 2 {
417            types::CompactArray(types::Struct { version }).decode(buf)?
418        } else {
419            types::Array(types::Struct { version }).decode(buf)?
420        };
421        let mut unknown_tagged_fields = BTreeMap::new();
422        if version >= 2 {
423            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
424            for _ in 0..num_tagged_fields {
425                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
426                let size: u32 = types::UnsignedVarInt.decode(buf)?;
427                let unknown_value = buf.try_get_bytes(size as usize)?;
428                unknown_tagged_fields.insert(tag as i32, unknown_value);
429            }
430        }
431        Ok(Self {
432            name,
433            partitions,
434            unknown_tagged_fields,
435        })
436    }
437}
438
439impl Default for DeleteRecordsTopic {
440    fn default() -> Self {
441        Self {
442            name: Default::default(),
443            partitions: Default::default(),
444            unknown_tagged_fields: BTreeMap::new(),
445        }
446    }
447}
448
449impl Message for DeleteRecordsTopic {
450    const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
451    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
452}
453
454impl HeaderVersion for DeleteRecordsRequest {
455    fn header_version(version: i16) -> i16 {
456        if version >= 2 {
457            2
458        } else {
459            1
460        }
461    }
462}