Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
ProduceRequest.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_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
6    get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
7    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
8};
9use crate::primitives::string_bytes::{
10    get_compact_nullable_bytes_owned, get_nullable_bytes_owned, put_bytes, put_compact_bytes,
11    put_compact_nullable_bytes, put_nullable_bytes,
12};
13use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
14use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
15use bytes::{Buf, BufMut};
16pub const API_KEY: i16 = 0;
17pub const MIN_VERSION: i16 = 3;
18pub const MAX_VERSION: i16 = 13;
19pub const FLEXIBLE_MIN: i16 = 9;
20#[inline]
21fn is_flexible(version: i16) -> bool {
22    version >= FLEXIBLE_MIN
23}
24#[derive(Debug, Clone, PartialEq, Eq, Default)]
25pub struct ProduceRequest {
26    pub transactional_id: Option<String>,
27    pub acks: i16,
28    pub timeout_ms: i32,
29    pub topic_data: Vec<TopicProduceData>,
30    pub unknown_tagged_fields: UnknownTaggedFields,
31}
32impl Encode for ProduceRequest {
33    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
34        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
35            return Err(ProtocolError::UnsupportedVersion {
36                api_key: API_KEY,
37                version,
38            });
39        }
40        let flex = is_flexible(version);
41        if version >= 3 {
42            if flex {
43                put_compact_nullable_string(buf, self.transactional_id.as_deref());
44            } else {
45                put_nullable_string(buf, self.transactional_id.as_deref());
46            }
47        }
48        if version >= 0 {
49            put_i16(buf, self.acks);
50        }
51        if version >= 0 {
52            put_i32(buf, self.timeout_ms);
53        }
54        if version >= 0 {
55            {
56                crate::primitives::array::put_array_len(buf, (self.topic_data).len(), flex);
57                for it in &self.topic_data {
58                    it.encode(buf, version)?;
59                }
60            }
61        }
62        if flex {
63            let tagged = WriteTaggedFields::new();
64            tagged.write(buf, &self.unknown_tagged_fields);
65        }
66        Ok(())
67    }
68    fn encoded_len(&self, version: i16) -> usize {
69        let flex = is_flexible(version);
70        let mut n: usize = 0;
71        if version >= 3 {
72            n += if flex {
73                compact_nullable_string_len(self.transactional_id.as_deref())
74            } else {
75                nullable_string_len(self.transactional_id.as_deref())
76            };
77        }
78        if version >= 0 {
79            n += 2;
80        }
81        if version >= 0 {
82            n += 4;
83        }
84        if version >= 0 {
85            n += {
86                let prefix =
87                    crate::primitives::array::array_len_prefix_len((self.topic_data).len(), flex);
88                let body: usize = (self.topic_data)
89                    .iter()
90                    .map(|it| it.encoded_len(version))
91                    .sum();
92                prefix + body
93            };
94        }
95        if flex {
96            let known_pairs: Vec<(u32, usize)> = Vec::new();
97            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
98        }
99        n
100    }
101}
102impl Decode<'_> for ProduceRequest {
103    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
104        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
105            return Err(ProtocolError::UnsupportedVersion {
106                api_key: API_KEY,
107                version,
108            });
109        }
110        let flex = is_flexible(version);
111        let mut out = Self::default();
112        if version >= 3 {
113            out.transactional_id = if flex {
114                get_compact_nullable_string_owned(buf)?
115            } else {
116                get_nullable_string_owned(buf)?
117            };
118        }
119        if version >= 0 {
120            out.acks = get_i16(buf)?;
121        }
122        if version >= 0 {
123            out.timeout_ms = get_i32(buf)?;
124        }
125        if version >= 0 {
126            out.topic_data = {
127                let n = crate::primitives::array::get_array_len(buf, flex)?;
128                let mut v = Vec::with_capacity(n);
129                for _ in 0..n {
130                    v.push(TopicProduceData::decode(buf, version)?);
131                }
132                v
133            };
134        }
135        if flex {
136            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
137        }
138        Ok(out)
139    }
140}
141#[cfg(test)]
142impl ProduceRequest {
143    #[must_use]
144    pub fn populated(version: i16) -> Self {
145        let mut m = Self::default();
146        if version >= 3 {
147            m.transactional_id = Some("x".to_string());
148        }
149        if version >= 0 {
150            m.acks = 1i16;
151        }
152        if version >= 0 {
153            m.timeout_ms = 1i32;
154        }
155        if version >= 0 {
156            m.topic_data = vec![TopicProduceData::populated(version)];
157        }
158        m
159    }
160}
161#[derive(Debug, Clone, PartialEq, Eq, Default)]
162pub struct TopicProduceData {
163    pub name: String,
164    pub topic_id: crate::primitives::uuid::Uuid,
165    pub partition_data: Vec<PartitionProduceData>,
166    pub unknown_tagged_fields: UnknownTaggedFields,
167}
168impl Encode for TopicProduceData {
169    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
170        let flex = version >= 9;
171        if (0..=12).contains(&version) {
172            if flex {
173                put_compact_string(buf, &self.name);
174            } else {
175                put_string(buf, &self.name);
176            }
177        }
178        if version >= 13 {
179            crate::primitives::uuid::put_uuid(buf, self.topic_id);
180        }
181        if version >= 0 {
182            {
183                crate::primitives::array::put_array_len(buf, (self.partition_data).len(), flex);
184                for it in &self.partition_data {
185                    it.encode(buf, version)?;
186                }
187            }
188        }
189        if flex {
190            let tagged = WriteTaggedFields::new();
191            tagged.write(buf, &self.unknown_tagged_fields);
192        }
193        Ok(())
194    }
195    fn encoded_len(&self, version: i16) -> usize {
196        let flex = version >= 9;
197        let mut n: usize = 0;
198        if (0..=12).contains(&version) {
199            n += if flex {
200                compact_string_len(&self.name)
201            } else {
202                string_len(&self.name)
203            };
204        }
205        if version >= 13 {
206            n += 16;
207        }
208        if version >= 0 {
209            n += {
210                let prefix = crate::primitives::array::array_len_prefix_len(
211                    (self.partition_data).len(),
212                    flex,
213                );
214                let body: usize = (self.partition_data)
215                    .iter()
216                    .map(|it| it.encoded_len(version))
217                    .sum();
218                prefix + body
219            };
220        }
221        if flex {
222            let known_pairs: Vec<(u32, usize)> = Vec::new();
223            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
224        }
225        n
226    }
227}
228impl Decode<'_> for TopicProduceData {
229    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
230        let flex = version >= 9;
231        let mut out = Self::default();
232        if (0..=12).contains(&version) {
233            out.name = if flex {
234                get_compact_string_owned(buf)?
235            } else {
236                get_string_owned(buf)?
237            };
238        }
239        if version >= 13 {
240            out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
241        }
242        if version >= 0 {
243            out.partition_data = {
244                let n = crate::primitives::array::get_array_len(buf, flex)?;
245                let mut v = Vec::with_capacity(n);
246                for _ in 0..n {
247                    v.push(PartitionProduceData::decode(buf, version)?);
248                }
249                v
250            };
251        }
252        if flex {
253            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
254        }
255        Ok(out)
256    }
257}
258#[cfg(test)]
259impl TopicProduceData {
260    #[must_use]
261    pub fn populated(version: i16) -> Self {
262        let mut m = Self::default();
263        if (0..=12).contains(&version) {
264            m.name = "x".to_string();
265        }
266        if version >= 13 {
267            m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
268        }
269        if version >= 0 {
270            m.partition_data = vec![PartitionProduceData::populated(version)];
271        }
272        m
273    }
274}
275#[derive(Debug, Clone, PartialEq, Eq, Default)]
276pub struct PartitionProduceData {
277    pub index: i32,
278    pub records: Option<crate::records::RecordsPayload>,
279    pub unknown_tagged_fields: UnknownTaggedFields,
280}
281impl Encode for PartitionProduceData {
282    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
283        let flex = version >= 9;
284        if version >= 0 {
285            put_i32(buf, self.index);
286        }
287        if version >= 0 {
288            match &self.records {
289                None => {
290                    if flex {
291                        put_compact_nullable_bytes(buf, None);
292                    } else {
293                        put_nullable_bytes(buf, None);
294                    }
295                }
296                Some(__rb) => {
297                    let mut __rb_buf = bytes::BytesMut::new();
298                    <crate::records::RecordsPayload as crate::Encode>::encode(
299                        __rb,
300                        &mut __rb_buf,
301                        version,
302                    )?;
303                    if flex {
304                        put_compact_bytes(buf, &__rb_buf);
305                    } else {
306                        put_bytes(buf, &__rb_buf);
307                    }
308                }
309            }
310        }
311        if flex {
312            let tagged = WriteTaggedFields::new();
313            tagged.write(buf, &self.unknown_tagged_fields);
314        }
315        Ok(())
316    }
317    fn encoded_len(&self, version: i16) -> usize {
318        let flex = version >= 9;
319        let mut n: usize = 0;
320        if version >= 0 {
321            n += 4;
322        }
323        if version >= 0 {
324            n += match &self.records {
325                None => {
326                    if flex {
327                        crate::primitives::varint::uvarint_len(0)
328                    } else {
329                        4
330                    }
331                }
332                Some(__rb) => {
333                    let __rb_len = <crate::records::RecordsPayload as crate::Encode>::encoded_len(
334                        __rb, version,
335                    );
336                    if flex {
337                        crate::primitives::string_bytes::compact_bytes_len_from_size(__rb_len)
338                    } else {
339                        4 + __rb_len
340                    }
341                }
342            };
343        }
344        if flex {
345            let known_pairs: Vec<(u32, usize)> = Vec::new();
346            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
347        }
348        n
349    }
350}
351impl Decode<'_> for PartitionProduceData {
352    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
353        let flex = version >= 9;
354        let mut out = Self::default();
355        if version >= 0 {
356            out.index = get_i32(buf)?;
357        }
358        if version >= 0 {
359            out.records = {
360                let __rb_opt = if flex {
361                    get_compact_nullable_bytes_owned(buf)?
362                } else {
363                    get_nullable_bytes_owned(buf)?
364                };
365                match __rb_opt {
366                    None => None,
367                    Some(__rb_bytes) => {
368                        let mut __rb_cur: &[u8] = &__rb_bytes;
369                        Some(<crate::records::RecordsPayload as crate::Decode>::decode(
370                            &mut __rb_cur,
371                            version,
372                        )?)
373                    }
374                }
375            };
376        }
377        if flex {
378            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
379        }
380        Ok(out)
381    }
382}
383#[cfg(test)]
384impl PartitionProduceData {
385    #[must_use]
386    pub fn populated(version: i16) -> Self {
387        let mut m = Self::default();
388        if version >= 0 {
389            m.index = 1i32;
390        }
391        m
392    }
393}
394/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
395/// Only includes fields valid for the given version.
396#[must_use]
397#[allow(unused_comparisons)]
398pub fn default_json(version: i16) -> ::serde_json::Value {
399    let mut obj = ::serde_json::Map::new();
400    if version >= 3 {
401        obj.insert("transactionalId".to_string(), ::serde_json::Value::Null);
402    }
403    obj.insert("acks".to_string(), ::serde_json::json!(0));
404    obj.insert("timeoutMs".to_string(), ::serde_json::json!(0));
405    obj.insert("topicData".to_string(), ::serde_json::Value::Array(vec![]));
406    ::serde_json::Value::Object(obj)
407}
408impl crate::ProtocolRequest for ProduceRequest {
409    const API_KEY: i16 = API_KEY;
410    const MIN_VERSION: i16 = MIN_VERSION;
411    const MAX_VERSION: i16 = MAX_VERSION;
412    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
413    type Response = super::produce_response::ProduceResponse;
414}