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