Crates.io | compiletime-regex |
lib.rs | compiletime-regex |
version | 1.0.0 |
source | src |
created_at | 2024-10-13 17:19:36.39081 |
updated_at | 2024-10-13 18:11:25.033398 |
description | simple proc macro for compile time regex |
homepage | |
repository | https://github.com/zahash/compiletime-regex/ |
max_upload_size | |
id | 1407489 |
size | 6,429 |
██████╗ ██████╗ ███╗ ███╗██████╗ ██╗██╗ ███████╗████████╗██╗███╗ ███╗███████╗ ██████╗ ███████╗ ██████╗ ███████╗██╗ ██╗ ██╔════╝██╔═══██╗████╗ ████║██╔══██╗██║██║ ██╔════╝╚══██╔══╝██║████╗ ████║██╔════╝ ██╔══██╗██╔════╝██╔════╝ ██╔════╝╚██╗██╔╝ ██║ ██║ ██║██╔████╔██║██████╔╝██║██║ █████╗ ██║ ██║██╔████╔██║█████╗█████╗██████╔╝█████╗ ██║ ███╗█████╗ ╚███╔╝ ██║ ██║ ██║██║╚██╔╝██║██╔═══╝ ██║██║ ██╔══╝ ██║ ██║██║╚██╔╝██║██╔══╝╚════╝██╔══██╗██╔══╝ ██║ ██║██╔══╝ ██╔██╗ ╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ██║███████╗███████╗ ██║ ██║██║ ╚═╝ ██║███████╗ ██║ ██║███████╗╚██████╔╝███████╗██╔╝ ██╗ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ----------------------------------------------------------------------------------------------------------------------------------- simple proc macro for compile time regex
include it in your Cargo.toml
under [dependencies]
compiletime-regex = "*"
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
}
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"));
}
zahash – zahash.z@gmail.com
Distributed under the MIT license. See LICENSE
for more information.
compiletime-regex
!git checkout -b feature/fooBar
)git commit -am 'Add some fooBar'
)git push origin feature/fooBar
)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
.