rematch

Crates.iorematch
lib.rsrematch
version0.1.2
sourcesrc
created_at2022-06-14 03:52:09.971825
updated_at2022-06-14 15:38:02.940937
descriptionA procedural macro to generate simple FromStr implementation based on Regular Expression matching
homepage
repositoryhttps://github.com/pierd/rematch
max_upload_size
id605536
size8,679
Kuba (pierd)

documentation

README

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(),
    }
);
Commit count: 6

cargo fmt