From 96574bfd96a19254831f242620f9cdeca02a36eb Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Wed, 9 Nov 2016 20:03:51 -0800 Subject: Map op codes via a macro --- src/utils/mod.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/utils') diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e6d2b4c..103a73e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -40,6 +40,32 @@ macro_rules! status_concat { } } +macro_rules! map_nums { + ($item:ident; $($entry:ident $value:expr,)*) => { + impl $item { + pub fn num(&self) -> u64 { + match *self { + $($item::$entry => $value,)* + } + } + + pub fn from_num(num: u64) -> Option { + match num { + $($value => Some($item::$entry),)* + _ => None, + } + } + + fn decode(value: Value) -> Result { + value.as_u64().and_then(Self::from_num).ok_or(Error::Decode( + concat!("Expected valid ", stringify!($item)), + value + )) + } + } + } +} + #[doc(hidden)] pub fn decode_array Result>(value: Value, f: F) -> Result> { into_array(value).and_then(|x| x.into_iter().map(f).collect()) -- cgit v1.2.3