use crate::error::DecodeError;
macro_rules! api_keys {
($($name:ident = $value:literal),+ $(,)?) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ApiKey {
$($name,)+
}
impl ApiKey {
pub const fn code(self) -> i16 {
match self {
$(ApiKey::$name => $value,)+
}
}
pub const ALL: &'static [ApiKey] = &[$(ApiKey::$name,)+];
}
impl TryFrom<i16> for ApiKey {
type Error = DecodeError;
fn try_from(value: i16) -> Result<Self, Self::Error> {
match value {
$($value => Ok(ApiKey::$name),)+
other => Err(DecodeError::UnknownDiscriminant {
kind: "api key",
value: other.into(),
}),
}
}
}
};
}
api_keys! {
Produce = 0,
Fetch = 1,
ListOffsets = 2,
Metadata = 3,
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,
TxnOffsetCommit = 28,
DescribeConfigs = 32,
AlterConfigs = 33,
SaslAuthenticate = 36,
CreatePartitions = 37,
DeleteGroups = 42,
ElectLeaders = 43,
IncrementalAlterConfigs = 44,
OffsetDelete = 47,
DescribeCluster = 60,
ConsumerGroupHeartbeat = 68,
ConsumerGroupDescribe = 69,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn code_roundtrip() {
for &key in ApiKey::ALL {
assert_eq!(ApiKey::try_from(key.code()), Ok(key));
}
}
#[test]
fn unknown_key_rejected() {
assert!(matches!(
ApiKey::try_from(i16::MAX),
Err(DecodeError::UnknownDiscriminant { .. })
));
}
}