| Crates.io | lure |
| lib.rs | lure |
| version | 0.1.2 |
| created_at | 2025-02-22 16:29:50.649675+00 |
| updated_at | 2025-02-22 18:46:27.710943+00 |
| description | 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. |
| homepage | |
| repository | https://github.com/luander/lure |
| max_upload_size | |
| id | 1565550 |
| size | 7,958 |
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.
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"));
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+");
| ^^^^^^^^^^^^^^^^^^^
Licensed under Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)