ismatch.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // imag - the personal information management suite for the commandline
  3. // Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; version
  8. // 2.1 of the License.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. //
  19. #[macro_export]
  20. macro_rules! is_match {
  21. ($expression: expr, $($pattern:tt)+) => {
  22. is_match! {tt
  23. match $expression {
  24. $($pattern)+ => true,
  25. _ => false
  26. }
  27. }
  28. };
  29. (tt $value:expr) => ($value);
  30. }
  31. #[test]
  32. fn test_matching() {
  33. let foo = Some("-12");
  34. assert!(is_match!(foo, Some(bar) if
  35. is_match!(bar.as_bytes()[0], b'+' | b'-') &&
  36. is_match!(bar.as_bytes()[1], b'0'...b'9')
  37. ));
  38. assert!(!is_match!(foo, None));
  39. }