Skip to main content

odradek_protocol/messages/
produce_request.rs

1//! `ProduceRequest` — generated from `schemas/ProduceRequest.json`.
2//!
3//! @generated by `cargo xtask codegen`. Do not edit by hand.
4
5#![allow(
6    clippy::field_reassign_with_default,
7    clippy::nonminimal_bool,
8    clippy::derivable_impls,
9    clippy::borrow_deref_ref,
10    clippy::double_parens,
11    unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21/// Whether `version` of this message uses the flexible (compact) encoding.
22pub fn is_flexible(version: i16) -> bool {
23    let _ = version;
24    version >= 9
25}
26
27/// `ProduceRequest`, versions 3-13.
28#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct ProduceRequest {
31    /// The transactional ID, or null if the producer is not transactional.
32    pub transactional_id: Option<String>,
33    /// The number of acknowledgments the producer requires the leader to have received before considering a request complete. Allowed values: 0 for no acknowledgments, 1 for only the leader and -1 for the full ISR.
34    pub acks: i16,
35    /// The timeout to await a response in milliseconds.
36    pub timeout_ms: i32,
37    /// Each topic to produce to.
38    pub topic_data: Vec<TopicProduceData>,
39    /// Tagged fields not (yet) known to this crate, kept raw.
40    pub unknown_tagged_fields: Vec<RawTaggedField>,
41}
42
43impl Default for ProduceRequest {
44    fn default() -> Self {
45        Self {
46            transactional_id: None,
47            acks: 0,
48            timeout_ms: 0,
49            topic_data: Vec::new(),
50            unknown_tagged_fields: Vec::new(),
51        }
52    }
53}
54
55impl ProduceRequest {
56    /// This message's api key in Kafka's registry.
57    pub const API_KEY: i16 = 0;
58    /// The lowest schema version this snapshot can speak.
59    pub const MIN_VERSION: i16 = 3;
60    /// The highest schema version this snapshot can speak.
61    pub const MAX_VERSION: i16 = 13;
62
63    /// Encode this `ProduceRequest` at `version`.
64    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
65        if is_flexible(version) {
66            wire::put_compact_nullable_string(buf, self.transactional_id.as_deref());
67        } else {
68            wire::put_nullable_string(buf, self.transactional_id.as_deref())?;
69        }
70        buf.put_i16(self.acks);
71        buf.put_i32(self.timeout_ms);
72        if is_flexible(version) {
73            wire::put_compact_array_len(buf, Some(self.topic_data.len()));
74        } else {
75            wire::put_array_len(buf, Some(self.topic_data.len()))?;
76        }
77        for item in &self.topic_data {
78            item.encode(buf, version)?;
79        }
80        if is_flexible(version) {
81            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
82        }
83        Ok(())
84    }
85
86    /// Decode one `ProduceRequest`, bounding allocation by the default
87    /// [`Limits`] derived from `buf`'s remaining length.
88    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
89        Self::decode_with_limits(buf, version, Limits::default())
90    }
91
92    /// Decode one `ProduceRequest` under `limits`.
93    pub fn decode_with_limits(
94        buf: &mut impl Buf,
95        version: i16,
96        limits: Limits,
97    ) -> Result<Self, DecodeError> {
98        let mut budget = limits.budget(buf.remaining());
99        Self::decode_with_budget(buf, version, &mut budget)
100    }
101
102    /// Decode one `ProduceRequest` against an existing `budget`.
103    pub fn decode_with_budget(
104        buf: &mut impl Buf,
105        version: i16,
106        budget: &mut Budget,
107    ) -> Result<Self, DecodeError> {
108        let mut this = Self::default();
109        this.transactional_id = if is_flexible(version) {
110            wire::get_compact_nullable_string(buf)?
111        } else {
112            wire::get_nullable_string(buf)?
113        };
114        this.acks = wire::get_i16(buf)?;
115        this.timeout_ms = wire::get_i32(buf)?;
116        this.topic_data = {
117            let len = if is_flexible(version) {
118                wire::get_compact_array_len(buf)?
119            } else {
120                wire::get_array_len(buf)?
121            };
122            match len {
123                None => return Err(DecodeError::InvalidLength(-1)),
124                Some(n) => {
125                    let mut items = Vec::new();
126                    for _ in 0..n {
127                        let mark = buf.remaining();
128                        let item = TopicProduceData::decode_with_budget(buf, version, budget)?;
129                        budget.progress(mark, buf.remaining())?;
130                        budget.push(&mut items, item)?;
131                    }
132                    items
133                }
134            }
135        };
136        if is_flexible(version) {
137            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
138        }
139        Ok(this)
140    }
141}
142
143impl crate::Message for ProduceRequest {
144    const API_KEY: i16 = 0;
145    const MIN_VERSION: i16 = 3;
146    const MAX_VERSION: i16 = 13;
147
148    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
149        ProduceRequest::encode(self, buf, version)
150    }
151    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
152        ProduceRequest::decode(buf, version)
153    }
154    fn decode_with_limits(
155        buf: &mut impl Buf,
156        version: i16,
157        limits: Limits,
158    ) -> Result<Self, DecodeError> {
159        ProduceRequest::decode_with_limits(buf, version, limits)
160    }
161}
162
163/// `TopicProduceData` (nested in [`ProduceRequest`]).
164#[derive(Debug, Clone, PartialEq)]
165#[non_exhaustive]
166pub struct TopicProduceData {
167    /// The topic name.
168    pub name: String,
169    /// The unique topic ID
170    pub topic_id: [u8; 16],
171    /// Each partition to produce to.
172    pub partition_data: Vec<PartitionProduceData>,
173    /// Tagged fields not (yet) known to this crate, kept raw.
174    pub unknown_tagged_fields: Vec<RawTaggedField>,
175}
176
177impl Default for TopicProduceData {
178    fn default() -> Self {
179        Self {
180            name: String::new(),
181            topic_id: [0u8; 16],
182            partition_data: Vec::new(),
183            unknown_tagged_fields: Vec::new(),
184        }
185    }
186}
187
188impl TopicProduceData {
189    /// Encode this `TopicProduceData` at `version`.
190    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
191        if version <= 12 {
192            if is_flexible(version) {
193                wire::put_compact_string(buf, &self.name);
194            } else {
195                wire::put_string(buf, &self.name)?;
196            }
197        }
198        if version >= 13 {
199            wire::put_uuid(buf, self.topic_id);
200        }
201        if is_flexible(version) {
202            wire::put_compact_array_len(buf, Some(self.partition_data.len()));
203        } else {
204            wire::put_array_len(buf, Some(self.partition_data.len()))?;
205        }
206        for item in &self.partition_data {
207            item.encode(buf, version)?;
208        }
209        if is_flexible(version) {
210            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
211        }
212        Ok(())
213    }
214
215    /// Decode one `TopicProduceData`, bounding allocation by the default
216    /// [`Limits`] derived from `buf`'s remaining length.
217    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
218        Self::decode_with_limits(buf, version, Limits::default())
219    }
220
221    /// Decode one `TopicProduceData` under `limits`.
222    pub fn decode_with_limits(
223        buf: &mut impl Buf,
224        version: i16,
225        limits: Limits,
226    ) -> Result<Self, DecodeError> {
227        let mut budget = limits.budget(buf.remaining());
228        Self::decode_with_budget(buf, version, &mut budget)
229    }
230
231    /// Decode one `TopicProduceData` against an existing `budget`.
232    pub fn decode_with_budget(
233        buf: &mut impl Buf,
234        version: i16,
235        budget: &mut Budget,
236    ) -> Result<Self, DecodeError> {
237        let mut this = Self::default();
238        if version <= 12 {
239            this.name = if is_flexible(version) {
240                wire::get_compact_string(buf)?
241            } else {
242                wire::get_string(buf)?
243            };
244        }
245        if version >= 13 {
246            this.topic_id = wire::get_uuid(buf)?;
247        }
248        this.partition_data = {
249            let len = if is_flexible(version) {
250                wire::get_compact_array_len(buf)?
251            } else {
252                wire::get_array_len(buf)?
253            };
254            match len {
255                None => return Err(DecodeError::InvalidLength(-1)),
256                Some(n) => {
257                    let mut items = Vec::new();
258                    for _ in 0..n {
259                        let mark = buf.remaining();
260                        let item = PartitionProduceData::decode_with_budget(buf, version, budget)?;
261                        budget.progress(mark, buf.remaining())?;
262                        budget.push(&mut items, item)?;
263                    }
264                    items
265                }
266            }
267        };
268        if is_flexible(version) {
269            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
270        }
271        Ok(this)
272    }
273}
274
275/// `PartitionProduceData` (nested in [`ProduceRequest`]).
276#[derive(Debug, Clone, PartialEq)]
277#[non_exhaustive]
278pub struct PartitionProduceData {
279    /// The partition index.
280    pub index: i32,
281    /// The record data to be produced.
282    pub records: Option<bytes::Bytes>,
283    /// Tagged fields not (yet) known to this crate, kept raw.
284    pub unknown_tagged_fields: Vec<RawTaggedField>,
285}
286
287impl Default for PartitionProduceData {
288    fn default() -> Self {
289        Self {
290            index: 0,
291            records: Some(bytes::Bytes::new()),
292            unknown_tagged_fields: Vec::new(),
293        }
294    }
295}
296
297impl PartitionProduceData {
298    /// Encode this `PartitionProduceData` at `version`.
299    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
300        buf.put_i32(self.index);
301        if is_flexible(version) {
302            wire::put_compact_nullable_bytes(buf, self.records.as_deref());
303        } else {
304            wire::put_nullable_bytes(buf, self.records.as_deref())?;
305        }
306        if is_flexible(version) {
307            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
308        }
309        Ok(())
310    }
311
312    /// Decode one `PartitionProduceData`, bounding allocation by the default
313    /// [`Limits`] derived from `buf`'s remaining length.
314    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
315        Self::decode_with_limits(buf, version, Limits::default())
316    }
317
318    /// Decode one `PartitionProduceData` under `limits`.
319    pub fn decode_with_limits(
320        buf: &mut impl Buf,
321        version: i16,
322        limits: Limits,
323    ) -> Result<Self, DecodeError> {
324        let mut budget = limits.budget(buf.remaining());
325        Self::decode_with_budget(buf, version, &mut budget)
326    }
327
328    /// Decode one `PartitionProduceData` against an existing `budget`.
329    pub fn decode_with_budget(
330        buf: &mut impl Buf,
331        version: i16,
332        budget: &mut Budget,
333    ) -> Result<Self, DecodeError> {
334        let mut this = Self::default();
335        this.index = wire::get_i32(buf)?;
336        this.records = if is_flexible(version) {
337            wire::get_compact_nullable_bytes(buf)?
338        } else {
339            wire::get_nullable_bytes(buf)?
340        };
341        if is_flexible(version) {
342            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
343        }
344        Ok(this)
345    }
346}