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