Skip to main content

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