Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
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 AddPartitionsToTxnResponse<'a> {
20    pub throttle_time_ms: i32,
21    pub error_code: i16,
22    pub results_by_transaction: Vec<AddPartitionsToTxnResult<'a>>,
23    pub results_by_topic_v3_and_below: Vec<
24        super::common::add_partitions_to_txn_response::add_partitions_to_txn_topic_result::AddPartitionsToTxnTopicResult<
25            'a,
26        >,
27    >,
28    pub unknown_tagged_fields: UnknownTaggedFields,
29}
30impl AddPartitionsToTxnResponse<'_> {
31    pub fn to_owned(
32        &self,
33    ) -> crate::owned::add_partitions_to_txn_response::AddPartitionsToTxnResponse {
34        crate::owned::add_partitions_to_txn_response::AddPartitionsToTxnResponse {
35            throttle_time_ms: (self.throttle_time_ms),
36            error_code: (self.error_code),
37            results_by_transaction: (self.results_by_transaction)
38                .iter()
39                .map(AddPartitionsToTxnResult::to_owned)
40                .collect(),
41            results_by_topic_v3_and_below: (self.results_by_topic_v3_and_below)
42                .iter()
43                .map(super::common::add_partitions_to_txn_response::add_partitions_to_txn_topic_result::AddPartitionsToTxnTopicResult::to_owned)
44                .collect(),
45            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
46        }
47    }
48}
49impl Encode for AddPartitionsToTxnResponse<'_> {
50    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
51        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
52            return Err(ProtocolError::UnsupportedVersion {
53                api_key: API_KEY,
54                version,
55            });
56        }
57        let flex = is_flexible(version);
58        if version >= 0 {
59            put_i32(buf, self.throttle_time_ms);
60        }
61        if version >= 4 {
62            put_i16(buf, self.error_code);
63        }
64        if version >= 4 {
65            {
66                crate::primitives::array::put_array_len(
67                    buf,
68                    (self.results_by_transaction).len(),
69                    flex,
70                );
71                for it in &self.results_by_transaction {
72                    it.encode(buf, version)?;
73                }
74            }
75        }
76        if (0..=3).contains(&version) {
77            {
78                crate::primitives::array::put_array_len(
79                    buf,
80                    (self.results_by_topic_v3_and_below).len(),
81                    flex,
82                );
83                for it in &self.results_by_topic_v3_and_below {
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 >= 0 {
98            n += 4;
99        }
100        if version >= 4 {
101            n += 2;
102        }
103        if version >= 4 {
104            n += {
105                let prefix = crate::primitives::array::array_len_prefix_len(
106                    (self.results_by_transaction).len(),
107                    flex,
108                );
109                let body: usize = (self.results_by_transaction)
110                    .iter()
111                    .map(|it| it.encoded_len(version))
112                    .sum();
113                prefix + body
114            };
115        }
116        if (0..=3).contains(&version) {
117            n += {
118                let prefix = crate::primitives::array::array_len_prefix_len(
119                    (self.results_by_topic_v3_and_below).len(),
120                    flex,
121                );
122                let body: usize = (self.results_by_topic_v3_and_below)
123                    .iter()
124                    .map(|it| it.encoded_len(version))
125                    .sum();
126                prefix + body
127            };
128        }
129        if flex {
130            let known_pairs: Vec<(u32, usize)> = Vec::new();
131            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
132        }
133        n
134    }
135}
136impl<'de> DecodeBorrow<'de> for AddPartitionsToTxnResponse<'de> {
137    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
138        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
139            return Err(ProtocolError::UnsupportedVersion {
140                api_key: API_KEY,
141                version,
142            });
143        }
144        let flex = is_flexible(version);
145        let mut out = Self::default();
146        if version >= 0 {
147            out.throttle_time_ms = get_i32(buf)?;
148        }
149        if version >= 4 {
150            out.error_code = get_i16(buf)?;
151        }
152        if version >= 4 {
153            out.results_by_transaction = {
154                let n = crate::primitives::array::get_array_len(buf, flex)?;
155                let mut v = Vec::with_capacity(n);
156                for _ in 0..n {
157                    v.push(AddPartitionsToTxnResult::decode_borrow(buf, version)?);
158                }
159                v
160            };
161        }
162        if (0..=3).contains(&version) {
163            out.results_by_topic_v3_and_below = {
164                let n = crate::primitives::array::get_array_len(buf, flex)?;
165                let mut v = Vec::with_capacity(n);
166                for _ in 0..n {
167                    v.push(
168                        super::common::add_partitions_to_txn_response::add_partitions_to_txn_topic_result::AddPartitionsToTxnTopicResult::decode_borrow(
169                            buf,
170                            version,
171                        )?,
172                    );
173                }
174                v
175            };
176        }
177        if flex {
178            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
179        }
180        Ok(out)
181    }
182}
183#[cfg(test)]
184impl AddPartitionsToTxnResponse<'_> {
185    #[must_use]
186    pub fn populated(version: i16) -> Self {
187        let mut m = Self::default();
188        if version >= 0 {
189            m.throttle_time_ms = 1i32;
190        }
191        if version >= 4 {
192            m.error_code = 1i16;
193        }
194        if version >= 4 {
195            m.results_by_transaction = vec![AddPartitionsToTxnResult::populated(version)];
196        }
197        if (0..=3).contains(&version) {
198            m.results_by_topic_v3_and_below = vec![
199                super::common::add_partitions_to_txn_response::add_partitions_to_txn_topic_result::AddPartitionsToTxnTopicResult::populated(version)
200            ];
201        }
202        m
203    }
204}
205#[derive(Debug, Clone, PartialEq, Eq, Default)]
206pub struct AddPartitionsToTxnResult<'a> {
207    pub transactional_id: &'a str,
208    pub topic_results: Vec<
209        super::common::add_partitions_to_txn_response::add_partitions_to_txn_topic_result::AddPartitionsToTxnTopicResult<
210            'a,
211        >,
212    >,
213    pub unknown_tagged_fields: UnknownTaggedFields,
214}
215impl AddPartitionsToTxnResult<'_> {
216    pub fn to_owned(
217        &self,
218    ) -> crate::owned::add_partitions_to_txn_response::AddPartitionsToTxnResult {
219        crate::owned::add_partitions_to_txn_response::AddPartitionsToTxnResult {
220            transactional_id: (self.transactional_id).to_string(),
221            topic_results: (self.topic_results)
222                .iter()
223                .map(super::common::add_partitions_to_txn_response::add_partitions_to_txn_topic_result::AddPartitionsToTxnTopicResult::to_owned)
224                .collect(),
225            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
226        }
227    }
228}
229impl Encode for AddPartitionsToTxnResult<'_> {
230    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
231        let flex = version >= 3;
232        if version >= 4 {
233            if flex {
234                put_compact_string(buf, self.transactional_id);
235            } else {
236                put_string(buf, self.transactional_id);
237            }
238        }
239        if version >= 4 {
240            {
241                crate::primitives::array::put_array_len(buf, (self.topic_results).len(), flex);
242                for it in &self.topic_results {
243                    it.encode(buf, version)?;
244                }
245            }
246        }
247        if flex {
248            let tagged = WriteTaggedFields::new();
249            tagged.write(buf, &self.unknown_tagged_fields);
250        }
251        Ok(())
252    }
253    fn encoded_len(&self, version: i16) -> usize {
254        let flex = version >= 3;
255        let mut n: usize = 0;
256        if version >= 4 {
257            n += if flex {
258                compact_string_len(self.transactional_id)
259            } else {
260                string_len(self.transactional_id)
261            };
262        }
263        if version >= 4 {
264            n += {
265                let prefix = crate::primitives::array::array_len_prefix_len(
266                    (self.topic_results).len(),
267                    flex,
268                );
269                let body: usize = (self.topic_results)
270                    .iter()
271                    .map(|it| it.encoded_len(version))
272                    .sum();
273                prefix + body
274            };
275        }
276        if flex {
277            let known_pairs: Vec<(u32, usize)> = Vec::new();
278            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
279        }
280        n
281    }
282}
283impl<'de> DecodeBorrow<'de> for AddPartitionsToTxnResult<'de> {
284    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
285        let flex = version >= 3;
286        let mut out = Self::default();
287        if version >= 4 {
288            out.transactional_id = if flex {
289                get_compact_string_borrowed(buf)?
290            } else {
291                get_string_borrowed(buf)?
292            };
293        }
294        if version >= 4 {
295            out.topic_results = {
296                let n = crate::primitives::array::get_array_len(buf, flex)?;
297                let mut v = Vec::with_capacity(n);
298                for _ in 0..n {
299                    v.push(
300                        super::common::add_partitions_to_txn_response::add_partitions_to_txn_topic_result::AddPartitionsToTxnTopicResult::decode_borrow(
301                            buf,
302                            version,
303                        )?,
304                    );
305                }
306                v
307            };
308        }
309        if flex {
310            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
311        }
312        Ok(out)
313    }
314}
315#[cfg(test)]
316impl AddPartitionsToTxnResult<'_> {
317    #[must_use]
318    pub fn populated(version: i16) -> Self {
319        let mut m = Self::default();
320        if version >= 4 {
321            m.transactional_id = "x";
322        }
323        if version >= 4 {
324            m.topic_results = vec![
325                super::common::add_partitions_to_txn_response::add_partitions_to_txn_topic_result::AddPartitionsToTxnTopicResult::populated(version)
326            ];
327        }
328        m
329    }
330}