rskafka/protocol/
api_key.rs

1//! ApiKey to tag request types.
2//!
3//! # References
4//! - <https://kafka.apache.org/protocol#protocol_api_keys>
5
6use super::primitives::Int16;
7
8#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
9#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
10pub enum ApiKey {
11    Produce,
12    Fetch,
13    ListOffsets,
14    Metadata,
15    LeaderAndIsr,
16    StopReplica,
17    UpdateMetadata,
18    ControlledShutdown,
19    OffsetCommit,
20    OffsetFetch,
21    FindCoordinator,
22    JoinGroup,
23    Heartbeat,
24    LeaveGroup,
25    SyncGroup,
26    DescribeGroups,
27    ListGroups,
28    SaslHandshake,
29    ApiVersions,
30    CreateTopics,
31    DeleteTopics,
32    DeleteRecords,
33    InitProducerId,
34    OffsetForLeaderEpoch,
35    AddPartitionsToTxn,
36    AddOffsetsToTxn,
37    EndTxn,
38    WriteTxnMarkers,
39    TxnOffsetCommit,
40    DescribeAcls,
41    CreateAcls,
42    DeleteAcls,
43    DescribeConfigs,
44    AlterConfigs,
45    AlterReplicaLogDirs,
46    DescribeLogDirs,
47    SaslAuthenticate,
48    CreatePartitions,
49    CreateDelegationToken,
50    RenewDelegationToken,
51    ExpireDelegationToken,
52    DescribeDelegationToken,
53    DeleteGroups,
54    ElectLeaders,
55    IncrementalAlterConfigs,
56    AlterPartitionReassignments,
57    ListPartitionReassignments,
58    OffsetDelete,
59    DescribeClientQuotas,
60    AlterClientQuotas,
61    DescribeUserScramCredentials,
62    AlterUserScramCredentials,
63    AlterIsr,
64    UpdateFeatures,
65    DescribeCluster,
66    DescribeProducers,
67    DescribeTransactions,
68    ListTransactions,
69    AllocateProducerIds,
70    Unknown(Int16),
71}
72
73impl From<Int16> for ApiKey {
74    fn from(key: Int16) -> Self {
75        match key.0 {
76            0 => Self::Produce,
77            1 => Self::Fetch,
78            2 => Self::ListOffsets,
79            3 => Self::Metadata,
80            4 => Self::LeaderAndIsr,
81            5 => Self::StopReplica,
82            6 => Self::UpdateMetadata,
83            7 => Self::ControlledShutdown,
84            8 => Self::OffsetCommit,
85            9 => Self::OffsetFetch,
86            10 => Self::FindCoordinator,
87            11 => Self::JoinGroup,
88            12 => Self::Heartbeat,
89            13 => Self::LeaveGroup,
90            14 => Self::SyncGroup,
91            15 => Self::DescribeGroups,
92            16 => Self::ListGroups,
93            17 => Self::SaslHandshake,
94            18 => Self::ApiVersions,
95            19 => Self::CreateTopics,
96            20 => Self::DeleteTopics,
97            21 => Self::DeleteRecords,
98            22 => Self::InitProducerId,
99            23 => Self::OffsetForLeaderEpoch,
100            24 => Self::AddPartitionsToTxn,
101            25 => Self::AddOffsetsToTxn,
102            26 => Self::EndTxn,
103            27 => Self::WriteTxnMarkers,
104            28 => Self::TxnOffsetCommit,
105            29 => Self::DescribeAcls,
106            30 => Self::CreateAcls,
107            31 => Self::DeleteAcls,
108            32 => Self::DescribeConfigs,
109            33 => Self::AlterConfigs,
110            34 => Self::AlterReplicaLogDirs,
111            35 => Self::DescribeLogDirs,
112            36 => Self::SaslAuthenticate,
113            37 => Self::CreatePartitions,
114            38 => Self::CreateDelegationToken,
115            39 => Self::RenewDelegationToken,
116            40 => Self::ExpireDelegationToken,
117            41 => Self::DescribeDelegationToken,
118            42 => Self::DeleteGroups,
119            43 => Self::ElectLeaders,
120            44 => Self::IncrementalAlterConfigs,
121            45 => Self::AlterPartitionReassignments,
122            46 => Self::ListPartitionReassignments,
123            47 => Self::OffsetDelete,
124            48 => Self::DescribeClientQuotas,
125            49 => Self::AlterClientQuotas,
126            50 => Self::DescribeUserScramCredentials,
127            51 => Self::AlterUserScramCredentials,
128            56 => Self::AlterIsr,
129            57 => Self::UpdateFeatures,
130            60 => Self::DescribeCluster,
131            61 => Self::DescribeProducers,
132            65 => Self::DescribeTransactions,
133            66 => Self::ListTransactions,
134            67 => Self::AllocateProducerIds,
135            _ => Self::Unknown(key),
136        }
137    }
138}
139
140impl From<ApiKey> for Int16 {
141    fn from(key: ApiKey) -> Self {
142        match key {
143            ApiKey::Produce => Self(0),
144            ApiKey::Fetch => Self(1),
145            ApiKey::ListOffsets => Self(2),
146            ApiKey::Metadata => Self(3),
147            ApiKey::LeaderAndIsr => Self(4),
148            ApiKey::StopReplica => Self(5),
149            ApiKey::UpdateMetadata => Self(6),
150            ApiKey::ControlledShutdown => Self(7),
151            ApiKey::OffsetCommit => Self(8),
152            ApiKey::OffsetFetch => Self(9),
153            ApiKey::FindCoordinator => Self(10),
154            ApiKey::JoinGroup => Self(11),
155            ApiKey::Heartbeat => Self(12),
156            ApiKey::LeaveGroup => Self(13),
157            ApiKey::SyncGroup => Self(14),
158            ApiKey::DescribeGroups => Self(15),
159            ApiKey::ListGroups => Self(16),
160            ApiKey::SaslHandshake => Self(17),
161            ApiKey::ApiVersions => Self(18),
162            ApiKey::CreateTopics => Self(19),
163            ApiKey::DeleteTopics => Self(20),
164            ApiKey::DeleteRecords => Self(21),
165            ApiKey::InitProducerId => Self(22),
166            ApiKey::OffsetForLeaderEpoch => Self(23),
167            ApiKey::AddPartitionsToTxn => Self(24),
168            ApiKey::AddOffsetsToTxn => Self(25),
169            ApiKey::EndTxn => Self(26),
170            ApiKey::WriteTxnMarkers => Self(27),
171            ApiKey::TxnOffsetCommit => Self(28),
172            ApiKey::DescribeAcls => Self(29),
173            ApiKey::CreateAcls => Self(30),
174            ApiKey::DeleteAcls => Self(31),
175            ApiKey::DescribeConfigs => Self(32),
176            ApiKey::AlterConfigs => Self(33),
177            ApiKey::AlterReplicaLogDirs => Self(34),
178            ApiKey::DescribeLogDirs => Self(35),
179            ApiKey::SaslAuthenticate => Self(36),
180            ApiKey::CreatePartitions => Self(37),
181            ApiKey::CreateDelegationToken => Self(38),
182            ApiKey::RenewDelegationToken => Self(39),
183            ApiKey::ExpireDelegationToken => Self(40),
184            ApiKey::DescribeDelegationToken => Self(41),
185            ApiKey::DeleteGroups => Self(42),
186            ApiKey::ElectLeaders => Self(43),
187            ApiKey::IncrementalAlterConfigs => Self(44),
188            ApiKey::AlterPartitionReassignments => Self(45),
189            ApiKey::ListPartitionReassignments => Self(46),
190            ApiKey::OffsetDelete => Self(47),
191            ApiKey::DescribeClientQuotas => Self(48),
192            ApiKey::AlterClientQuotas => Self(49),
193            ApiKey::DescribeUserScramCredentials => Self(50),
194            ApiKey::AlterUserScramCredentials => Self(51),
195            ApiKey::AlterIsr => Self(56),
196            ApiKey::UpdateFeatures => Self(57),
197            ApiKey::DescribeCluster => Self(60),
198            ApiKey::DescribeProducers => Self(61),
199            ApiKey::DescribeTransactions => Self(65),
200            ApiKey::ListTransactions => Self(66),
201            ApiKey::AllocateProducerIds => Self(67),
202            ApiKey::Unknown(code) => code,
203        }
204    }
205}
206
207#[cfg(test)]
208mod tests {
209    use super::*;
210
211    use proptest::prelude::*;
212
213    proptest! {
214        #[test]
215        fn test_roundrip_int16(code: Int16) {
216            let api_key = ApiKey::from(code);
217            let code2 = Int16::from(api_key);
218            assert_eq!(code, code2);
219        }
220
221        #[test]
222        fn test_roundrip_api_key(key: ApiKey) {
223            let key = match key {
224                // Ensure key is actually unknown
225                ApiKey::Unknown(x) => ApiKey::from(x),
226                _ => key,
227            };
228
229            let code = Int16::from(key);
230            let key2 = ApiKey::from(code);
231            assert_eq!(key, key2);
232        }
233    }
234}