Skip to main content

odradek_protocol/messages/
add_partitions_to_txn_request.rs

1//! `AddPartitionsToTxnRequest` — generated from `schemas/AddPartitionsToTxnRequest.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 >= 3
25}
26
27/// `AddPartitionsToTxnRequest`, versions 0-5.
28#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct AddPartitionsToTxnRequest {
31    /// List of transactions to add partitions to.
32    pub transactions: Vec<AddPartitionsToTxnTransaction>,
33    /// The transactional id corresponding to the transaction.
34    pub v3_and_below_transactional_id: String,
35    /// Current producer id in use by the transactional id.
36    pub v3_and_below_producer_id: i64,
37    /// Current epoch associated with the producer id.
38    pub v3_and_below_producer_epoch: i16,
39    /// The partitions to add to the transaction.
40    pub v3_and_below_topics: Vec<AddPartitionsToTxnTopic>,
41    /// Tagged fields not (yet) known to this crate, kept raw.
42    pub unknown_tagged_fields: Vec<RawTaggedField>,
43}
44
45impl Default for AddPartitionsToTxnRequest {
46    fn default() -> Self {
47        Self {
48            transactions: Vec::new(),
49            v3_and_below_transactional_id: String::new(),
50            v3_and_below_producer_id: 0,
51            v3_and_below_producer_epoch: 0,
52            v3_and_below_topics: Vec::new(),
53            unknown_tagged_fields: Vec::new(),
54        }
55    }
56}
57
58impl AddPartitionsToTxnRequest {
59    /// This message's api key in Kafka's registry.
60    pub const API_KEY: i16 = 24;
61    /// The lowest schema version this snapshot can speak.
62    pub const MIN_VERSION: i16 = 0;
63    /// The highest schema version this snapshot can speak.
64    pub const MAX_VERSION: i16 = 5;
65
66    /// Encode this `AddPartitionsToTxnRequest` at `version`.
67    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
68        if version >= 4 {
69            if is_flexible(version) {
70                wire::put_compact_array_len(buf, Some(self.transactions.len()));
71            } else {
72                wire::put_array_len(buf, Some(self.transactions.len()))?;
73            }
74            for item in &self.transactions {
75                item.encode(buf, version)?;
76            }
77        }
78        if version <= 3 {
79            if is_flexible(version) {
80                wire::put_compact_string(buf, &self.v3_and_below_transactional_id);
81            } else {
82                wire::put_string(buf, &self.v3_and_below_transactional_id)?;
83            }
84        }
85        if version <= 3 {
86            buf.put_i64(self.v3_and_below_producer_id);
87        }
88        if version <= 3 {
89            buf.put_i16(self.v3_and_below_producer_epoch);
90        }
91        if version <= 3 {
92            if is_flexible(version) {
93                wire::put_compact_array_len(buf, Some(self.v3_and_below_topics.len()));
94            } else {
95                wire::put_array_len(buf, Some(self.v3_and_below_topics.len()))?;
96            }
97            for item in &self.v3_and_below_topics {
98                item.encode(buf, version)?;
99            }
100        }
101        if is_flexible(version) {
102            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
103        }
104        Ok(())
105    }
106
107    /// Decode one `AddPartitionsToTxnRequest`, bounding allocation by the default
108    /// [`Limits`] derived from `buf`'s remaining length.
109    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
110        Self::decode_with_limits(buf, version, Limits::default())
111    }
112
113    /// Decode one `AddPartitionsToTxnRequest` under `limits`.
114    pub fn decode_with_limits(
115        buf: &mut impl Buf,
116        version: i16,
117        limits: Limits,
118    ) -> Result<Self, DecodeError> {
119        let mut budget = limits.budget(buf.remaining());
120        Self::decode_with_budget(buf, version, &mut budget)
121    }
122
123    /// Decode one `AddPartitionsToTxnRequest` against an existing `budget`.
124    pub fn decode_with_budget(
125        buf: &mut impl Buf,
126        version: i16,
127        budget: &mut Budget,
128    ) -> Result<Self, DecodeError> {
129        let mut this = Self::default();
130        if version >= 4 {
131            this.transactions = {
132                let len = if is_flexible(version) {
133                    wire::get_compact_array_len(buf)?
134                } else {
135                    wire::get_array_len(buf)?
136                };
137                match len {
138                    None => return Err(DecodeError::InvalidLength(-1)),
139                    Some(n) => {
140                        let mut items = Vec::new();
141                        for _ in 0..n {
142                            let mark = buf.remaining();
143                            let item = AddPartitionsToTxnTransaction::decode_with_budget(
144                                buf, version, budget,
145                            )?;
146                            budget.progress(mark, buf.remaining())?;
147                            budget.push(&mut items, item)?;
148                        }
149                        items
150                    }
151                }
152            };
153        }
154        if version <= 3 {
155            this.v3_and_below_transactional_id = if is_flexible(version) {
156                wire::get_compact_string(buf)?
157            } else {
158                wire::get_string(buf)?
159            };
160        }
161        if version <= 3 {
162            this.v3_and_below_producer_id = wire::get_i64(buf)?;
163        }
164        if version <= 3 {
165            this.v3_and_below_producer_epoch = wire::get_i16(buf)?;
166        }
167        if version <= 3 {
168            this.v3_and_below_topics = {
169                let len = if is_flexible(version) {
170                    wire::get_compact_array_len(buf)?
171                } else {
172                    wire::get_array_len(buf)?
173                };
174                match len {
175                    None => return Err(DecodeError::InvalidLength(-1)),
176                    Some(n) => {
177                        let mut items = Vec::new();
178                        for _ in 0..n {
179                            let mark = buf.remaining();
180                            let item =
181                                AddPartitionsToTxnTopic::decode_with_budget(buf, version, budget)?;
182                            budget.progress(mark, buf.remaining())?;
183                            budget.push(&mut items, item)?;
184                        }
185                        items
186                    }
187                }
188            };
189        }
190        if is_flexible(version) {
191            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
192        }
193        Ok(this)
194    }
195}
196
197impl crate::Message for AddPartitionsToTxnRequest {
198    const API_KEY: i16 = 24;
199    const MIN_VERSION: i16 = 0;
200    const MAX_VERSION: i16 = 5;
201
202    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
203        AddPartitionsToTxnRequest::encode(self, buf, version)
204    }
205    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
206        AddPartitionsToTxnRequest::decode(buf, version)
207    }
208    fn decode_with_limits(
209        buf: &mut impl Buf,
210        version: i16,
211        limits: Limits,
212    ) -> Result<Self, DecodeError> {
213        AddPartitionsToTxnRequest::decode_with_limits(buf, version, limits)
214    }
215}
216
217/// `AddPartitionsToTxnTransaction` (nested in [`AddPartitionsToTxnRequest`]).
218#[derive(Debug, Clone, PartialEq)]
219#[non_exhaustive]
220pub struct AddPartitionsToTxnTransaction {
221    /// The transactional id corresponding to the transaction.
222    pub transactional_id: String,
223    /// Current producer id in use by the transactional id.
224    pub producer_id: i64,
225    /// Current epoch associated with the producer id.
226    pub producer_epoch: i16,
227    /// Boolean to signify if we want to check if the partition is in the transaction rather than add it.
228    pub verify_only: bool,
229    /// The partitions to add to the transaction.
230    pub topics: Vec<AddPartitionsToTxnTopic>,
231    /// Tagged fields not (yet) known to this crate, kept raw.
232    pub unknown_tagged_fields: Vec<RawTaggedField>,
233}
234
235impl Default for AddPartitionsToTxnTransaction {
236    fn default() -> Self {
237        Self {
238            transactional_id: String::new(),
239            producer_id: 0,
240            producer_epoch: 0,
241            verify_only: false,
242            topics: Vec::new(),
243            unknown_tagged_fields: Vec::new(),
244        }
245    }
246}
247
248impl AddPartitionsToTxnTransaction {
249    /// Encode this `AddPartitionsToTxnTransaction` at `version`.
250    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
251        if version >= 4 {
252            if is_flexible(version) {
253                wire::put_compact_string(buf, &self.transactional_id);
254            } else {
255                wire::put_string(buf, &self.transactional_id)?;
256            }
257        }
258        if version >= 4 {
259            buf.put_i64(self.producer_id);
260        }
261        if version >= 4 {
262            buf.put_i16(self.producer_epoch);
263        }
264        if version >= 4 {
265            buf.put_u8(u8::from(self.verify_only));
266        }
267        if version >= 4 {
268            if is_flexible(version) {
269                wire::put_compact_array_len(buf, Some(self.topics.len()));
270            } else {
271                wire::put_array_len(buf, Some(self.topics.len()))?;
272            }
273            for item in &self.topics {
274                item.encode(buf, version)?;
275            }
276        }
277        if is_flexible(version) {
278            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
279        }
280        Ok(())
281    }
282
283    /// Decode one `AddPartitionsToTxnTransaction`, bounding allocation by the default
284    /// [`Limits`] derived from `buf`'s remaining length.
285    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
286        Self::decode_with_limits(buf, version, Limits::default())
287    }
288
289    /// Decode one `AddPartitionsToTxnTransaction` under `limits`.
290    pub fn decode_with_limits(
291        buf: &mut impl Buf,
292        version: i16,
293        limits: Limits,
294    ) -> Result<Self, DecodeError> {
295        let mut budget = limits.budget(buf.remaining());
296        Self::decode_with_budget(buf, version, &mut budget)
297    }
298
299    /// Decode one `AddPartitionsToTxnTransaction` against an existing `budget`.
300    pub fn decode_with_budget(
301        buf: &mut impl Buf,
302        version: i16,
303        budget: &mut Budget,
304    ) -> Result<Self, DecodeError> {
305        let mut this = Self::default();
306        if version >= 4 {
307            this.transactional_id = if is_flexible(version) {
308                wire::get_compact_string(buf)?
309            } else {
310                wire::get_string(buf)?
311            };
312        }
313        if version >= 4 {
314            this.producer_id = wire::get_i64(buf)?;
315        }
316        if version >= 4 {
317            this.producer_epoch = wire::get_i16(buf)?;
318        }
319        if version >= 4 {
320            this.verify_only = wire::get_bool(buf)?;
321        }
322        if version >= 4 {
323            this.topics = {
324                let len = if is_flexible(version) {
325                    wire::get_compact_array_len(buf)?
326                } else {
327                    wire::get_array_len(buf)?
328                };
329                match len {
330                    None => return Err(DecodeError::InvalidLength(-1)),
331                    Some(n) => {
332                        let mut items = Vec::new();
333                        for _ in 0..n {
334                            let mark = buf.remaining();
335                            let item =
336                                AddPartitionsToTxnTopic::decode_with_budget(buf, version, budget)?;
337                            budget.progress(mark, buf.remaining())?;
338                            budget.push(&mut items, item)?;
339                        }
340                        items
341                    }
342                }
343            };
344        }
345        if is_flexible(version) {
346            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
347        }
348        Ok(this)
349    }
350}
351
352/// `AddPartitionsToTxnTopic` (nested in [`AddPartitionsToTxnRequest`]).
353#[derive(Debug, Clone, PartialEq)]
354#[non_exhaustive]
355pub struct AddPartitionsToTxnTopic {
356    /// The name of the topic.
357    pub name: String,
358    /// The partition indexes to add to the transaction.
359    pub partitions: Vec<i32>,
360    /// Tagged fields not (yet) known to this crate, kept raw.
361    pub unknown_tagged_fields: Vec<RawTaggedField>,
362}
363
364impl Default for AddPartitionsToTxnTopic {
365    fn default() -> Self {
366        Self {
367            name: String::new(),
368            partitions: Vec::new(),
369            unknown_tagged_fields: Vec::new(),
370        }
371    }
372}
373
374impl AddPartitionsToTxnTopic {
375    /// Encode this `AddPartitionsToTxnTopic` at `version`.
376    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
377        if is_flexible(version) {
378            wire::put_compact_string(buf, &self.name);
379        } else {
380            wire::put_string(buf, &self.name)?;
381        }
382        if is_flexible(version) {
383            wire::put_compact_array_len(buf, Some(self.partitions.len()));
384        } else {
385            wire::put_array_len(buf, Some(self.partitions.len()))?;
386        }
387        for item in &self.partitions {
388            buf.put_i32(*item);
389        }
390        if is_flexible(version) {
391            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
392        }
393        Ok(())
394    }
395
396    /// Decode one `AddPartitionsToTxnTopic`, bounding allocation by the default
397    /// [`Limits`] derived from `buf`'s remaining length.
398    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
399        Self::decode_with_limits(buf, version, Limits::default())
400    }
401
402    /// Decode one `AddPartitionsToTxnTopic` under `limits`.
403    pub fn decode_with_limits(
404        buf: &mut impl Buf,
405        version: i16,
406        limits: Limits,
407    ) -> Result<Self, DecodeError> {
408        let mut budget = limits.budget(buf.remaining());
409        Self::decode_with_budget(buf, version, &mut budget)
410    }
411
412    /// Decode one `AddPartitionsToTxnTopic` against an existing `budget`.
413    pub fn decode_with_budget(
414        buf: &mut impl Buf,
415        version: i16,
416        budget: &mut Budget,
417    ) -> Result<Self, DecodeError> {
418        let mut this = Self::default();
419        this.name = if is_flexible(version) {
420            wire::get_compact_string(buf)?
421        } else {
422            wire::get_string(buf)?
423        };
424        this.partitions = {
425            let len = if is_flexible(version) {
426                wire::get_compact_array_len(buf)?
427            } else {
428                wire::get_array_len(buf)?
429            };
430            match len {
431                None => return Err(DecodeError::InvalidLength(-1)),
432                Some(n) => {
433                    let mut items = Vec::new();
434                    for _ in 0..n {
435                        budget.push(&mut items, wire::get_i32(buf)?)?;
436                    }
437                    items
438                }
439            }
440        };
441        if is_flexible(version) {
442            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
443        }
444        Ok(this)
445    }
446}