Skip to main content

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

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