Skip to main content

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

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