bytes_regex_switch!() { /* proc-macro */ }
Expand description
Define a set of lazy static statically compiled regexes, with a block
or expression for each one. The first matching expression is computed
with the named capture groups declaring &str
variables available for this
computation.
If no regex matches, return None
.
Example:
#[derive(Debug, PartialEq)]
enum Color {
Grey(u8),
Pink,
Rgb(u8, u8, u8),
}
let input = "rgb(1, 2, 3)";
let color = regex_switch!(input,
r#"^gr(a|e)y\((?<level>\d{1,2})\)$"#i => {
Color::Grey(level.parse()?)
}
"^pink"i => Color::Pink,
r#"^rgb\((?<r>\d+),\s*(?<g>\d+),\s*(?<b>\d+),?\)$"#i => Color::Rgb (
r.parse()?,
g.parse()?,
b.parse()?,
),
);
assert_eq!(color, Some(Color::Rgb(1, 2, 3)));