crabka-protocol 0.1.2

Apache Kafka wire-protocol codec (4.3.0), with typed RecordBatch and zero-copy borrowed decode
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.

use bytes::BufMut;

use crate::primitives::fixed::{get_i16, get_i32, get_i64, put_i16, put_i32, put_i64};
use crate::primitives::string_bytes::{
    compact_nullable_string_len, compact_string_len, nullable_string_len,
    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
    string_len,
};
use crate::primitives::string_bytes_borrowed::{
    get_compact_nullable_string_borrowed, get_compact_string_borrowed,
    get_nullable_string_borrowed, get_string_borrowed,
};
use crate::primitives::string_bytes::{put_bytes, put_compact_bytes, put_compact_nullable_bytes, put_nullable_bytes};
use crate::primitives::string_bytes_borrowed::{get_compact_nullable_bytes_borrowed, get_nullable_bytes_borrowed};
use crate::tagged_fields::{encode_to_bytes, read_tagged_fields, tagged_fields_len, WriteTaggedFields};
use crate::{Decode, DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};

pub const API_KEY: i16 = 1;
pub const MIN_VERSION: i16 = 4;
pub const MAX_VERSION: i16 = 18;
pub const FLEXIBLE_MIN: i16 = 12;

#[inline]
fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FetchResponse<'a> {
    pub throttle_time_ms: i32,
    pub error_code: i16,
    pub session_id: i32,
    pub responses: Vec<FetchableTopicResponse<'a>>,
    pub node_endpoints: Vec<crate::owned::fetch_response::NodeEndpoint>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for FetchResponse<'a> {
    fn default() -> Self {
        Self {
            throttle_time_ms: 0i32,
            error_code: 0i16,
            session_id: 0i32,
            responses: Vec::new(),
            node_endpoints: Vec::new(),
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> FetchResponse<'a> {
    pub fn to_owned(&self) -> crate::owned::fetch_response::FetchResponse {
        crate::owned::fetch_response::FetchResponse {
            throttle_time_ms: (self.throttle_time_ms),
            error_code: (self.error_code),
            session_id: (self.session_id),
            responses: (self.responses).iter().map(|it| it.to_owned()).collect(),
            node_endpoints: self.node_endpoints.clone(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for FetchResponse<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
        }
        let flex = is_flexible(version);
        if version >= 1 { put_i32(buf, self.throttle_time_ms) }
        if version >= 7 { put_i16(buf, self.error_code) }
        if version >= 7 { put_i32(buf, self.session_id) }
        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.responses).len(), flex); for it in &self.responses { it.encode(buf, version)?; } } }
        if flex {
            let mut tagged = WriteTaggedFields::new();
            if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
                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(()) });
                tagged.add(0, payload);
            }
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = is_flexible(version);
        let mut n: usize = 0;
        if version >= 1 { n += 4; }
        if version >= 7 { n += 2; }
        if version >= 7 { n += 4; }
        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 }; }
        if flex {
            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
            if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
                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 }));
            }
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}

