Skip to main content

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