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