impl<'de> DecodeBorrow<'de> for FetchResponse<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
        }
        let flex = is_flexible(version);
        let mut out = Self::default();
        if version >= 1 { out.throttle_time_ms = get_i32(buf)?; }
        if version >= 7 { out.error_code = get_i16(buf)?; }
        if version >= 7 { out.session_id = get_i32(buf)?; }
        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 }; }
        if flex {
            // Pre-declare typed slots for known tagged fields.
            let mut tag_node_endpoints = None;
            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
                match tag {
                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) }
                    _ => Ok(false),
                }
            })?;
            if let Some(v) = tag_node_endpoints { out.node_endpoints = v; }
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FetchableTopicResponse<'a> {
    pub topic: &'a str,
    pub topic_id: crate::primitives::uuid::Uuid,
    pub partitions: Vec<PartitionData<'a>>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for FetchableTopicResponse<'a> {
    fn default() -> Self {
        Self {
            topic: "",
            topic_id: Default::default(),
            partitions: Vec::new(),
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> FetchableTopicResponse<'a> {
    pub fn to_owned(&self) -> crate::owned::fetch_response::FetchableTopicResponse {
        crate::owned::fetch_response::FetchableTopicResponse {
            topic: (self.topic).to_string(),
            topic_id: (self.topic_id),
            partitions: (self.partitions).iter().map(|it| it.to_owned()).collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for FetchableTopicResponse<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 12;
        if version >= 0 && version <= 12 { if flex { put_compact_string(buf, self.topic) } else { put_string(buf, self.topic) } }
        if version >= 13 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 12;
        let mut n: usize = 0;
        if version >= 0 && version <= 12 { n += if flex { compact_string_len(self.topic) } else { string_len(self.topic) }; }
        if version >= 13 { n += 16; }
        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 }; }
        if flex {
            let known_pairs: Vec<(u32, usize)> = Vec::new();
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}

impl<'de> DecodeBorrow<'de> for FetchableTopicResponse<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 12;
        let mut out = Self::default();
        if version >= 0 && version <= 12 { out.topic = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
        if version >= 13 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
        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 }; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PartitionData<'a> {
    pub partition_index: i32,
    pub error_code: i16,
    pub high_watermark: i64,
    pub last_stable_offset: i64,
    pub log_start_offset: i64,
    pub aborted_transactions: Option<Vec<AbortedTransaction>>,
    pub preferred_read_replica: i32,
    pub records: Option<crate::records::RecordsPayloadBorrowed<'a>>,
    pub diverging_epoch: EpochEndOffset,
    pub current_leader: LeaderIdAndEpoch,
    pub snapshot_id: SnapshotId,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for PartitionData<'a> {
    fn default() -> Self {
        Self {
            partition_index: 0i32,
            error_code: 0i16,
            high_watermark: 0i64,
            last_stable_offset: -1i64,
            log_start_offset: -1i64,
            aborted_transactions: None,
            preferred_read_replica: -1i32,
            records: None,
            diverging_epoch: Default::default(),
            current_leader: Default::default(),
            snapshot_id: Default::default(),
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> PartitionData<'a> {
    pub fn to_owned(&self) -> crate::owned::fetch_response::PartitionData {
        crate::owned::fetch_response::PartitionData {
            partition_index: (self.partition_index),
            error_code: (self.error_code),
            high_watermark: (self.high_watermark),
            last_stable_offset: (self.last_stable_offset),
            log_start_offset: (self.log_start_offset),
            aborted_transactions: (self.aborted_transactions).as_ref().map(|v| v.iter().map(|it| it.to_owned()).collect()),
            preferred_read_replica: (self.preferred_read_replica),
            records: (self.records).as_ref().map(|rb| rb.to_owned().expect("records to_owned")),
            diverging_epoch: (self.diverging_epoch).to_owned(),
            current_leader: (self.current_leader).to_owned(),
            snapshot_id: (self.snapshot_id).to_owned(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for PartitionData<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 12;
        if version >= 0 { put_i32(buf, self.partition_index) }
        if version >= 0 { put_i16(buf, self.error_code) }
        if version >= 0 { put_i64(buf, self.high_watermark) }
        if version >= 4 { put_i64(buf, self.last_stable_offset) }
        if version >= 5 { put_i64(buf, self.log_start_offset) }
        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)?; } } } }
        if version >= 11 { put_i32(buf, self.preferred_read_replica) }
        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) } } } }
        if flex {
            let mut tagged = WriteTaggedFields::new();
            if !(crate::codegen_helpers::is_default(&self.diverging_epoch)) {
                let payload = encode_to_bytes(self.diverging_epoch.encoded_len(version), |b| { self.diverging_epoch.encode(b, version)?; Ok(()) });
                tagged.add(0, payload);
            }
            if !(crate::codegen_helpers::is_default(&self.current_leader)) {
                let payload = encode_to_bytes(self.current_leader.encoded_len(version), |b| { self.current_leader.encode(b, version)?; Ok(()) });
                tagged.add(1, payload);
            }
            if !(crate::codegen_helpers::is_default(&self.snapshot_id)) {
                let payload = encode_to_bytes(self.snapshot_id.encoded_len(version), |b| { self.snapshot_id.encode(b, version)?; Ok(()) });
                tagged.add(2, payload);
            }
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 12;
        let mut n: usize = 0;
        if version >= 0 { n += 4; }
        if version >= 0 { n += 2; }
        if version >= 0 { n += 8; }
        if version >= 4 { n += 8; }
        if version >= 5 { n += 8; }
        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 }; }
        if version >= 11 { n += 4; }
        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 } } }; }
        if flex {
            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
            if !(crate::codegen_helpers::is_default(&self.diverging_epoch)) {
                known_pairs.push((0, self.diverging_epoch.encoded_len(version)));
            }
            if !(crate::codegen_helpers::is_default(&self.current_leader)) {
                known_pairs.push((1, self.current_leader.encoded_len(version)));
            }
            if !(crate::codegen_helpers::is_default(&self.snapshot_id)) {
                known_pairs.push((2, self.snapshot_id.encoded_len(version)));
            }
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}

