lure 0.1.2

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.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented2 out of 2 items with examples
  • Size
  • Source code size: 7.46 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.28 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 25s Average build duration of successful builds.
  • all releases: 24s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • luander/lure
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • luander

lure

Crates.io Version Docs.rs Latest cicd

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)