graphfind_rs/pattern_matching/matcher.rs
1/// The Matcher type stands for any function that evaluates, given an element
2/// in the base graph with the type Weight, if the pattern graph accepts this element.
3///
4/// As an example, we may define a function that tests if a node matches a Rust pattern.
5///
6/// Recommended ways to create conditions are using either the lambda functions or the [crate::matcher] macro.
7pub type Matcher<Weight> = dyn Fn(&Weight) -> bool;
8
9/// Creates a `Matcher` function from a given pattern
10///
11/// The syntax is similar to the `std::matches` macro.
12/// Calling matcher with no arguments will match anything.
13///
14/// # Examples
15/// ```
16/// #[macro_use]
17/// extern crate graphfind_rs;
18/// use graphfind_rs::pattern_matching::*;
19///
20/// # // This line is hidden in the docs but required to pass the docstest, see https://users.rust-lang.org/t/how-to-use-a-macro-in-the-doc-test/3664/5?u=slrtbtfs
21///
22/// # fn main() {
23///
24/// enum Person {
25/// Student {name: String},
26/// Prof {},
27/// }
28///
29/// let student = matcher!(Person::Student{..});
30///
31/// let even = matcher!(i if i % 2 == 0);
32/// # }
33#[macro_export]
34macro_rules! matcher {
35 () => {matcher!(_)};
36 ($(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
37 |__weight__: &_|
38 match __weight__ {
39 $( $pattern )|+ $( if $guard )? => true,
40 _ => false
41 }
42 };
43}