Skip to content

tropicbliss/strmatch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);

About

Conditionally match strings in Rust using regex without much boilerplate

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages