Skip to main content

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

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