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
use super::{
    codec::{FixedLength, Read, Write},
    errors::Result,
};
use tokio::io::AsyncWrite;

#[derive(Debug, Clone, Copy)]
pub enum ApiKey {
    Produce,
    Fetch,
    ListOffsets,
    Metadata,
    LeaderAndIsr,
    StopReplica,
    UpdateMetadata,
    ControlledShutdown,
    OffsetCommit,
    OffsetFetch,
    FindCoordinator,
    JoinGroup,
    Heartbeat,
    LeaveGroup,
    SyncGroup,
    DescribeGroups,
    ListGroups,
    SaslHandshake,
    ApiVersions,
    CreateTopics,
    DeleteTopics,
    DeleteRecords,
    InitProducerId,
    OffsetForLeaderEpoch,
    AddPartitionsToTxn,
    AddOffsetsToTxn,
    EndTxn,
    WriteTxnMarkers,
    TxnOffsetCommit,
    DescribeAcls,
    CreateAcls,
    DeleteAcls,
    DescribeConfigs,
    AlterConfigs,
    AlterReplicaLogDirs,
    DescribeLogDirs,
    SaslAuthenticate,
    CreatePartitions,
    CreateDelegationToken,
    RenewDelegationToken,
    ExpireDelegationToken,
    DescribeDelegationToken,
    DeleteGroups,
    ElectLeaders,
    IncrementalAlterConfigs,
    AlterPartitionReassignments,
    ListPartitionReassignments,
    OffsetDelete,
    DescribeClientQuotas,
    AlterClientQuotas,
    DescribeUserScramCredentials,
    AlterUserScramCredentials,
    DescribeQuorum,
    AlterPartition,
    UpdateFeatures,
    DescribeCluster,
    DescribeProducers,
    UnregisterBroker,
    DescribeTransactions,
    ListTransactions,
    AllocateProducerIds,
    Unsupported(i16),
}

impl Write for ApiKey {
    fn calculate_size(&self) -> i32 {
        i16::SIZE
    }
    async fn write_to(&self, writer: &mut (dyn AsyncWrite + Send + Unpin)) -> Result<()> {
        use ApiKey::*;
        let n: i16 = match self {
            Produce => 0,
            Fetch => 1,
            ListOffsets => 2,
            Metadata => 3,
            LeaderAndIsr => 4,
            StopReplica => 5,
            UpdateMetadata => 6,
            ControlledShutdown => 7,
            OffsetCommit => 8,
            OffsetFetch => 9,
            FindCoordinator => 10,
            JoinGroup => 11,
            Heartbeat => 12,
            LeaveGroup => 13,
            SyncGroup => 14,
            DescribeGroups => 15,
            ListGroups => 16,
            SaslHandshake => 17,
            ApiVersions => 18,
            CreateTopics => 19,
            DeleteTopics => 20,
            DeleteRecords => 21,
            InitProducerId => 22,
            OffsetForLeaderEpoch => 23,
            AddPartitionsToTxn => 24,
            AddOffsetsToTxn => 25,
            EndTxn => 26,
            WriteTxnMarkers => 27,
            TxnOffsetCommit => 28,
            DescribeAcls => 29,
            CreateAcls => 30,
            DeleteAcls => 31,
            DescribeConfigs => 32,
            AlterConfigs => 33,
            AlterReplicaLogDirs => 34,
            DescribeLogDirs => 35,
            SaslAuthenticate => 36,
            CreatePartitions => 37,
            CreateDelegationToken => 38,
            RenewDelegationToken => 39,
            ExpireDelegationToken => 40,
            DescribeDelegationToken => 41,
            DeleteGroups => 42,
            ElectLeaders => 43,
            IncrementalAlterConfigs => 44,
            AlterPartitionReassignments => 45,
            ListPartitionReassignments => 46,
            OffsetDelete => 47,
            DescribeClientQuotas => 48,
            AlterClientQuotas => 49,
            DescribeUserScramCredentials => 50,
            AlterUserScramCredentials => 51,
            DescribeQuorum => 55,
            AlterPartition => 56,
            UpdateFeatures => 57,
            DescribeCluster => 60,
            DescribeProducers => 61,
            UnregisterBroker => 64,
            DescribeTransactions => 65,
            ListTransactions => 66,
            AllocateProducerIds => 67,
            Unsupported(n) => *n,
        };
        n.write_to(writer).await
    }
}

impl Read for ApiKey {
    async fn read_from(reader: &mut (dyn tokio::io::AsyncRead + Send + Unpin)) -> Result<Self> {
        let n = i16::read_from(reader).await?;
        use ApiKey::*;
        let v = match n {
            0 => Produce,
            1 => Fetch,
            2 => ListOffsets,
            3 => Metadata,
            4 => LeaderAndIsr,
            5 => StopReplica,
            6 => UpdateMetadata,
            7 => ControlledShutdown,
            8 => OffsetCommit,
            9 => OffsetFetch,
            10 => FindCoordinator,
            11 => JoinGroup,
            12 => Heartbeat,
            13 => LeaveGroup,
            14 => SyncGroup,
            15 => DescribeGroups,
            16 => ListGroups,
            17 => SaslHandshake,
            18 => ApiVersions,
            19 => CreateTopics,
            20 => DeleteTopics,
            21 => DeleteRecords,
            22 => InitProducerId,
            23 => OffsetForLeaderEpoch,
            24 => AddPartitionsToTxn,
            25 => AddOffsetsToTxn,
            26 => EndTxn,
            27 => WriteTxnMarkers,
            28 => TxnOffsetCommit,
            29 => DescribeAcls,
            30 => CreateAcls,
            31 => DeleteAcls,
            32 => DescribeConfigs,
            33 => AlterConfigs,
            34 => AlterReplicaLogDirs,
            35 => DescribeLogDirs,
            36 => SaslAuthenticate,
            37 => CreatePartitions,
            38 => CreateDelegationToken,
            39 => RenewDelegationToken,
            40 => ExpireDelegationToken,
            41 => DescribeDelegationToken,
            42 => DeleteGroups,
            43 => ElectLeaders,
            44 => IncrementalAlterConfigs,
            45 => AlterPartitionReassignments,
            46 => ListPartitionReassignments,
            47 => OffsetDelete,
            48 => DescribeClientQuotas,
            49 => AlterClientQuotas,
            50 => DescribeUserScramCredentials,
            51 => AlterUserScramCredentials,
            55 => DescribeQuorum,
            56 => AlterPartition,
            57 => UpdateFeatures,
            60 => DescribeCluster,
            61 => DescribeProducers,
            64 => UnregisterBroker,
            65 => DescribeTransactions,
            66 => ListTransactions,
            67 => AllocateProducerIds,
            n => Unsupported(n),
        };
        Ok(v)
    }
}