diff options
| author | Austin Hellyer <[email protected]> | 2016-11-09 20:03:51 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-11-09 20:03:51 -0800 |
| commit | 96574bfd96a19254831f242620f9cdeca02a36eb (patch) | |
| tree | 222148f1fc1f38d8bfe71b504d41a1a2f80c79fa /src/utils | |
| parent | Fix message decoding with reactions (diff) | |
| download | serenity-96574bfd96a19254831f242620f9cdeca02a36eb.tar.xz serenity-96574bfd96a19254831f242620f9cdeca02a36eb.zip | |
Map op codes via a macro
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/mod.rs | 26 |
1 files changed, 26 insertions, 0 deletions
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<Self> { + match num { + $($value => Some($item::$entry),)* + _ => None, + } + } + + fn decode(value: Value) -> Result<Self> { + value.as_u64().and_then(Self::from_num).ok_or(Error::Decode( + concat!("Expected valid ", stringify!($item)), + value + )) + } + } + } +} + #[doc(hidden)] pub fn decode_array<T, F: Fn(Value) -> Result<T>>(value: Value, f: F) -> Result<Vec<T>> { into_array(value).and_then(|x| x.into_iter().map(f).collect()) |