Crates.io | rematch |
lib.rs | rematch |
version | 0.1.2 |
source | src |
created_at | 2022-06-14 03:52:09.971825 |
updated_at | 2022-06-14 15:38:02.940937 |
description | A procedural macro to generate simple FromStr implementation based on Regular Expression matching |
homepage | |
repository | https://github.com/pierd/rematch |
max_upload_size | |
id | 605536 |
size | 8,679 |
A procedural macro to generate simple FromStr implementation based on Regular Expression matching
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(),
}
);