impl<'de> DecodeBorrow<'de> for PartitionData<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 12;
        let mut out = Self::default();
        if version >= 0 { out.partition_index = get_i32(buf)?; }
        if version >= 0 { out.error_code = get_i16(buf)?; }
        if version >= 0 { out.high_watermark = get_i64(buf)?; }
        if version >= 4 { out.last_stable_offset = get_i64(buf)?; }
        if version >= 5 { out.log_start_offset = get_i64(buf)?; }
        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) } } }; }
        if version >= 11 { out.preferred_read_replica = get_i32(buf)?; }
        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)?) } } }; }
        if flex {
            // Pre-declare typed slots for known tagged fields.
            let mut tag_diverging_epoch = None;
            let mut tag_current_leader = None;
            let mut tag_snapshot_id = None;
            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
                match tag {
                0 => { tag_diverging_epoch = Some({ let b: &mut &[u8] = payload; EpochEndOffset::decode_borrow(b, version)? }); Ok(true) }
                1 => { tag_current_leader = Some({ let b: &mut &[u8] = payload; LeaderIdAndEpoch::decode_borrow(b, version)? }); Ok(true) }
                2 => { tag_snapshot_id = Some({ let b: &mut &[u8] = payload; SnapshotId::decode_borrow(b, version)? }); Ok(true) }
                    _ => Ok(false),
                }
            })?;
            if let Some(v) = tag_diverging_epoch { out.diverging_epoch = v; }
            if let Some(v) = tag_current_leader { out.current_leader = v; }
            if let Some(v) = tag_snapshot_id { out.snapshot_id = v; }
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EpochEndOffset {
    pub epoch: i32,
    pub end_offset: i64,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Default for EpochEndOffset {
    fn default() -> Self {
        Self {
            epoch: -1i32,
            end_offset: -1i64,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl EpochEndOffset {
    pub fn to_owned(&self) -> crate::owned::fetch_response::EpochEndOffset {
        crate::owned::fetch_response::EpochEndOffset {
            epoch: (self.epoch),
            end_offset: (self.end_offset),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl Encode for EpochEndOffset {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 12;
        if version >= 12 { put_i32(buf, self.epoch) }
        if version >= 12 { put_i64(buf, self.end_offset) }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 12;
        let mut n: usize = 0;
        if version >= 12 { n += 4; }
        if version >= 12 { n += 8; }
        if flex {
            let known_pairs: Vec<(u32, usize)> = Vec::new();
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}

impl<'de> DecodeBorrow<'de> for EpochEndOffset {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 12;
        let mut out = Self::default();
        if version >= 12 { out.epoch = get_i32(buf)?; }
        if version >= 12 { out.end_offset = get_i64(buf)?; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LeaderIdAndEpoch {
    pub leader_id: i32,
    pub leader_epoch: i32,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Default for LeaderIdAndEpoch {
    fn default() -> Self {
        Self {
            leader_id: -1i32,
            leader_epoch: -1i32,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl LeaderIdAndEpoch {
    pub fn to_owned(&self) -> crate::owned::fetch_response::LeaderIdAndEpoch {
        crate::owned::fetch_response::LeaderIdAndEpoch {
            leader_id: (self.leader_id),
            leader_epoch: (self.leader_epoch),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl Encode for LeaderIdAndEpoch {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 12;
        if version >= 12 { put_i32(buf, self.leader_id) }
        if version >= 12 { put_i32(buf, self.leader_epoch) }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 12;
        let mut n: usize = 0;
        if version >= 12 { n += 4; }
        if version >= 12 { n += 4; }
        if flex {
            let known_pairs: Vec<(u32, usize)> = Vec::new();
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}

impl<'de> DecodeBorrow<'de> for LeaderIdAndEpoch {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 12;
        let mut out = Self::default();
        if version >= 12 { out.leader_id = get_i32(buf)?; }
        if version >= 12 { out.leader_epoch = get_i32(buf)?; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SnapshotId {
    pub end_offset: i64,
    pub epoch: i32,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Default for SnapshotId {
    fn default() -> Self {
        Self {
            end_offset: -1i64,
            epoch: -1i32,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl SnapshotId {
    pub fn to_owned(&self) -> crate::owned::fetch_response::SnapshotId {
        crate::owned::fetch_response::SnapshotId {
            end_offset: (self.end_offset),
            epoch: (self.epoch),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl Encode for SnapshotId {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 12;
        if version >= 0 { put_i64(buf, self.end_offset) }
        if version >= 0 { put_i32(buf, self.epoch) }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 12;
        let mut n: usize = 0;
        if version >= 0 { n += 8; }
        if version >= 0 { n += 4; }
        if flex {
            let known_pairs: Vec<(u32, usize)> = Vec::new();
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}

impl<'de> DecodeBorrow<'de> for SnapshotId {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 12;
        let mut out = Self::default();
        if version >= 0 { out.end_offset = get_i64(buf)?; }
        if version >= 0 { out.epoch = get_i32(buf)?; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AbortedTransaction {
    pub producer_id: i64,
    pub first_offset: i64,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Default for AbortedTransaction {
    fn default() -> Self {
        Self {
            producer_id: 0i64,
            first_offset: 0i64,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl AbortedTransaction {
    pub fn to_owned(&self) -> crate::owned::fetch_response::AbortedTransaction {
        crate::owned::fetch_response::AbortedTransaction {
            producer_id: (self.producer_id),
            first_offset: (self.first_offset),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl Encode for AbortedTransaction {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 12;
        if version >= 4 { put_i64(buf, self.producer_id) }
        if version >= 4 { put_i64(buf, self.first_offset) }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 12;
        let mut n: usize = 0;
        if version >= 4 { n += 8; }
        if version >= 4 { n += 8; }
        if flex {
            let known_pairs: Vec<(u32, usize)> = Vec::new();
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}

impl<'de> DecodeBorrow<'de> for AbortedTransaction {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 12;
        let mut out = Self::default();
        if version >= 4 { out.producer_id = get_i64(buf)?; }
        if version >= 4 { out.first_offset = get_i64(buf)?; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NodeEndpoint<'a> {
    pub node_id: i32,
    pub host: &'a str,
    pub port: i32,
    pub rack: Option<&'a str>,
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl<'a> Default for NodeEndpoint<'a> {
    fn default() -> Self {
        Self {
            node_id: 0i32,
            host: "",
            port: 0i32,
            rack: None,
            unknown_tagged_fields: Default::default(),
        }
    }
}

impl<'a> NodeEndpoint<'a> {
    pub fn to_owned(&self) -> crate::owned::fetch_response::NodeEndpoint {
        crate::owned::fetch_response::NodeEndpoint {
            node_id: (self.node_id),
            host: (self.host).to_string(),
            port: (self.port),
            rack: (self.rack).map(|s| s.to_string()),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}

impl<'a> Encode for NodeEndpoint<'a> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 12;
        if version >= 16 { put_i32(buf, self.node_id) }
        if version >= 16 { if flex { put_compact_string(buf, self.host) } else { put_string(buf, self.host) } }
        if version >= 16 { put_i32(buf, self.port) }
        if version >= 16 { if flex { put_compact_nullable_string(buf, self.rack) } else { put_nullable_string(buf, self.rack) } }
        if flex {
            let tagged = WriteTaggedFields::new();
            tagged.write(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }
    fn encoded_len(&self, version: i16) -> usize {
        let flex = version >= 12;
        let mut n: usize = 0;
        if version >= 16 { n += 4; }
        if version >= 16 { n += if flex { compact_string_len(self.host) } else { string_len(self.host) }; }
        if version >= 16 { n += 4; }
        if version >= 16 { n += if flex { compact_nullable_string_len(self.rack) } else { nullable_string_len(self.rack) }; }
        if flex {
            let known_pairs: Vec<(u32, usize)> = Vec::new();
            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
        }
        n
    }
}

impl<'de> DecodeBorrow<'de> for NodeEndpoint<'de> {
    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
        let flex = version >= 12;
        let mut out = Self::default();
        if version >= 16 { out.node_id = get_i32(buf)?; }
        if version >= 16 { out.host = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
        if version >= 16 { out.port = get_i32(buf)?; }
        if version >= 16 { out.rack = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
        if flex {
            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
                Ok(false)
            })?;
        }
        Ok(out)
    }
}