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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! This crate contains a little macro to generate a lazy
//! [`Regex`](../regex/struct.Regex.html) and remove some boilerplate when
//! compiling regex expressions.
//!
//! # Usage
//!
//! Since it is an anti-pattern to compile the same regular expression in a loop
//! this means for most regex expressions you need to store them in a
//! [`LazyLock`] in a static. But this can get a bit tiresome with many regex
//! expressions. This crate provides a [`regex!`] macro that will store the
//! compiled regex in a global static and return a reference to it.
//!
//! [`LazyLock`]: std::sync::LazyLock
//!
//! ### Before
//!
//! ```
//! use std::sync::LazyLock;
//! use regex::Regex;
//!
//! // only compiled once, by storing in a static
//! static HEX_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
//! Regex::new("[0-9a-f]+").expect("invalid regex")
//! });
//!
//! # let my_iter = vec!["deadbeaf", "1234"];
//! for item in my_iter {
//! if HEX_PATTERN.is_match(item) {
//! // frobnicate
//! }
//! }
//! ```
//!
//! ### After
//!
//! ```
//! use regex_macro::regex;
//!
//! # let my_iter = vec!["deadbeaf", "1234"];
//! for item in my_iter {
//! // this is still only compiled once!
//! if regex!("[0-9a-f]+").is_match(item) {
//! // frobnicate
//! }
//! }
//! ```
//!
//! Isn't that much nicer?
//!
use LazyLock;
pub use Regex;
/// Convenient type alias for a lazy regex.
pub type LazyRegex = ;
/// Returns a lazy regex.
///
/// # Examples
///
/// ```
/// use regex_macro::{LazyRegex, lazy_regex};
///
/// static RE: LazyRegex = lazy_regex!("[0-9a-f]+");
/// ```
/// Creates a static regex and returns a reference to it.
///
/// See the [crate level documentation][crate] for more information.