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 regex;
let re = regex!;
assert!;
Invalid Regex
Compilation fails if the regex is invalid. For example, the following code will not compile:
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
-/simple/src/main.rs:4:14
|
4 | let re = regex!;
Other examples are wrong syntax, missing closing parenthesis, and invalid escape sequences:
Which prints out the error:
error: Invalid regex: regex parse error:
r"[0-9a-f+"
^
error: unclosed character class
-/simple/src/main.rs:4:14
|
4 | let re = regex!;
License
Licensed under Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)