path_scan!() { /* proc-macro */ }Expand description
path_scan! macro parses an input string using the provided patterns
and returns a closure that yields the result of the first matching arm.
Patterns are specified as follows:
-
"pattern string" => expressionIn the pattern string, placeholders like:identifierand{identifier}are captured and become available as variables. -
Multiple patterns can be combined using
"pattern1" | "pattern2" => expression. -
An optional
ifcondition may follow the pattern(s) (e.g.,"pattern" if condition => expression). The arm is considered a match only if the condition evaluates to true. -
The default arm is specified as
_ => expression.
Note:
If an if-less default arm is not provided—that is, if a default arm without an if condition is missing—
the closure returns None on failure, making the overall expression type Option<T>.
§Examples
use path_scan::path_scan;
// With a default arm, the return type is a concrete type (e.g., String)
let scanner = path_scan! {
"blog/:slug/index" => format!("blog: {}", slug),
"other/:slug" if slug.len() == 5 => format!("short blog: {}", slug),
_ => format!("default")
};
assert_eq!(scanner("blog/hello/index"), "blog: hello");
assert_eq!(scanner("other/short"), "short blog: short");
assert_eq!(scanner("unknown/path"), "default");
// Without a default arm, the return type is `Option<String>`
let scanner_opt = path_scan! {
"blog/:slug/index" => format!("blog: {}", slug)
};
assert_eq!(scanner_opt("blog/world/index"), Some("blog: world".to_string()));
assert_eq!(scanner_opt("unknown/path"), None);