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