1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! This crate contains a little macro to generate a lazy
//! [`Regex`](../regex/struct.Regex.html) and remove some boilerplate when
//! compiling regex expressions.
//!
//! # Usage
//!
//! Generally you want to avoid compiling a regex multiple times. The `regex`
//! crate suggests using `lazy_static` for this but you can also use `once_cell`
//! which is what this crate uses. For example:
//!
//! ```rust
//! use regex_macro::regex;
//!
//! let re = regex!("[0-9a-f]+");
//! assert!(re.is_match("1234deadbeef"));
//! ```
//!
//! Which is equivalent to the following.
//! ```rust
//! use once_cell::sync::Lazy;
//! use regex::Regex;
//!
//! static RE: Lazy<Regex> = Lazy::new(|| Regex::new("[0-9a-f]+").unwrap());
//! assert!(RE.is_match("1234deadbeef"));
//! ```

#[doc(hidden)]
pub type Regex = regex::Regex;
#[doc(hidden)]
pub type Lazy = once_cell::sync::Lazy<Regex>;

/// Generate a static regex.
#[macro_export]
macro_rules! regex {
    ($re:expr $(,)?) => {{
        static RE: $crate::Lazy = $crate::Lazy::new(|| $crate::Regex::new($re).unwrap());
        $crate::Lazy::force(&RE)
    }};
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn regex() {
        let hex = regex!("[0-9a-f]+");
        assert!(hex.is_match("1234deadbeef"));
    }
}