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