Skip to main content

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