strmatch

Crates.iostrmatch
lib.rsstrmatch
version0.1.1
sourcesrc
created_at2023-01-24 13:45:56.845188
updated_at2023-02-01 17:47:58.409735
descriptionConditionally match strings in Rust using regex without much boilerplate
homepage
repositoryhttps://github.com/tropicbliss/strmatch
max_upload_size
id766682
size7,449
tropicbliss (tropicbliss)

documentation

README

strmatch

Conditionally match strings in Rust using regex without much boilerplate. Yes, this uses once_cell.

Usage

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);
Commit count: 9

cargo fmt