odradek_protocol/
api_key.rs1use crate::error::DecodeError;
8
9macro_rules! api_keys {
10 ($($name:ident = $value:literal),+ $(,)?) => {
11 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13 #[non_exhaustive]
14 pub enum ApiKey {
15 $($name,)+
16 }
17
18 impl ApiKey {
19 pub const fn code(self) -> i16 {
21 match self {
22 $(ApiKey::$name => $value,)+
23 }
24 }
25
26 pub const ALL: &'static [ApiKey] = &[$(ApiKey::$name,)+];
28 }
29
30 impl TryFrom<i16> for ApiKey {
31 type Error = DecodeError;
32
33 fn try_from(value: i16) -> Result<Self, Self::Error> {
34 match value {
35 $($value => Ok(ApiKey::$name),)+
36 other => Err(DecodeError::UnknownDiscriminant {
37 kind: "api key",
38 value: other.into(),
39 }),
40 }
41 }
42 }
43 };
44}
45
46api_keys! {
47 Produce = 0,
48 Fetch = 1,
49 ListOffsets = 2,
50 Metadata = 3,
51 OffsetCommit = 8,
52 OffsetFetch = 9,
53 FindCoordinator = 10,
54 JoinGroup = 11,
55 Heartbeat = 12,
56 LeaveGroup = 13,
57 SyncGroup = 14,
58 DescribeGroups = 15,
59 ListGroups = 16,
60 SaslHandshake = 17,
61 ApiVersions = 18,
62 CreateTopics = 19,
63 DeleteTopics = 20,
64 DeleteRecords = 21,
65 InitProducerId = 22,
66 OffsetForLeaderEpoch = 23,
67 AddPartitionsToTxn = 24,
68 AddOffsetsToTxn = 25,
69 EndTxn = 26,
70 TxnOffsetCommit = 28,
71 DescribeConfigs = 32,
72 AlterConfigs = 33,
73 SaslAuthenticate = 36,
74 CreatePartitions = 37,
75 DeleteGroups = 42,
76 ElectLeaders = 43,
77 IncrementalAlterConfigs = 44,
78 OffsetDelete = 47,
79 DescribeCluster = 60,
80 ConsumerGroupHeartbeat = 68,
81 ConsumerGroupDescribe = 69,
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87
88 #[test]
89 fn code_roundtrip() {
90 for &key in ApiKey::ALL {
91 assert_eq!(ApiKey::try_from(key.code()), Ok(key));
92 }
93 }
94
95 #[test]
96 fn unknown_key_rejected() {
97 assert!(matches!(
98 ApiKey::try_from(i16::MAX),
99 Err(DecodeError::UnknownDiscriminant { .. })
100 ));
101 }
102}