Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
ProduceRequest.borrowed.rs

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