Crates.io | strmatch |
lib.rs | strmatch |
version | 0.1.1 |
source | src |
created_at | 2023-01-24 13:45:56.845188 |
updated_at | 2023-02-01 17:47:58.409735 |
description | Conditionally match strings in Rust using regex without much boilerplate |
homepage | |
repository | https://github.com/tropicbliss/strmatch |
max_upload_size | |
id | 766682 |
size | 7,449 |
Conditionally match strings in Rust using regex without much boilerplate. Yes, this uses once_cell
.
use strmatch::strmatch;
#[derive(PartialEq, Eq, Debug)]
enum StringType {
Phone,
Email,
Others,
}
let email = "example@example.com";
let result = strmatch!(email => {
r#"(\d{4})-(\d{2})-(\d{2})"# => StringType::Phone,
r#"^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$"# => StringType::Email,
_ => StringType::Others
});
assert_eq!(StringType::Email, result);
let result = strmatch!("example@example.com" => {
// Phone
r#"(\d{4})-(\d{2})-(\d{2})"# => {
1 + 2
},
// Email
r#"^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$"# => {
3 + 4
},
_ => 5,
});
assert_eq!(7, result);