From 24207feb7726bd2db97693eb8fdd155d33612574 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Wed, 31 Mar 2021 23:04:09 -0600 Subject: basic sketch of general vibe --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 README.md (limited to 'README.md') diff --git a/README.md b/README.md new file mode 100644 index 0000000..91b8e71 --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# bird-machine + +Compile your regular expressions at compile time. + +## Example: find a date + +```rust +use bird_machine::{bird_machine, Machine}; + +#[bird_machine(r"^\d{4}-\d{2}-\d{2}$")] +struct Date; + +assert!(Date::is_match("2014-01-01")); +``` + +## Example: iterating over capture groups + +```rust +use bird_machine::{bird_machine, Machine}; + +#[bird_machine(r"(\d{4})-(\d{2})-(\d{2})")] +struct Date<'a>(&'a str, &'a str, &'a str); +let input = "2012-03-14, 2013-01-01 and 2014-07-05"; +let match_info = Date::captures_iter(input) + .map(|x: Date| format!("Month: {} Day: {} Year: {}", x.1, x.2, x.0)); +let expected = [ + "Month: 03 Day: 14 Year: 2012", + "Month: 01 Day: 01 Year: 2013", + "Month: 07 Day: 05 Year: 2014", +]; +for (actual, expected) in match_info.zip(expected) { + assert_eq!(actual, expected); +} +``` + +# Example: replacement with named capture groups + +```rust +use bird_machine::{bird_machine, Machine}; + +#[bird_machine(r"(?P\d{4})-(?P\d{2})-(?P\d{2})")] +struct Date<'a> { + y: &'a str, + m: &'a str, + d: &'a str, +} +let before = "2012-03-14, 2013-01-01 and 2014-07-05"; +let after = Date::replace_all(before, "$m/$d/$y"); +assert_eq!(after, "03/14/2012, 01/01/2013 and 07/05/2014"); +``` + +# Example: compile-time rejection of invalid regular expressions + +```rust,compile_fail +use bird_machine::bird_machine; + +#[bird_machine(r"(oops i left this group open")] +struct Bad; +``` \ No newline at end of file -- cgit v1.2.3