1use bytes::BufMut;
4
5use crate::primitives::fixed::{get_i16, get_i32, get_i64, put_i16, put_i32, put_i64};
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::tagged_fields::{encode_to_bytes, read_tagged_fields, tagged_fields_len, WriteTaggedFields};
16use crate::{Decode, DecodeBorrow, 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 { version >= FLEXIBLE_MIN }
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct ProduceResponse<'a> {
28 pub responses: Vec<TopicProduceResponse<'a>>,
29 pub throttle_time_ms: i32,
30 pub node_endpoints: Vec<crate::owned::produce_response::NodeEndpoint>,
31 pub unknown_tagged_fields: UnknownTaggedFields,
32}
33
34impl<'a> Default for ProduceResponse<'a> {
35 fn default() -> Self {
36 Self {
37 responses: Vec::new(),
38 throttle_time_ms: 0i32,
39 node_endpoints: Vec::new(),
40 unknown_tagged_fields: Default::default(),
41 }
42 }
43}
44
45impl<'a> ProduceResponse<'a> {
46 pub fn to_owned(&self) -> crate::owned::produce_response::ProduceResponse {
47 crate::owned::produce_response::ProduceResponse {
48 responses: (self.responses).iter().map(|it| it.to_owned()).collect(),
49 throttle_time_ms: (self.throttle_time_ms),
50 node_endpoints: self.node_endpoints.clone(),
51 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
52 }
53 }
54}
55
56impl<'a> Encode for ProduceResponse<'a> {
57 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
58 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
59 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
60 }
61 let flex = is_flexible(version);
62 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.responses).len(), flex); for it in &self.responses { it.encode(buf, version)?; } } }
63 if version >= 1 { put_i32(buf, self.throttle_time_ms) }
64 if flex {
65 let mut tagged = WriteTaggedFields::new();
66 if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
67 let payload = encode_to_bytes({ let prefix = crate::primitives::array::array_len_prefix_len((self.node_endpoints).len(), flex); let body: usize = (self.node_endpoints).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }, |b| { { crate::primitives::array::put_array_len(b, (self.node_endpoints).len(), flex); for it in &self.node_endpoints { it.encode(b, version)?; } }; Ok(()) });
68 tagged.add(0, payload);
69 }
70 tagged.write(buf, &self.unknown_tagged_fields);
71 }
72 Ok(())
73 }
74 fn encoded_len(&self, version: i16) -> usize {
75 let flex = is_flexible(version);
76 let mut n: usize = 0;
77 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.responses).len(), flex); let body: usize = (self.responses).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
78 if version >= 1 { n += 4; }
79 if flex {
80 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
81 if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
82 known_pairs.push((0, { let prefix = crate::primitives::array::array_len_prefix_len((self.node_endpoints).len(), flex); let body: usize = (self.node_endpoints).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }));
83 }
84 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
85 }
86 n
87 }
88}
89
90impl<'de> DecodeBorrow<'de> for ProduceResponse<'de> {
91 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
92 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
93 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
94 }
95 let flex = is_flexible(version);
96 let mut out = Self::default();
97 if version >= 0 { out.responses = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(TopicProduceResponse::decode_borrow(buf, version)?); } v }; }
98 if version >= 1 { out.throttle_time_ms = get_i32(buf)?; }
99 if flex {
100 let mut tag_node_endpoints = None;
102 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
103 match tag {
104 0 => { tag_node_endpoints = Some({ let b: &mut &[u8] = payload; { let n = crate::primitives::array::get_array_len(b, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(crate::owned::produce_response::NodeEndpoint::decode(b, version)?); } v } }); Ok(true) }
105 _ => Ok(false),
106 }
107 })?;
108 if let Some(v) = tag_node_endpoints { out.node_endpoints = v; }
109 }
110 Ok(out)
111 }
112}
113
114#[derive(Debug, Clone, PartialEq, Eq)]
115pub struct TopicProduceResponse<'a> {
116 pub name: &'a str,
117 pub topic_id: crate::primitives::uuid::Uuid,
118 pub partition_responses: Vec<PartitionProduceResponse<'a>>,
119 pub unknown_tagged_fields: UnknownTaggedFields,
120}
121
122impl<'a> Default for TopicProduceResponse<'a> {
123 fn default() -> Self {
124 Self {
125 name: "",
126 topic_id: Default::default(),
127 partition_responses: Vec::new(),
128 unknown_tagged_fields: Default::default(),
129 }
130 }
131}
132
133impl<'a> TopicProduceResponse<'a> {
134 pub fn to_owned(&self) -> crate::owned::produce_response::TopicProduceResponse {
135 crate::owned::produce_response::TopicProduceResponse {
136 name: (self.name).to_string(),
137 topic_id: (self.topic_id),
138 partition_responses: (self.partition_responses).iter().map(|it| it.to_owned()).collect(),
139 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
140 }
141 }
142}
143
144impl<'a> Encode for TopicProduceResponse<'a> {
145 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
146 let flex = version >= 9;
147 if version >= 0 && version <= 12 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
148 if version >= 13 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
149 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partition_responses).len(), flex); for it in &self.partition_responses { it.encode(buf, version)?; } } }
150 if flex {
151 let tagged = WriteTaggedFields::new();
152 tagged.write(buf, &self.unknown_tagged_fields);
153 }
154 Ok(())
155 }
156 fn encoded_len(&self, version: i16) -> usize {
157 let flex = version >= 9;
158 let mut n: usize = 0;
159 if version >= 0 && version <= 12 { n += if flex { compact_string_len(self.name) } else { string_len(self.name) }; }
160 if version >= 13 { n += 16; }
161 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partition_responses).len(), flex); let body: usize = (self.partition_responses).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
162 if flex {
163 let known_pairs: Vec<(u32, usize)> = Vec::new();
164 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
165 }
166 n
167 }
168}
169
170impl<'de> DecodeBorrow<'de> for TopicProduceResponse<'de> {
171 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
172 let flex = version >= 9;
173 let mut out = Self::default();
174 if version >= 0 && version <= 12 { out.name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
175 if version >= 13 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
176 if version >= 0 { out.partition_responses = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(PartitionProduceResponse::decode_borrow(buf, version)?); } v }; }
177 if flex {
178 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
179 Ok(false)
180 })?;
181 }
182 Ok(out)
183 }
184}
185
186#[derive(Debug, Clone, PartialEq, Eq)]
187pub struct PartitionProduceResponse<'a> {
188 pub index: i32,
189 pub error_code: i16,
190 pub base_offset: i64,
191 pub log_append_time_ms: i64,
192 pub log_start_offset: i64,
193 pub record_errors: Vec<BatchIndexAndErrorMessage<'a>>,
194 pub error_message: Option<&'a str>,
195 pub current_leader: LeaderIdAndEpoch,
196 pub unknown_tagged_fields: UnknownTaggedFields,
197}
198
199impl<'a> Default for PartitionProduceResponse<'a> {
200 fn default() -> Self {
201 Self {
202 index: 0i32,
203 error_code: 0i16,
204 base_offset: 0i64,
205 log_append_time_ms: -1i64,
206 log_start_offset: -1i64,
207 record_errors: Vec::new(),
208 error_message: None,
209 current_leader: Default::default(),
210 unknown_tagged_fields: Default::default(),
211 }
212 }
213}
214
215impl<'a> PartitionProduceResponse<'a> {
216 pub fn to_owned(&self) -> crate::owned::produce_response::PartitionProduceResponse {
217 crate::owned::produce_response::PartitionProduceResponse {
218 index: (self.index),
219 error_code: (self.error_code),
220 base_offset: (self.base_offset),
221 log_append_time_ms: (self.log_append_time_ms),
222 log_start_offset: (self.log_start_offset),
223 record_errors: (self.record_errors).iter().map(|it| it.to_owned()).collect(),
224 error_message: (self.error_message).map(|s| s.to_string()),
225 current_leader: (self.current_leader).to_owned(),
226 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
227 }
228 }
229}
230
231impl<'a> Encode for PartitionProduceResponse<'a> {
232 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
233 let flex = version >= 9;
234 if version >= 0 { put_i32(buf, self.index) }
235 if version >= 0 { put_i16(buf, self.error_code) }
236 if version >= 0 { put_i64(buf, self.base_offset) }
237 if version >= 2 { put_i64(buf, self.log_append_time_ms) }
238 if version >= 5 { put_i64(buf, self.log_start_offset) }
239 if version >= 8 { { crate::primitives::array::put_array_len(buf, (self.record_errors).len(), flex); for it in &self.record_errors { it.encode(buf, version)?; } } }
240 if version >= 8 { if flex { put_compact_nullable_string(buf, self.error_message) } else { put_nullable_string(buf, self.error_message) } }
241 if flex {
242 let mut tagged = WriteTaggedFields::new();
243 if !(crate::codegen_helpers::is_default(&self.current_leader)) {
244 let payload = encode_to_bytes(self.current_leader.encoded_len(version), |b| { self.current_leader.encode(b, version)?; Ok(()) });
245 tagged.add(0, payload);
246 }
247 tagged.write(buf, &self.unknown_tagged_fields);
248 }
249 Ok(())
250 }
251 fn encoded_len(&self, version: i16) -> usize {
252 let flex = version >= 9;
253 let mut n: usize = 0;
254 if version >= 0 { n += 4; }
255 if version >= 0 { n += 2; }
256 if version >= 0 { n += 8; }
257 if version >= 2 { n += 8; }
258 if version >= 5 { n += 8; }
259 if version >= 8 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.record_errors).len(), flex); let body: usize = (self.record_errors).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
260 if version >= 8 { n += if flex { compact_nullable_string_len(self.error_message) } else { nullable_string_len(self.error_message) }; }
261 if flex {
262 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
263 if !(crate::codegen_helpers::is_default(&self.current_leader)) {
264 known_pairs.push((0, self.current_leader.encoded_len(version)));
265 }
266 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
267 }
268 n
269 }
270}
271
272impl<'de> DecodeBorrow<'de> for PartitionProduceResponse<'de> {
273 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
274 let flex = version >= 9;
275 let mut out = Self::default();
276 if version >= 0 { out.index = get_i32(buf)?; }
277 if version >= 0 { out.error_code = get_i16(buf)?; }
278 if version >= 0 { out.base_offset = get_i64(buf)?; }
279 if version >= 2 { out.log_append_time_ms = get_i64(buf)?; }
280 if version >= 5 { out.log_start_offset = get_i64(buf)?; }
281 if version >= 8 { out.record_errors = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(BatchIndexAndErrorMessage::decode_borrow(buf, version)?); } v }; }
282 if version >= 8 { out.error_message = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
283 if flex {
284 let mut tag_current_leader = None;
286 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
287 match tag {
288 0 => { tag_current_leader = Some({ let b: &mut &[u8] = payload; LeaderIdAndEpoch::decode_borrow(b, version)? }); Ok(true) }
289 _ => Ok(false),
290 }
291 })?;
292 if let Some(v) = tag_current_leader { out.current_leader = v; }
293 }
294 Ok(out)
295 }
296}
297
298#[derive(Debug, Clone, PartialEq, Eq)]
299pub struct BatchIndexAndErrorMessage<'a> {
300 pub batch_index: i32,
301 pub batch_index_error_message: Option<&'a str>,
302 pub unknown_tagged_fields: UnknownTaggedFields,
303}
304
305impl<'a> Default for BatchIndexAndErrorMessage<'a> {
306 fn default() -> Self {
307 Self {
308 batch_index: 0i32,
309 batch_index_error_message: None,
310 unknown_tagged_fields: Default::default(),
311 }
312 }
313}
314
315impl<'a> BatchIndexAndErrorMessage<'a> {
316 pub fn to_owned(&self) -> crate::owned::produce_response::BatchIndexAndErrorMessage {
317 crate::owned::produce_response::BatchIndexAndErrorMessage {
318 batch_index: (self.batch_index),
319 batch_index_error_message: (self.batch_index_error_message).map(|s| s.to_string()),
320 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
321 }
322 }
323}
324
325impl<'a> Encode for BatchIndexAndErrorMessage<'a> {
326 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
327 let flex = version >= 9;
328 if version >= 8 { put_i32(buf, self.batch_index) }
329 if version >= 8 { if flex { put_compact_nullable_string(buf, self.batch_index_error_message) } else { put_nullable_string(buf, self.batch_index_error_message) } }
330 if flex {
331 let tagged = WriteTaggedFields::new();
332 tagged.write(buf, &self.unknown_tagged_fields);
333 }
334 Ok(())
335 }
336 fn encoded_len(&self, version: i16) -> usize {
337 let flex = version >= 9;
338 let mut n: usize = 0;
339 if version >= 8 { n += 4; }
340 if version >= 8 { n += if flex { compact_nullable_string_len(self.batch_index_error_message) } else { nullable_string_len(self.batch_index_error_message) }; }
341 if flex {
342 let known_pairs: Vec<(u32, usize)> = Vec::new();
343 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
344 }
345 n
346 }
347}
348
349impl<'de> DecodeBorrow<'de> for BatchIndexAndErrorMessage<'de> {
350 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
351 let flex = version >= 9;
352 let mut out = Self::default();
353 if version >= 8 { out.batch_index = get_i32(buf)?; }
354 if version >= 8 { out.batch_index_error_message = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
355 if flex {
356 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
357 Ok(false)
358 })?;
359 }
360 Ok(out)
361 }
362}
363
364#[derive(Debug, Clone, PartialEq, Eq)]
365pub struct LeaderIdAndEpoch {
366 pub leader_id: i32,
367 pub leader_epoch: i32,
368 pub unknown_tagged_fields: UnknownTaggedFields,
369}
370
371impl Default for LeaderIdAndEpoch {
372 fn default() -> Self {
373 Self {
374 leader_id: -1i32,
375 leader_epoch: -1i32,
376 unknown_tagged_fields: Default::default(),
377 }
378 }
379}
380
381impl LeaderIdAndEpoch {
382 pub fn to_owned(&self) -> crate::owned::produce_response::LeaderIdAndEpoch {
383 crate::owned::produce_response::LeaderIdAndEpoch {
384 leader_id: (self.leader_id),
385 leader_epoch: (self.leader_epoch),
386 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
387 }
388 }
389}
390
391impl Encode for LeaderIdAndEpoch {
392 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
393 let flex = version >= 9;
394 if version >= 10 { put_i32(buf, self.leader_id) }
395 if version >= 10 { put_i32(buf, self.leader_epoch) }
396 if flex {
397 let tagged = WriteTaggedFields::new();
398 tagged.write(buf, &self.unknown_tagged_fields);
399 }
400 Ok(())
401 }
402 fn encoded_len(&self, version: i16) -> usize {
403 let flex = version >= 9;
404 let mut n: usize = 0;
405 if version >= 10 { n += 4; }
406 if version >= 10 { n += 4; }
407 if flex {
408 let known_pairs: Vec<(u32, usize)> = Vec::new();
409 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
410 }
411 n
412 }
413}
414
415impl<'de> DecodeBorrow<'de> for LeaderIdAndEpoch {
416 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
417 let flex = version >= 9;
418 let mut out = Self::default();
419 if version >= 10 { out.leader_id = get_i32(buf)?; }
420 if version >= 10 { out.leader_epoch = get_i32(buf)?; }
421 if flex {
422 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
423 Ok(false)
424 })?;
425 }
426 Ok(out)
427 }
428}
429
430#[derive(Debug, Clone, PartialEq, Eq)]
431pub struct NodeEndpoint<'a> {
432 pub node_id: i32,
433 pub host: &'a str,
434 pub port: i32,
435 pub rack: Option<&'a str>,
436 pub unknown_tagged_fields: UnknownTaggedFields,
437}
438
439impl<'a> Default for NodeEndpoint<'a> {
440 fn default() -> Self {
441 Self {
442 node_id: 0i32,
443 host: "",
444 port: 0i32,
445 rack: None,
446 unknown_tagged_fields: Default::default(),
447 }
448 }
449}
450
451impl<'a> NodeEndpoint<'a> {
452 pub fn to_owned(&self) -> crate::owned::produce_response::NodeEndpoint {
453 crate::owned::produce_response::NodeEndpoint {
454 node_id: (self.node_id),
455 host: (self.host).to_string(),
456 port: (self.port),
457 rack: (self.rack).map(|s| s.to_string()),
458 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
459 }
460 }
461}
462
463impl<'a> Encode for NodeEndpoint<'a> {
464 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
465 let flex = version >= 9;
466 if version >= 10 { put_i32(buf, self.node_id) }
467 if version >= 10 { if flex { put_compact_string(buf, self.host) } else { put_string(buf, self.host) } }
468 if version >= 10 { put_i32(buf, self.port) }
469 if version >= 10 { if flex { put_compact_nullable_string(buf, self.rack) } else { put_nullable_string(buf, self.rack) } }
470 if flex {
471 let tagged = WriteTaggedFields::new();
472 tagged.write(buf, &self.unknown_tagged_fields);
473 }
474 Ok(())
475 }
476 fn encoded_len(&self, version: i16) -> usize {
477 let flex = version >= 9;
478 let mut n: usize = 0;
479 if version >= 10 { n += 4; }
480 if version >= 10 { n += if flex { compact_string_len(self.host) } else { string_len(self.host) }; }
481 if version >= 10 { n += 4; }
482 if version >= 10 { n += if flex { compact_nullable_string_len(self.rack) } else { nullable_string_len(self.rack) }; }
483 if flex {
484 let known_pairs: Vec<(u32, usize)> = Vec::new();
485 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
486 }
487 n
488 }
489}
490
491impl<'de> DecodeBorrow<'de> for NodeEndpoint<'de> {
492 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
493 let flex = version >= 9;
494 let mut out = Self::default();
495 if version >= 10 { out.node_id = get_i32(buf)?; }
496 if version >= 10 { out.host = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
497 if version >= 10 { out.port = get_i32(buf)?; }
498 if version >= 10 { out.rack = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
499 if flex {
500 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
501 Ok(false)
502 })?;
503 }
504 Ok(out)
505 }
506}