3 releases
new 0.1.2 | Feb 22, 2025 |
---|---|
0.1.1 | Feb 22, 2025 |
0.1.0 | Feb 22, 2025 |
#624 in Procedural macros
58 downloads per month
Used in lure
6KB
lure
Shift left with Lure, a Rust crate that provides a macro for creating lazy Regex instances with compile-time validation, ensuring invalid patterns fail to compile.
Usage
This Rust crate helps prevent common pitfalls when working with regular expressions by ensuring patterns are valid at compile time and avoiding redundant compilations. It leverages the standard library OnceCell
to compile regexes only once and uses a procedural macro for compile-time validation, improving both safety and performance.
The only dependency in the crate if regex
Example:
use lure::regex;
let re = regex!("[0-9a-f]+");
assert!(re.is_match("deadbeef1234"));
Invalid Regex
Compilation fails if the regex is invalid. For example, the following code will not compile:
fn main() {
let re = lure::regex!(r"/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/");
assert!(re.is_match("randomChars123!"));
}
Trying to compile the code above will result in the following error:
error: Invalid regex: regex parse error:
r"/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/"
^^^
error: look-around, including look-ahead and look-behind, is not supported
--> examples/simple/src/main.rs:4:14
|
4 | let re = regex!(r"/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Other examples are wrong syntax, missing closing parenthesis, and invalid escape sequences:
fn main() {
let re = regex!(r"[0-9a-f+");
assert!(re.is_match("1234deadbeef"));
}
Which prints out the error:
error: Invalid regex: regex parse error:
r"[0-9a-f+"
^
error: unclosed character class
--> examples/simple/src/main.rs:4:14
|
4 | let re = regex!(r"[0-9a-f+");
| ^^^^^^^^^^^^^^^^^^^
License
Licensed under Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
Dependencies
~2.2–3MB
~54K SLoC