crabka-protocol 0.3.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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
// 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::{
    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::primitives::string_bytes_borrowed::{
    get_compact_nullable_string_borrowed, get_compact_string_borrowed,
    get_nullable_string_borrowed, get_string_borrowed,
};
use crate::tagged_fields::{
    WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len,
};
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, Default)]
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 FetchResponse<'_> {
    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(FetchableTopicResponse::to_owned)
                .collect(),
            node_endpoints: self.node_endpoints.clone(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for FetchResponse<'_> {
    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 {
            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)
    }
}
#[cfg(test)]
impl FetchResponse<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 1 {
            m.throttle_time_ms = 1i32;
        }
        if version >= 7 {
            m.error_code = 1i16;
        }
        if version >= 7 {
            m.session_id = 1i32;
        }
        if version >= 0 {
            m.responses = vec![FetchableTopicResponse::populated(version)];
        }
        if version >= 16 {
            m.node_endpoints = vec![crate::owned::fetch_response::NodeEndpoint::populated(
                version,
            )];
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
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 FetchableTopicResponse<'_> {
    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(PartitionData::to_owned)
                .collect(),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for FetchableTopicResponse<'_> {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        let flex = version >= 12;
        if (0..=12).contains(&version) {
            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 (0..=12).contains(&version) {
            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 (0..=12).contains(&version) {
            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)
    }
}
#[cfg(test)]
impl FetchableTopicResponse<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if (0..=12).contains(&version) {
            m.topic = "x";
        }
        if version >= 13 {
            m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
        }
        if version >= 0 {
            m.partitions = vec![PartitionData::populated(version)];
        }
        m
    }
}
#[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 Default for PartitionData<'_> {
    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 PartitionData<'_> {
    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(AbortedTransaction::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 Encode for PartitionData<'_> {
    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(std::vec::Vec::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 {
            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)
    }
}
#[cfg(test)]
impl PartitionData<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.partition_index = 1i32;
        }
        if version >= 0 {
            m.error_code = 1i16;
        }
        if version >= 0 {
            m.high_watermark = 1i64;
        }
        if version >= 4 {
            m.last_stable_offset = 1i64;
        }
        if version >= 5 {
            m.log_start_offset = 1i64;
        }
        if version >= 4 {
            m.aborted_transactions = Some(vec![AbortedTransaction::populated(version)]);
        }
        if version >= 11 {
            m.preferred_read_replica = 1i32;
        }
        if version >= 12 {
            m.diverging_epoch = EpochEndOffset::populated(version);
        }
        if version >= 12 {
            m.current_leader = LeaderIdAndEpoch::populated(version);
        }
        if version >= 12 {
            m.snapshot_id = SnapshotId::populated(version);
        }
        m
    }
}
#[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)
    }
}
#[cfg(test)]
impl EpochEndOffset {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 12 {
            m.epoch = 1i32;
        }
        if version >= 12 {
            m.end_offset = 1i64;
        }
        m
    }
}
#[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)
    }
}
#[cfg(test)]
impl LeaderIdAndEpoch {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 12 {
            m.leader_id = 1i32;
        }
        if version >= 12 {
            m.leader_epoch = 1i32;
        }
        m
    }
}
#[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)
    }
}
#[cfg(test)]
impl SnapshotId {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 0 {
            m.end_offset = 1i64;
        }
        if version >= 0 {
            m.epoch = 1i32;
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct AbortedTransaction {
    pub producer_id: i64,
    pub first_offset: i64,
    pub unknown_tagged_fields: UnknownTaggedFields,
}
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)
    }
}
#[cfg(test)]
impl AbortedTransaction {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 4 {
            m.producer_id = 1i64;
        }
        if version >= 4 {
            m.first_offset = 1i64;
        }
        m
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
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 NodeEndpoint<'_> {
    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(std::string::ToString::to_string),
            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
        }
    }
}
impl Encode for NodeEndpoint<'_> {
    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)
    }
}
#[cfg(test)]
impl NodeEndpoint<'_> {
    #[must_use]
    pub fn populated(version: i16) -> Self {
        let mut m = Self::default();
        if version >= 16 {
            m.node_id = 1i32;
        }
        if version >= 16 {
            m.host = "x";
        }
        if version >= 16 {
            m.port = 1i32;
        }
        if version >= 16 {
            m.rack = Some("x");
        }
        m
    }
}