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::primitives::string_bytes::{put_bytes, put_compact_bytes, put_compact_nullable_bytes, put_nullable_bytes};
16use crate::primitives::string_bytes_borrowed::{get_compact_nullable_bytes_borrowed, get_nullable_bytes_borrowed};
17use crate::tagged_fields::{encode_to_bytes, read_tagged_fields, tagged_fields_len, WriteTaggedFields};
18use crate::{Decode, DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
19
20pub const API_KEY: i16 = 1;
21pub const MIN_VERSION: i16 = 4;
22pub const MAX_VERSION: i16 = 18;
23pub const FLEXIBLE_MIN: i16 = 12;
24
25#[inline]
26fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
27
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct FetchResponse<'a> {
30 pub throttle_time_ms: i32,
31 pub error_code: i16,
32 pub session_id: i32,
33 pub responses: Vec<FetchableTopicResponse<'a>>,
34 pub node_endpoints: Vec<crate::owned::fetch_response::NodeEndpoint>,
35 pub unknown_tagged_fields: UnknownTaggedFields,
36}
37
38impl<'a> Default for FetchResponse<'a> {
39 fn default() -> Self {
40 Self {
41 throttle_time_ms: 0i32,
42 error_code: 0i16,
43 session_id: 0i32,
44 responses: Vec::new(),
45 node_endpoints: Vec::new(),
46 unknown_tagged_fields: Default::default(),
47 }
48 }
49}
50
51impl<'a> FetchResponse<'a> {
52 pub fn to_owned(&self) -> crate::owned::fetch_response::FetchResponse {
53 crate::owned::fetch_response::FetchResponse {
54 throttle_time_ms: (self.throttle_time_ms),
55 error_code: (self.error_code),
56 session_id: (self.session_id),
57 responses: (self.responses).iter().map(|it| it.to_owned()).collect(),
58 node_endpoints: self.node_endpoints.clone(),
59 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
60 }
61 }
62}
63
64impl<'a> Encode for FetchResponse<'a> {
65 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
66 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
67 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
68 }
69 let flex = is_flexible(version);
70 if version >= 1 { put_i32(buf, self.throttle_time_ms) }
71 if version >= 7 { put_i16(buf, self.error_code) }
72 if version >= 7 { put_i32(buf, self.session_id) }
73 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.responses).len(), flex); for it in &self.responses { it.encode(buf, version)?; } } }
74 if flex {
75 let mut tagged = WriteTaggedFields::new();
76 if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
77 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(()) });
78 tagged.add(0, payload);
79 }
80 tagged.write(buf, &self.unknown_tagged_fields);
81 }
82 Ok(())
83 }
84 fn encoded_len(&self, version: i16) -> usize {
85 let flex = is_flexible(version);
86 let mut n: usize = 0;
87 if version >= 1 { n += 4; }
88 if version >= 7 { n += 2; }
89 if version >= 7 { n += 4; }
90 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 }; }
91 if flex {
92 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
93 if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
94 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 }));
95 }
96 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
97 }
98 n
99 }
100}
101
102impl<'de> DecodeBorrow<'de> for FetchResponse<'de> {
103 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
104 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
105 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
106 }
107 let flex = is_flexible(version);
108 let mut out = Self::default();
109 if version >= 1 { out.throttle_time_ms = get_i32(buf)?; }
110 if version >= 7 { out.error_code = get_i16(buf)?; }
111 if version >= 7 { out.session_id = get_i32(buf)?; }
112 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(FetchableTopicResponse::decode_borrow(buf, version)?); } v }; }
113 if flex {
114 let mut tag_node_endpoints = None;
116 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
117 match tag {
118 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::fetch_response::NodeEndpoint::decode(b, version)?); } v } }); Ok(true) }
119 _ => Ok(false),
120 }
121 })?;
122 if let Some(v) = tag_node_endpoints { out.node_endpoints = v; }
123 }
124 Ok(out)
125 }
126}
127
128#[derive(Debug, Clone, PartialEq, Eq)]
129pub struct FetchableTopicResponse<'a> {
130 pub topic: &'a str,
131 pub topic_id: crate::primitives::uuid::Uuid,
132 pub partitions: Vec<PartitionData<'a>>,
133 pub unknown_tagged_fields: UnknownTaggedFields,
134}
135
136impl<'a> Default for FetchableTopicResponse<'a> {
137 fn default() -> Self {
138 Self {
139 topic: "",
140 topic_id: Default::default(),
141 partitions: Vec::new(),
142 unknown_tagged_fields: Default::default(),
143 }
144 }
145}
146
147impl<'a> FetchableTopicResponse<'a> {
148 pub fn to_owned(&self) -> crate::owned::fetch_response::FetchableTopicResponse {
149 crate::owned::fetch_response::FetchableTopicResponse {
150 topic: (self.topic).to_string(),
151 topic_id: (self.topic_id),
152 partitions: (self.partitions).iter().map(|it| it.to_owned()).collect(),
153 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
154 }
155 }
156}
157
158impl<'a> Encode for FetchableTopicResponse<'a> {
159 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
160 let flex = version >= 12;
161 if version >= 0 && version <= 12 { if flex { put_compact_string(buf, self.topic) } else { put_string(buf, self.topic) } }
162 if version >= 13 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
163 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
164 if flex {
165 let tagged = WriteTaggedFields::new();
166 tagged.write(buf, &self.unknown_tagged_fields);
167 }
168 Ok(())
169 }
170 fn encoded_len(&self, version: i16) -> usize {
171 let flex = version >= 12;
172 let mut n: usize = 0;
173 if version >= 0 && version <= 12 { n += if flex { compact_string_len(self.topic) } else { string_len(self.topic) }; }
174 if version >= 13 { n += 16; }
175 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
176 if flex {
177 let known_pairs: Vec<(u32, usize)> = Vec::new();
178 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
179 }
180 n
181 }
182}
183
184impl<'de> DecodeBorrow<'de> for FetchableTopicResponse<'de> {
185 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
186 let flex = version >= 12;
187 let mut out = Self::default();
188 if version >= 0 && version <= 12 { out.topic = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
189 if version >= 13 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
190 if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(PartitionData::decode_borrow(buf, version)?); } v }; }
191 if flex {
192 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
193 Ok(false)
194 })?;
195 }
196 Ok(out)
197 }
198}
199
200#[derive(Debug, Clone, PartialEq, Eq)]
201pub struct PartitionData<'a> {
202 pub partition_index: i32,
203 pub error_code: i16,
204 pub high_watermark: i64,
205 pub last_stable_offset: i64,
206 pub log_start_offset: i64,
207 pub aborted_transactions: Option<Vec<AbortedTransaction>>,
208 pub preferred_read_replica: i32,
209 pub records: Option<crate::records::RecordsPayloadBorrowed<'a>>,
210 pub diverging_epoch: EpochEndOffset,
211 pub current_leader: LeaderIdAndEpoch,
212 pub snapshot_id: SnapshotId,
213 pub unknown_tagged_fields: UnknownTaggedFields,
214}
215
216impl<'a> Default for PartitionData<'a> {
217 fn default() -> Self {
218 Self {
219 partition_index: 0i32,
220 error_code: 0i16,
221 high_watermark: 0i64,
222 last_stable_offset: -1i64,
223 log_start_offset: -1i64,
224 aborted_transactions: None,
225 preferred_read_replica: -1i32,
226 records: None,
227 diverging_epoch: Default::default(),
228 current_leader: Default::default(),
229 snapshot_id: Default::default(),
230 unknown_tagged_fields: Default::default(),
231 }
232 }
233}
234
235impl<'a> PartitionData<'a> {
236 pub fn to_owned(&self) -> crate::owned::fetch_response::PartitionData {
237 crate::owned::fetch_response::PartitionData {
238 partition_index: (self.partition_index),
239 error_code: (self.error_code),
240 high_watermark: (self.high_watermark),
241 last_stable_offset: (self.last_stable_offset),
242 log_start_offset: (self.log_start_offset),
243 aborted_transactions: (self.aborted_transactions).as_ref().map(|v| v.iter().map(|it| it.to_owned()).collect()),
244 preferred_read_replica: (self.preferred_read_replica),
245 records: (self.records).as_ref().map(|rb| rb.to_owned().expect("records to_owned")),
246 diverging_epoch: (self.diverging_epoch).to_owned(),
247 current_leader: (self.current_leader).to_owned(),
248 snapshot_id: (self.snapshot_id).to_owned(),
249 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
250 }
251 }
252}
253
254impl<'a> Encode for PartitionData<'a> {
255 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
256 let flex = version >= 12;
257 if version >= 0 { put_i32(buf, self.partition_index) }
258 if version >= 0 { put_i16(buf, self.error_code) }
259 if version >= 0 { put_i64(buf, self.high_watermark) }
260 if version >= 4 { put_i64(buf, self.last_stable_offset) }
261 if version >= 5 { put_i64(buf, self.log_start_offset) }
262 if version >= 4 { { let len = (self.aborted_transactions).as_ref().map(Vec::len); crate::primitives::array::put_nullable_array_len(buf, len, flex); if let Some(v) = &self.aborted_transactions { for it in v { it.encode(buf, version)?; } } } }
263 if version >= 11 { put_i32(buf, self.preferred_read_replica) }
264 if version >= 0 { match &self.records { None => if flex { put_compact_nullable_bytes(buf, None) } else { put_nullable_bytes(buf, None) }, Some(__rb) => { let mut __rb_buf = bytes::BytesMut::new(); <crate::records::RecordsPayloadBorrowed as crate::Encode>::encode(__rb, &mut __rb_buf, version)?; if flex { put_compact_bytes(buf, &__rb_buf) } else { put_bytes(buf, &__rb_buf) } } } }
265 if flex {
266 let mut tagged = WriteTaggedFields::new();
267 if !(crate::codegen_helpers::is_default(&self.diverging_epoch)) {
268 let payload = encode_to_bytes(self.diverging_epoch.encoded_len(version), |b| { self.diverging_epoch.encode(b, version)?; Ok(()) });
269 tagged.add(0, payload);
270 }
271 if !(crate::codegen_helpers::is_default(&self.current_leader)) {
272 let payload = encode_to_bytes(self.current_leader.encoded_len(version), |b| { self.current_leader.encode(b, version)?; Ok(()) });
273 tagged.add(1, payload);
274 }
275 if !(crate::codegen_helpers::is_default(&self.snapshot_id)) {
276 let payload = encode_to_bytes(self.snapshot_id.encoded_len(version), |b| { self.snapshot_id.encode(b, version)?; Ok(()) });
277 tagged.add(2, payload);
278 }
279 tagged.write(buf, &self.unknown_tagged_fields);
280 }
281 Ok(())
282 }
283 fn encoded_len(&self, version: i16) -> usize {
284 let flex = version >= 12;
285 let mut n: usize = 0;
286 if version >= 0 { n += 4; }
287 if version >= 0 { n += 2; }
288 if version >= 0 { n += 8; }
289 if version >= 4 { n += 8; }
290 if version >= 5 { n += 8; }
291 if version >= 4 { n += { let opt: Option<&Vec<_>> = (self.aborted_transactions).as_ref(); let prefix = crate::primitives::array::nullable_array_len_prefix_len(opt.map(|v| v.len()), flex); let body: usize = opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum()); prefix + body }; }
292 if version >= 11 { n += 4; }
293 if version >= 0 { n += match &self.records { None => if flex { crate::primitives::varint::uvarint_len(0) } else { 4 }, Some(__rb) => { let __rb_len = <crate::records::RecordsPayloadBorrowed as crate::Encode>::encoded_len(__rb, version); if flex { crate::primitives::string_bytes::compact_bytes_len_from_size(__rb_len) } else { 4 + __rb_len } } }; }
294 if flex {
295 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
296 if !(crate::codegen_helpers::is_default(&self.diverging_epoch)) {
297 known_pairs.push((0, self.diverging_epoch.encoded_len(version)));
298 }
299 if !(crate::codegen_helpers::is_default(&self.current_leader)) {
300 known_pairs.push((1, self.current_leader.encoded_len(version)));
301 }
302 if !(crate::codegen_helpers::is_default(&self.snapshot_id)) {
303 known_pairs.push((2, self.snapshot_id.encoded_len(version)));
304 }
305 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
306 }
307 n
308 }
309}
310
311impl<'de> DecodeBorrow<'de> for PartitionData<'de> {
312 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
313 let flex = version >= 12;
314 let mut out = Self::default();
315 if version >= 0 { out.partition_index = get_i32(buf)?; }
316 if version >= 0 { out.error_code = get_i16(buf)?; }
317 if version >= 0 { out.high_watermark = get_i64(buf)?; }
318 if version >= 4 { out.last_stable_offset = get_i64(buf)?; }
319 if version >= 5 { out.log_start_offset = get_i64(buf)?; }
320 if version >= 4 { out.aborted_transactions = { let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?; match opt { None => None, Some(n) => { let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(AbortedTransaction::decode_borrow(buf, version)?); } Some(v) } } }; }
321 if version >= 11 { out.preferred_read_replica = get_i32(buf)?; }
322 if version >= 0 { out.records = { let __rb_opt = if flex { get_compact_nullable_bytes_borrowed(buf)? } else { get_nullable_bytes_borrowed(buf)? }; match __rb_opt { None => None, Some(__rb_slice) => { let mut __rb_cur = __rb_slice; Some(<crate::records::RecordsPayloadBorrowed as crate::DecodeBorrow>::decode_borrow(&mut __rb_cur, version)?) } } }; }
323 if flex {
324 let mut tag_diverging_epoch = None;
326 let mut tag_current_leader = None;
327 let mut tag_snapshot_id = None;
328 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
329 match tag {
330 0 => { tag_diverging_epoch = Some({ let b: &mut &[u8] = payload; EpochEndOffset::decode_borrow(b, version)? }); Ok(true) }
331 1 => { tag_current_leader = Some({ let b: &mut &[u8] = payload; LeaderIdAndEpoch::decode_borrow(b, version)? }); Ok(true) }
332 2 => { tag_snapshot_id = Some({ let b: &mut &[u8] = payload; SnapshotId::decode_borrow(b, version)? }); Ok(true) }
333 _ => Ok(false),
334 }
335 })?;
336 if let Some(v) = tag_diverging_epoch { out.diverging_epoch = v; }
337 if let Some(v) = tag_current_leader { out.current_leader = v; }
338 if let Some(v) = tag_snapshot_id { out.snapshot_id = v; }
339 }
340 Ok(out)
341 }
342}
343
344#[derive(Debug, Clone, PartialEq, Eq)]
345pub struct EpochEndOffset {
346 pub epoch: i32,
347 pub end_offset: i64,
348 pub unknown_tagged_fields: UnknownTaggedFields,
349}
350
351impl Default for EpochEndOffset {
352 fn default() -> Self {
353 Self {
354 epoch: -1i32,
355 end_offset: -1i64,
356 unknown_tagged_fields: Default::default(),
357 }
358 }
359}
360
361impl EpochEndOffset {
362 pub fn to_owned(&self) -> crate::owned::fetch_response::EpochEndOffset {
363 crate::owned::fetch_response::EpochEndOffset {
364 epoch: (self.epoch),
365 end_offset: (self.end_offset),
366 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
367 }
368 }
369}
370
371impl Encode for EpochEndOffset {
372 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
373 let flex = version >= 12;
374 if version >= 12 { put_i32(buf, self.epoch) }
375 if version >= 12 { put_i64(buf, self.end_offset) }
376 if flex {
377 let tagged = WriteTaggedFields::new();
378 tagged.write(buf, &self.unknown_tagged_fields);
379 }
380 Ok(())
381 }
382 fn encoded_len(&self, version: i16) -> usize {
383 let flex = version >= 12;
384 let mut n: usize = 0;
385 if version >= 12 { n += 4; }
386 if version >= 12 { n += 8; }
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}
394
395impl<'de> DecodeBorrow<'de> for EpochEndOffset {
396 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
397 let flex = version >= 12;
398 let mut out = Self::default();
399 if version >= 12 { out.epoch = get_i32(buf)?; }
400 if version >= 12 { out.end_offset = get_i64(buf)?; }
401 if flex {
402 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
403 Ok(false)
404 })?;
405 }
406 Ok(out)
407 }
408}
409
410#[derive(Debug, Clone, PartialEq, Eq)]
411pub struct LeaderIdAndEpoch {
412 pub leader_id: i32,
413 pub leader_epoch: i32,
414 pub unknown_tagged_fields: UnknownTaggedFields,
415}
416
417impl Default for LeaderIdAndEpoch {
418 fn default() -> Self {
419 Self {
420 leader_id: -1i32,
421 leader_epoch: -1i32,
422 unknown_tagged_fields: Default::default(),
423 }
424 }
425}
426
427impl LeaderIdAndEpoch {
428 pub fn to_owned(&self) -> crate::owned::fetch_response::LeaderIdAndEpoch {
429 crate::owned::fetch_response::LeaderIdAndEpoch {
430 leader_id: (self.leader_id),
431 leader_epoch: (self.leader_epoch),
432 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
433 }
434 }
435}
436
437impl Encode for LeaderIdAndEpoch {
438 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
439 let flex = version >= 12;
440 if version >= 12 { put_i32(buf, self.leader_id) }
441 if version >= 12 { put_i32(buf, self.leader_epoch) }
442 if flex {
443 let tagged = WriteTaggedFields::new();
444 tagged.write(buf, &self.unknown_tagged_fields);
445 }
446 Ok(())
447 }
448 fn encoded_len(&self, version: i16) -> usize {
449 let flex = version >= 12;
450 let mut n: usize = 0;
451 if version >= 12 { n += 4; }
452 if version >= 12 { n += 4; }
453 if flex {
454 let known_pairs: Vec<(u32, usize)> = Vec::new();
455 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
456 }
457 n
458 }
459}
460
461impl<'de> DecodeBorrow<'de> for LeaderIdAndEpoch {
462 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
463 let flex = version >= 12;
464 let mut out = Self::default();
465 if version >= 12 { out.leader_id = get_i32(buf)?; }
466 if version >= 12 { out.leader_epoch = get_i32(buf)?; }
467 if flex {
468 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
469 Ok(false)
470 })?;
471 }
472 Ok(out)
473 }
474}
475
476#[derive(Debug, Clone, PartialEq, Eq)]
477pub struct SnapshotId {
478 pub end_offset: i64,
479 pub epoch: i32,
480 pub unknown_tagged_fields: UnknownTaggedFields,
481}
482
483impl Default for SnapshotId {
484 fn default() -> Self {
485 Self {
486 end_offset: -1i64,
487 epoch: -1i32,
488 unknown_tagged_fields: Default::default(),
489 }
490 }
491}
492
493impl SnapshotId {
494 pub fn to_owned(&self) -> crate::owned::fetch_response::SnapshotId {
495 crate::owned::fetch_response::SnapshotId {
496 end_offset: (self.end_offset),
497 epoch: (self.epoch),
498 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
499 }
500 }
501}
502
503impl Encode for SnapshotId {
504 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
505 let flex = version >= 12;
506 if version >= 0 { put_i64(buf, self.end_offset) }
507 if version >= 0 { put_i32(buf, self.epoch) }
508 if flex {
509 let tagged = WriteTaggedFields::new();
510 tagged.write(buf, &self.unknown_tagged_fields);
511 }
512 Ok(())
513 }
514 fn encoded_len(&self, version: i16) -> usize {
515 let flex = version >= 12;
516 let mut n: usize = 0;
517 if version >= 0 { n += 8; }
518 if version >= 0 { n += 4; }
519 if flex {
520 let known_pairs: Vec<(u32, usize)> = Vec::new();
521 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
522 }
523 n
524 }
525}
526
527impl<'de> DecodeBorrow<'de> for SnapshotId {
528 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
529 let flex = version >= 12;
530 let mut out = Self::default();
531 if version >= 0 { out.end_offset = get_i64(buf)?; }
532 if version >= 0 { out.epoch = get_i32(buf)?; }
533 if flex {
534 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
535 Ok(false)
536 })?;
537 }
538 Ok(out)
539 }
540}
541
542#[derive(Debug, Clone, PartialEq, Eq)]
543pub struct AbortedTransaction {
544 pub producer_id: i64,
545 pub first_offset: i64,
546 pub unknown_tagged_fields: UnknownTaggedFields,
547}
548
549impl Default for AbortedTransaction {
550 fn default() -> Self {
551 Self {
552 producer_id: 0i64,
553 first_offset: 0i64,
554 unknown_tagged_fields: Default::default(),
555 }
556 }
557}
558
559impl AbortedTransaction {
560 pub fn to_owned(&self) -> crate::owned::fetch_response::AbortedTransaction {
561 crate::owned::fetch_response::AbortedTransaction {
562 producer_id: (self.producer_id),
563 first_offset: (self.first_offset),
564 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
565 }
566 }
567}
568
569impl Encode for AbortedTransaction {
570 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
571 let flex = version >= 12;
572 if version >= 4 { put_i64(buf, self.producer_id) }
573 if version >= 4 { put_i64(buf, self.first_offset) }
574 if flex {
575 let tagged = WriteTaggedFields::new();
576 tagged.write(buf, &self.unknown_tagged_fields);
577 }
578 Ok(())
579 }
580 fn encoded_len(&self, version: i16) -> usize {
581 let flex = version >= 12;
582 let mut n: usize = 0;
583 if version >= 4 { n += 8; }
584 if version >= 4 { n += 8; }
585 if flex {
586 let known_pairs: Vec<(u32, usize)> = Vec::new();
587 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
588 }
589 n
590 }
591}
592
593impl<'de> DecodeBorrow<'de> for AbortedTransaction {
594 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
595 let flex = version >= 12;
596 let mut out = Self::default();
597 if version >= 4 { out.producer_id = get_i64(buf)?; }
598 if version >= 4 { out.first_offset = get_i64(buf)?; }
599 if flex {
600 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
601 Ok(false)
602 })?;
603 }
604 Ok(out)
605 }
606}
607
608#[derive(Debug, Clone, PartialEq, Eq)]
609pub struct NodeEndpoint<'a> {
610 pub node_id: i32,
611 pub host: &'a str,
612 pub port: i32,
613 pub rack: Option<&'a str>,
614 pub unknown_tagged_fields: UnknownTaggedFields,
615}
616
617impl<'a> Default for NodeEndpoint<'a> {
618 fn default() -> Self {
619 Self {
620 node_id: 0i32,
621 host: "",
622 port: 0i32,
623 rack: None,
624 unknown_tagged_fields: Default::default(),
625 }
626 }
627}
628
629impl<'a> NodeEndpoint<'a> {
630 pub fn to_owned(&self) -> crate::owned::fetch_response::NodeEndpoint {
631 crate::owned::fetch_response::NodeEndpoint {
632 node_id: (self.node_id),
633 host: (self.host).to_string(),
634 port: (self.port),
635 rack: (self.rack).map(|s| s.to_string()),
636 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
637 }
638 }
639}
640
641impl<'a> Encode for NodeEndpoint<'a> {
642 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
643 let flex = version >= 12;
644 if version >= 16 { put_i32(buf, self.node_id) }
645 if version >= 16 { if flex { put_compact_string(buf, self.host) } else { put_string(buf, self.host) } }
646 if version >= 16 { put_i32(buf, self.port) }
647 if version >= 16 { if flex { put_compact_nullable_string(buf, self.rack) } else { put_nullable_string(buf, self.rack) } }
648 if flex {
649 let tagged = WriteTaggedFields::new();
650 tagged.write(buf, &self.unknown_tagged_fields);
651 }
652 Ok(())
653 }
654 fn encoded_len(&self, version: i16) -> usize {
655 let flex = version >= 12;
656 let mut n: usize = 0;
657 if version >= 16 { n += 4; }
658 if version >= 16 { n += if flex { compact_string_len(self.host) } else { string_len(self.host) }; }
659 if version >= 16 { n += 4; }
660 if version >= 16 { n += if flex { compact_nullable_string_len(self.rack) } else { nullable_string_len(self.rack) }; }
661 if flex {
662 let known_pairs: Vec<(u32, usize)> = Vec::new();
663 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
664 }
665 n
666 }
667}
668
669impl<'de> DecodeBorrow<'de> for NodeEndpoint<'de> {
670 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
671 let flex = version >= 12;
672 let mut out = Self::default();
673 if version >= 16 { out.node_id = get_i32(buf)?; }
674 if version >= 16 { out.host = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
675 if version >= 16 { out.port = get_i32(buf)?; }
676 if version >= 16 { out.rack = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
677 if flex {
678 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
679 Ok(false)
680 })?;
681 }
682 Ok(out)
683 }
684}