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,
9    string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12    get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13    get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::primitives::string_bytes::{put_bytes, put_compact_bytes, put_compact_nullable_bytes, put_nullable_bytes};
16use crate::primitives::string_bytes_borrowed::{get_compact_nullable_bytes_borrowed, get_nullable_bytes_borrowed};
17use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
18use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
19
20pub const API_KEY: i16 = 0;
21pub const MIN_VERSION: i16 = 3;
22pub const MAX_VERSION: i16 = 13;
23pub const FLEXIBLE_MIN: i16 = 9;
24
25#[inline]
26fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
27
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct ProduceRequest<'a> {
30    pub transactional_id: Option<&'a str>,
31    pub acks: i16,
32    pub timeout_ms: i32,
33    pub topic_data: Vec<TopicProduceData<'a>>,
34    pub unknown_tagged_fields: UnknownTaggedFields,
35}
36
37impl<'a> Default for ProduceRequest<'a> {
38    fn default() -> Self {
39        Self {
40            transactional_id: None,
41            acks: 0i16,
42            timeout_ms: 0i32,
43            topic_data: Vec::new(),
44            unknown_tagged_fields: Default::default(),
45        }
46    }
47}
48
49impl<'a> ProduceRequest<'a> {
50    pub fn to_owned(&self) -> crate::owned::produce_request::ProduceRequest {
51        crate::owned::produce_request::ProduceRequest {
52            transactional_id: (self.transactional_id).map(|s| s.to_string()),
53            acks: (self.acks),
54            timeout_ms: (self.timeout_ms),
55            topic_data: (self.topic_data).iter().map(|it| it.to_owned()).collect(),
56            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
57        }
58    }
59}
60
61impl<'a> Encode for ProduceRequest<'a> {
62    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
63        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
64            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
65        }
66        let flex = is_flexible(version);
67        if version >= 3 { if flex { put_compact_nullable_string(buf, self.transactional_id) } else { put_nullable_string(buf, self.transactional_id) } }
68        if version >= 0 { put_i16(buf, self.acks) }
69        if version >= 0 { put_i32(buf, self.timeout_ms) }
70        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topic_data).len(), flex); for it in &self.topic_data { it.encode(buf, version)?; } } }
71        if flex {
72            let tagged = WriteTaggedFields::new();
73            tagged.write(buf, &self.unknown_tagged_fields);
74        }
75        Ok(())
76    }
77    fn encoded_len(&self, version: i16) -> usize {
78        let flex = is_flexible(version);
79        let mut n: usize = 0;
80        if version >= 3 { n += if flex { compact_nullable_string_len(self.transactional_id) } else { nullable_string_len(self.transactional_id) }; }
81        if version >= 0 { n += 2; }
82        if version >= 0 { n += 4; }
83        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topic_data).len(), flex); let body: usize = (self.topic_data).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
84        if flex {
85            let known_pairs: Vec<(u32, usize)> = Vec::new();
86            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
87        }
88        n
89    }
90}
91
92impl<'de> DecodeBorrow<'de> for ProduceRequest<'de> {
93    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
94        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
95            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
96        }
97        let flex = is_flexible(version);
98        let mut out = Self::default();
99        if version >= 3 { out.transactional_id = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
100        if version >= 0 { out.acks = get_i16(buf)?; }
101        if version >= 0 { out.timeout_ms = get_i32(buf)?; }
102        if version >= 0 { out.topic_data = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(TopicProduceData::decode_borrow(buf, version)?); } v }; }
103        if flex {
104            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
105                Ok(false)
106            })?;
107        }
108        Ok(out)
109    }
110}
111
112#[derive(Debug, Clone, PartialEq, Eq)]
113pub struct TopicProduceData<'a> {
114    pub name: &'a str,
115    pub topic_id: crate::primitives::uuid::Uuid,
116    pub partition_data: Vec<PartitionProduceData<'a>>,
117    pub unknown_tagged_fields: UnknownTaggedFields,
118}
119
120impl<'a> Default for TopicProduceData<'a> {
121    fn default() -> Self {
122        Self {
123            name: "",
124            topic_id: Default::default(),
125            partition_data: Vec::new(),
126            unknown_tagged_fields: Default::default(),
127        }
128    }
129}
130
131impl<'a> TopicProduceData<'a> {
132    pub fn to_owned(&self) -> crate::owned::produce_request::TopicProduceData {
133        crate::owned::produce_request::TopicProduceData {
134            name: (self.name).to_string(),
135            topic_id: (self.topic_id),
136            partition_data: (self.partition_data).iter().map(|it| it.to_owned()).collect(),
137            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
138        }
139    }
140}
141
142impl<'a> Encode for TopicProduceData<'a> {
143    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
144        let flex = version >= 9;
145        if version >= 0 && version <= 12 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
146        if version >= 13 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
147        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partition_data).len(), flex); for it in &self.partition_data { it.encode(buf, version)?; } } }
148        if flex {
149            let tagged = WriteTaggedFields::new();
150            tagged.write(buf, &self.unknown_tagged_fields);
151        }
152        Ok(())
153    }
154    fn encoded_len(&self, version: i16) -> usize {
155        let flex = version >= 9;
156        let mut n: usize = 0;
157        if version >= 0 && version <= 12 { n += if flex { compact_string_len(self.name) } else { string_len(self.name) }; }
158        if version >= 13 { n += 16; }
159        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partition_data).len(), flex); let body: usize = (self.partition_data).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
160        if flex {
161            let known_pairs: Vec<(u32, usize)> = Vec::new();
162            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
163        }
164        n
165    }
166}
167
168impl<'de> DecodeBorrow<'de> for TopicProduceData<'de> {
169    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
170        let flex = version >= 9;
171        let mut out = Self::default();
172        if version >= 0 && version <= 12 { out.name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
173        if version >= 13 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
174        if version >= 0 { out.partition_data = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(PartitionProduceData::decode_borrow(buf, version)?); } v }; }
175        if flex {
176            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
177                Ok(false)
178            })?;
179        }
180        Ok(out)
181    }
182}
183
184#[derive(Debug, Clone, PartialEq, Eq)]
185pub struct PartitionProduceData<'a> {
186    pub index: i32,
187    pub records: Option<crate::records::RecordsPayloadBorrowed<'a>>,
188    pub unknown_tagged_fields: UnknownTaggedFields,
189}
190
191impl<'a> Default for PartitionProduceData<'a> {
192    fn default() -> Self {
193        Self {
194            index: 0i32,
195            records: None,
196            unknown_tagged_fields: Default::default(),
197        }
198    }
199}
200
201impl<'a> PartitionProduceData<'a> {
202    pub fn to_owned(&self) -> crate::owned::produce_request::PartitionProduceData {
203        crate::owned::produce_request::PartitionProduceData {
204            index: (self.index),
205            records: (self.records).as_ref().map(|rb| rb.to_owned().expect("records to_owned")),
206            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
207        }
208    }
209}
210
211impl<'a> Encode for PartitionProduceData<'a> {
212    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
213        let flex = version >= 9;
214        if version >= 0 { put_i32(buf, self.index) }
215        if version >= 0 { match &self.records { None => if flex { put_compact_nullable_bytes(buf, None) } else { put_nullable_bytes(buf, None) }, Some(__rb) => { let mut __rb_buf = bytes::BytesMut::new(); <crate::records::RecordsPayloadBorrowed as crate::Encode>::encode(__rb, &mut __rb_buf, version)?; if flex { put_compact_bytes(buf, &__rb_buf) } else { put_bytes(buf, &__rb_buf) } } } }
216        if flex {
217            let tagged = WriteTaggedFields::new();
218            tagged.write(buf, &self.unknown_tagged_fields);
219        }
220        Ok(())
221    }
222    fn encoded_len(&self, version: i16) -> usize {
223        let flex = version >= 9;
224        let mut n: usize = 0;
225        if version >= 0 { n += 4; }
226        if version >= 0 { n += match &self.records { None => if flex { crate::primitives::varint::uvarint_len(0) } else { 4 }, Some(__rb) => { let __rb_len = <crate::records::RecordsPayloadBorrowed as crate::Encode>::encoded_len(__rb, version); if flex { crate::primitives::string_bytes::compact_bytes_len_from_size(__rb_len) } else { 4 + __rb_len } } }; }
227        if flex {
228            let known_pairs: Vec<(u32, usize)> = Vec::new();
229            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
230        }
231        n
232    }
233}
234
235impl<'de> DecodeBorrow<'de> for PartitionProduceData<'de> {
236    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
237        let flex = version >= 9;
238        let mut out = Self::default();
239        if version >= 0 { out.index = get_i32(buf)?; }
240        if version >= 0 { out.records = { let __rb_opt = if flex { get_compact_nullable_bytes_borrowed(buf)? } else { get_nullable_bytes_borrowed(buf)? }; match __rb_opt { None => None, Some(__rb_slice) => { let mut __rb_cur = __rb_slice; Some(<crate::records::RecordsPayloadBorrowed as crate::DecodeBorrow>::decode_borrow(&mut __rb_cur, version)?) } } }; }
241        if flex {
242            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
243                Ok(false)
244            })?;
245        }
246        Ok(out)
247    }
248}