2 releases (1 stable)
1.0.0 | Oct 13, 2024 |
---|---|
0.1.0 | Oct 13, 2024 |
#384 in Procedural macros
301 downloads per month
7KB
██████╗ ██████╗ ███╗ ███╗██████╗ ██╗██╗ ███████╗████████╗██╗███╗ ███╗███████╗ ██████╗ ███████╗ ██████╗ ███████╗██╗ ██╗ ██╔════╝██╔═══██╗████╗ ████║██╔══██╗██║██║ ██╔════╝╚══██╔══╝██║████╗ ████║██╔════╝ ██╔══██╗██╔════╝██╔════╝ ██╔════╝╚██╗██╔╝ ██║ ██║ ██║██╔████╔██║██████╔╝██║██║ █████╗ ██║ ██║██╔████╔██║█████╗█████╗██████╔╝█████╗ ██║ ███╗█████╗ ╚███╔╝ ██║ ██║ ██║██║╚██╔╝██║██╔═══╝ ██║██║ ██╔══╝ ██║ ██║██║╚██╔╝██║██╔══╝╚════╝██╔══██╗██╔══╝ ██║ ██║██╔══╝ ██╔██╗ ╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ██║███████╗███████╗ ██║ ██║██║ ╚═╝ ██║███████╗ ██║ ██║███████╗╚██████╔╝███████╗██╔╝ ██╗ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ----------------------------------------------------------------------------------------------------------------------------------- simple proc macro for compile time regex
🚀 Installation
include it in your Cargo.toml
under [dependencies]
compiletime-regex = "*"
🧑💻 Usage examples
Basic Usage
use compiletime_regex::regex;
use regex::Regex;
fn main() {
let hex_color_regex = regex!(r#"^#[a-fA-F0-9]{6}$"#);
assert!(hex_color_regex.is_match("#00C853"));
let invalid_regex: Regex = regex!(r#"^#[a-fA-F0-9]{6"#); // compile time error
}
with LazyCell
and LazyLock
use std::{cell::LazyCell, sync::LazyLock};
use compiletime_regex::regex;
use regex::Regex;
const USERNAME_REGEX: LazyCell<Regex> = LazyCell::new(|| regex!(r#"^[A-Za-z0-9_]{2,30}$"#));
const DATE_REGEX: LazyLock<Regex> = LazyLock::new(|| regex!(r#"^\d{4}-\d{2}-\d{2}$"#));
const INVALID_REGEX: LazyCell<Regex> = LazyCell::new(|| regex!(r#"^[A-Za-z0-9_]{2,30"#)); // compile time error
const INVALID_REGEX: LazyLock<Regex> = LazyLock::new(|| regex!(r#"^\d{4}-\d{2}-\d{2"#)); // compile time error
fn main() {
assert!(USERNAME_REGEX.is_match("enigma"));
assert!(DATE_REGEX.is_match("1912-06-23"));
}
🌟 Connect with Us
zahash – zahash.z@gmail.com
Distributed under the MIT license. See LICENSE
for more information.
🤝 Contribute to compiletime-regex
!
- Fork it (https://github.com/zahash/compiletime-regex/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request
❤️ Show Some Love!
If you find compiletime-regex
helpful and enjoy using it, consider giving it a ⭐ on GitHub! Your star is a gesture of appreciation and encouragement for the continuous improvement of compiletime-regex
.
Dependencies
~2.5–4MB
~71K SLoC