From a429e872e1c43b9d8a330443d5e2fd605101ca9c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 19 Feb 2016 10:49:50 +0100 Subject: [PATCH] Add ismatch!() macro --- libimagutil/src/ismatch.rs | 23 +++++++++++++++++++++++ libimagutil/src/lib.rs | 1 + 2 files changed, 24 insertions(+) create mode 100644 libimagutil/src/ismatch.rs diff --git a/libimagutil/src/ismatch.rs b/libimagutil/src/ismatch.rs new file mode 100644 index 00000000..9739f139 --- /dev/null +++ b/libimagutil/src/ismatch.rs @@ -0,0 +1,23 @@ +#[macro_export] +macro_rules! is_match { + ($expression: expr, $($pattern:tt)+) => { + is_match! {tt + match $expression { + $($pattern)+ => true, + _ => false + } + } + }; + (tt $value:expr) => ($value); +} + +#[test] +fn test_matching() { + let foo = Some("-12"); + assert!(is_match!(foo, Some(bar) if + is_match!(bar.as_bytes()[0], b'+' | b'-') && + is_match!(bar.as_bytes()[1], b'0'...b'9') + )); + assert!(!is_match!(foo, None)); +} + diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index a7586143..7ee20819 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -1,6 +1,7 @@ #[macro_use] extern crate log; extern crate regex; +pub mod ismatch; pub mod key_value_split; pub mod trace; pub mod variants;