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