coerce_pattern is a Rust library crate (here on crates.io) containing macros that force expressions into patterns. It is version-controlled on GitHub here.
This crate contains two proc macros relating to asserting, under penalty of panic, that expressions match patterns.
coerce_pattern!matches an expression to a target pattern and evaluates to an expression of the inner variables from the pattern, panicking if the pattern doesn't matchassert_pattern!matches an expression to a target and panics if the pattern doesn't match.
To use them, run the following command in your project directory.
cargo add coerce_patternThen, you can use the macros using
use coerce_pattern::{coerce_pattern, assert_pattern};The assert_pattern!($expression, $target_pattern) macro roughly expands out to
match $expression {
$target_pattern => {}
_ => panic!(),
}This means you can use it whenever you want to claim, at risk of panicking (such as in unit tests in particular), that a particular expression matches a particular pattern. For example
let x = Some(Some(1));
assert_pattern!(x, Some(Some(_)));The coerce_pattern!($expression, $target_pattern, $result) macro roughly expands out to
match $expression {
$target_pattern => $result,
_ => panic!(),
}This is useful in cases similar to assert_pattern!, except that you actually want to capture one or more variables from the matching pattern. Consider
struct LegalEntity {
Person { name: String },
Company { dba: String, states: Vec<String> },
}
let entity = LegalEntity::Company{
dba: String::from("my_company"),
states: ["NY", "NJ", "CT"].into_iter().map(String::from).collect()
}
let states = coerce_pattern!(entity, LegalEntity::Company { states, .. }, states);
assert_eq!(states.len(), 3);See the documentation in src/lib.rs for more specific details and more examples of each macro's usage.