Add ismatch!() macro

This commit is contained in:
Matthias Beyer 2016-02-19 10:49:50 +01:00
parent 0bf22672f9
commit a429e872e1
2 changed files with 24 additions and 0 deletions

View file

@ -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));
}

View file

@ -1,6 +1,7 @@
#[macro_use] extern crate log; #[macro_use] extern crate log;
extern crate regex; extern crate regex;
pub mod ismatch;
pub mod key_value_split; pub mod key_value_split;
pub mod trace; pub mod trace;
pub mod variants; pub mod variants;