Skip to content

Latest commit

 

History

History
46 lines (39 loc) · 872 Bytes

README.md

File metadata and controls

46 lines (39 loc) · 872 Bytes

rematch

A procedural macro to generate simple FromStr implementation based on Regular Expression matching

Examples

Usage for enum:

use rematch::rematch;

#[derive(Debug, PartialEq, Eq)]
#[rematch]
enum Test {
    #[rematch(r"a")]
    A,
    #[rematch(r"b (\d+)")]
    B(usize),
    #[rematch(r"c = (\d+)")]
    C { x: usize },
}

assert_eq!("a".parse::<Test>().unwrap(), Test::A);
assert_eq!("b 42".parse::<Test>().unwrap(), Test::B(42));
assert_eq!("c = 123".parse::<Test>().unwrap(), Test::C { x: 123 });

Usage for struct:

use rematch::rematch;

#[derive(Debug, PartialEq, Eq)]
#[rematch(r"a number (\d+) with some string ([abc]+)")]
struct Test {
    a: usize,
    s: String,
}

assert_eq!(
    "a number 42 with some string cabab"
        .parse::<Test>()
        .unwrap(),
    Test {
        a: 42,
        s: "cabab".to_owned(),
    }
);