Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
AddPartitionsToTxnRequest.borrowed.rs

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use bytes::BufMut;
3
4use crate::{
5    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6    primitives::{
7        fixed::{get_bool, get_i16, get_i64, put_bool, put_i16, put_i64},
8        string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
9        string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
10    },
11    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
12};
13pub const API_KEY: i16 = 24;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 5;
16pub const LATEST_STABLE_VERSION: i16 = 5;
17pub const FLEXIBLE_MIN: i16 = 3;
18#[inline]
19#[must_use]
20pub fn is_flexible(version: i16) -> bool {
21    version >= FLEXIBLE_MIN
22}
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct AddPartitionsToTxnRequest<'a> {
25    pub transactions: Vec<AddPartitionsToTxnTransaction<'a>>,
26    pub v3_and_below_transactional_id: &'a str,
27    pub v3_and_below_producer_id: i64,
28    pub v3_and_below_producer_epoch: i16,
29    pub v3_and_below_topics: Vec<super::common::add_partitions_to_txn_request::add_partitions_to_txn_topic::AddPartitionsToTxnTopic<'a>>,
30    pub unknown_tagged_fields: UnknownTaggedFields,
31}
32impl AddPartitionsToTxnRequest<'_> {
33    /// # Panics
34    ///
35    /// Panics if a records field contains an invalid encoded record batch.
36    #[must_use]
37    pub fn to_owned(
38        &self,
39    ) -> crate::owned::add_partitions_to_txn_request::AddPartitionsToTxnRequest {
40        crate::owned::add_partitions_to_txn_request::AddPartitionsToTxnRequest {
41            transactions: (self.transactions).iter().map(AddPartitionsToTxnTransaction::to_owned).collect(),
42            v3_and_below_transactional_id: (self.v3_and_below_transactional_id).to_string(),
43            v3_and_below_producer_id: (self.v3_and_below_producer_id),
44            v3_and_below_producer_epoch: (self.v3_and_below_producer_epoch),
45            v3_and_below_topics: (self.v3_and_below_topics).iter().map(super::common::add_partitions_to_txn_request::add_partitions_to_txn_topic::AddPartitionsToTxnTopic::to_owned).collect(),
46            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
47        }
48    }
49}
50impl Encode for AddPartitionsToTxnRequest<'_> {
51    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
52        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
53            return Err(ProtocolError::UnsupportedVersion {
54                api_key: API_KEY,
55                version,
56            });
57        }
58        let flex = is_flexible(version);
59        if version >= 4 {
60            {
61                crate::primitives::array::put_array_len(buf, (self.transactions).len(), flex);
62                for it in &self.transactions {
63                    it.encode(buf, version)?;
64                }
65            };
66        }
67        if (0..=3).contains(&version) {
68            if flex {
69                let () = put_compact_string(buf, self.v3_and_below_transactional_id);
70            } else {
71                let () = put_string(buf, self.v3_and_below_transactional_id);
72            }
73        }
74        if (0..=3).contains(&version) {
75            put_i64(buf, self.v3_and_below_producer_id);
76        }
77        if (0..=3).contains(&version) {
78            put_i16(buf, self.v3_and_below_producer_epoch);
79        }
80        if (0..=3).contains(&version) {
81            {
82                crate::primitives::array::put_array_len(
83                    buf,
84                    (self.v3_and_below_topics).len(),
85                    flex,
86                );
87                for it in &self.v3_and_below_topics {
88                    it.encode(buf, version)?;
89                }
90            };
91        }
92        if flex {
93            let tagged = WriteTaggedFields::new();
94            tagged.write(buf, &self.unknown_tagged_fields);
95        }
96        Ok(())
97    }
98    fn encoded_len(&self, version: i16) -> usize {
99        let flex = is_flexible(version);
100        let mut n: usize = 0;
101        if version >= 4 {
102            n += {
103                let prefix =
104                    crate::primitives::array::array_len_prefix_len((self.transactions).len(), flex);
105                let body: usize = (self.transactions)
106                    .iter()
107                    .map(|it| it.encoded_len(version))
108                    .sum();
109                prefix + body
110            };
111        }
112        if (0..=3).contains(&version) {
113            n += if flex {
114                compact_string_len(self.v3_and_below_transactional_id)
115            } else {
116                string_len(self.v3_and_below_transactional_id)
117            };
118        }
119        if (0..=3).contains(&version) {
120            n += 8;
121        }
122        if (0..=3).contains(&version) {
123            n += 2;
124        }
125        if (0..=3).contains(&version) {
126            n += {
127                let prefix = crate::primitives::array::array_len_prefix_len(
128                    (self.v3_and_below_topics).len(),
129                    flex,
130                );
131                let body: usize = (self.v3_and_below_topics)
132                    .iter()
133                    .map(|it| it.encoded_len(version))
134                    .sum();
135                prefix + body
136            };
137        }
138        if flex {
139            let known_pairs: Vec<(u32, usize)> = Vec::new();
140            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
141        }
142        n
143    }
144}
145impl<'de> DecodeBorrow<'de> for AddPartitionsToTxnRequest<'de> {
146    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
147        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
148            return Err(ProtocolError::UnsupportedVersion {
149                api_key: API_KEY,
150                version,
151            });
152        }
153        let flex = is_flexible(version);
154        let mut out = Self::default();
155        if version >= 4 {
156            out.transactions = {
157                let n = crate::primitives::array::get_array_len(buf, flex)?;
158                let mut v = Vec::with_capacity(n);
159                for _ in 0..n {
160                    v.push(AddPartitionsToTxnTransaction::decode_borrow(buf, version)?);
161                }
162                v
163            };
164        }
165        if (0..=3).contains(&version) {
166            out.v3_and_below_transactional_id = if flex {
167                get_compact_string_borrowed(buf)?
168            } else {
169                get_string_borrowed(buf)?
170            };
171        }
172        if (0..=3).contains(&version) {
173            out.v3_and_below_producer_id = get_i64(buf)?;
174        }
175        if (0..=3).contains(&version) {
176            out.v3_and_below_producer_epoch = get_i16(buf)?;
177        }
178        if (0..=3).contains(&version) {
179            out.v3_and_below_topics = {
180                let n = crate::primitives::array::get_array_len(buf, flex)?;
181                let mut v = Vec::with_capacity(n);
182                for _ in 0..n {
183                    v.push(super::common::add_partitions_to_txn_request::add_partitions_to_txn_topic::AddPartitionsToTxnTopic::decode_borrow(buf, version)?);
184                }
185                v
186            };
187        }
188        if flex {
189            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
190        }
191        Ok(out)
192    }
193}
194#[cfg(test)]
195impl AddPartitionsToTxnRequest<'_> {
196    #[must_use]
197    pub fn populated(version: i16) -> Self {
198        let mut m = Self::default();
199        if version >= 4 {
200            m.transactions = vec![AddPartitionsToTxnTransaction::populated(version)];
201        }
202        if (0..=3).contains(&version) {
203            m.v3_and_below_transactional_id = "x";
204        }
205        if (0..=3).contains(&version) {
206            m.v3_and_below_producer_id = 1i64;
207        }
208        if (0..=3).contains(&version) {
209            m.v3_and_below_producer_epoch = 1i16;
210        }
211        if (0..=3).contains(&version) {
212            m.v3_and_below_topics = vec![super::common::add_partitions_to_txn_request::add_partitions_to_txn_topic::AddPartitionsToTxnTopic::populated(version)];
213        }
214        m
215    }
216}
217#[derive(Debug, Clone, PartialEq, Eq, Default)]
218pub struct AddPartitionsToTxnTransaction<'a> {
219    pub transactional_id: &'a str,
220    pub producer_id: i64,
221    pub producer_epoch: i16,
222    pub verify_only: bool,
223    pub topics: Vec<super::common::add_partitions_to_txn_request::add_partitions_to_txn_topic::AddPartitionsToTxnTopic<'a>>,
224    pub unknown_tagged_fields: UnknownTaggedFields,
225}
226impl AddPartitionsToTxnTransaction<'_> {
227    /// # Panics
228    ///
229    /// Panics if a records field contains an invalid encoded record batch.
230    #[must_use]
231    pub fn to_owned(
232        &self,
233    ) -> crate::owned::add_partitions_to_txn_request::AddPartitionsToTxnTransaction {
234        crate::owned::add_partitions_to_txn_request::AddPartitionsToTxnTransaction {
235            transactional_id: (self.transactional_id).to_string(),
236            producer_id: (self.producer_id),
237            producer_epoch: (self.producer_epoch),
238            verify_only: (self.verify_only),
239            topics: (self.topics).iter().map(super::common::add_partitions_to_txn_request::add_partitions_to_txn_topic::AddPartitionsToTxnTopic::to_owned).collect(),
240            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
241        }
242    }
243}
244impl Encode for AddPartitionsToTxnTransaction<'_> {
245    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
246        let flex = version >= 3;
247        if version >= 4 {
248            if flex {
249                let () = put_compact_string(buf, self.transactional_id);
250            } else {
251                let () = put_string(buf, self.transactional_id);
252            }
253        }
254        if version >= 4 {
255            put_i64(buf, self.producer_id);
256        }
257        if version >= 4 {
258            put_i16(buf, self.producer_epoch);
259        }
260        if version >= 4 {
261            put_bool(buf, self.verify_only);
262        }
263        if version >= 4 {
264            {
265                crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
266                for it in &self.topics {
267                    it.encode(buf, version)?;
268                }
269            };
270        }
271        if flex {
272            let tagged = WriteTaggedFields::new();
273            tagged.write(buf, &self.unknown_tagged_fields);
274        }
275        Ok(())
276    }
277    fn encoded_len(&self, version: i16) -> usize {
278        let flex = version >= 3;
279        let mut n: usize = 0;
280        if version >= 4 {
281            n += if flex {
282                compact_string_len(self.transactional_id)
283            } else {
284                string_len(self.transactional_id)
285            };
286        }
287        if version >= 4 {
288            n += 8;
289        }
290        if version >= 4 {
291            n += 2;
292        }
293        if version >= 4 {
294            n += 1;
295        }
296        if version >= 4 {
297            n += {
298                let prefix =
299                    crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
300                let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
301                prefix + body
302            };
303        }
304        if flex {
305            let known_pairs: Vec<(u32, usize)> = Vec::new();
306            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
307        }
308        n
309    }
310}
311impl<'de> DecodeBorrow<'de> for AddPartitionsToTxnTransaction<'de> {
312    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
313        let flex = version >= 3;
314        let mut out = Self::default();
315        if version >= 4 {
316            out.transactional_id = if flex {
317                get_compact_string_borrowed(buf)?
318            } else {
319                get_string_borrowed(buf)?
320            };
321        }
322        if version >= 4 {
323            out.producer_id = get_i64(buf)?;
324        }
325        if version >= 4 {
326            out.producer_epoch = get_i16(buf)?;
327        }
328        if version >= 4 {
329            out.verify_only = get_bool(buf)?;
330        }
331        if version >= 4 {
332            out.topics = {
333                let n = crate::primitives::array::get_array_len(buf, flex)?;
334                let mut v = Vec::with_capacity(n);
335                for _ in 0..n {
336                    v.push(super::common::add_partitions_to_txn_request::add_partitions_to_txn_topic::AddPartitionsToTxnTopic::decode_borrow(buf, version)?);
337                }
338                v
339            };
340        }
341        if flex {
342            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
343        }
344        Ok(out)
345    }
346}
347#[cfg(test)]
348impl AddPartitionsToTxnTransaction<'_> {
349    #[must_use]
350    pub fn populated(version: i16) -> Self {
351        let mut m = Self::default();
352        if version >= 4 {
353            m.transactional_id = "x";
354        }
355        if version >= 4 {
356            m.producer_id = 1i64;
357        }
358        if version >= 4 {
359            m.producer_epoch = 1i16;
360        }
361        if version >= 4 {
362            m.verify_only = true;
363        }
364        if version >= 4 {
365            m.topics = vec![super::common::add_partitions_to_txn_request::add_partitions_to_txn_topic::AddPartitionsToTxnTopic::populated(version)];
366        }
367        m
368    }
369}