Skip to main content

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

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