Add ismatch!() macro
This commit is contained in:
parent
0bf22672f9
commit
a429e872e1
2 changed files with 24 additions and 0 deletions
23
libimagutil/src/ismatch.rs
Normal file
23
libimagutil/src/ismatch.rs
Normal 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));
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue