A rust macro that allows regular expression creation through independent files. Allows fully typed named groups.
added the outer val
4 files changed, +13 -4
+1-1.cargo/config.toml
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | [alias] |
| 2 | -tr = "test --release" | |
| 3 | 2 | e = "run --example" |
| 4 | 3 | b = "build" |
| 4 | +t = "test --tests" |
+3-3README.md
| @@ -22,7 +22,7 @@ Allows for named groups in regular expressions to be generated from special file | ||
| 22 | 22 | 5. `iter_from_str(text: &str)` Extracts all the matches in a given string. |
| 23 | 23 | 6. `iter_from_file(filename: &str)` Extracts all the matches after opening the given filename. |
| 24 | 24 | |
| 25 | -The abstract structure for matches within the regex_macro library contains `start_pos`, `end_pos`, and public fields for each named group inside of the `.re` file. Each field then contains a `start_pos`, `end_pos`, and `val` field. | |
| 25 | +The abstract structure for matches within the regex_folder_macro library contains `start_pos`, `end_pos`, and public fields for each named group inside of the `.re` file. Each field then contains a `start_pos`, `end_pos`, and `val` field. | |
| 26 | 26 | |
| 27 | 27 | ### Tips |
| 28 | 28 | |
| @@ -40,12 +40,12 @@ The abstract structure for matches within the regex_macro library contains `star | ||
| 40 | 40 | ### event.rs |
| 41 | 41 | |
| 42 | 42 | ```rust |
| 43 | -use regex_macro::load_regex_files; | |
| 43 | +use regex_folder_macro::load_regex_files; | |
| 44 | 44 | // Loads the regular expressions within the folder named regex |
| 45 | 45 | load_regex_files!("regex"); |
| 46 | 46 | |
| 47 | 47 | fn main() { |
| 48 | - let events = EventRE::vec_from_file("tests/samples/events.txt").unwrap(); | |
| 48 | + let events = EventRE::vec_from_file("samples/events.txt").unwrap(); | |
| 49 | 49 | println!("{}", serde_json::to_string_pretty(&events).unwrap()); |
| 50 | 50 | } |
| 51 | 51 | ``` |
+3-0src/lib.rs
| @@ -107,6 +107,7 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 107 | 107 | pub struct #ident { |
| 108 | 108 | pub start_pos: usize, |
| 109 | 109 | pub end_pos: usize, |
| 110 | + pub val: String, | |
| 110 | 111 | #(#fields),* |
| 111 | 112 | } |
| 112 | 113 | |
| @@ -126,6 +127,7 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 126 | 127 | Some(Self { |
| 127 | 128 | start_pos: captures.get(0).unwrap().start(), |
| 128 | 129 | end_pos: captures.get(0).unwrap().end(), |
| 130 | + val: captures.get(0).unwrap().as_str().to_string(), | |
| 129 | 131 | #(#field_initializers),* |
| 130 | 132 | }) |
| 131 | 133 | } else { |
| @@ -149,6 +151,7 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 149 | 151 | Self { |
| 150 | 152 | start_pos: captures.get(0).unwrap().start(), |
| 151 | 153 | end_pos: captures.get(0).unwrap().end(), |
| 154 | + val: captures.get(0).unwrap().as_str().to_string(), | |
| 152 | 155 | #(#field_initializers),* |
| 153 | 156 | } |
| 154 | 157 | }) |
+6-0tests/test.rs
| @@ -13,3 +13,9 @@ pub fn events_iter() { | ||
| 13 | 13 | let events = EventRE::iter_from_file("./samples/events.txt").unwrap(); |
| 14 | 14 | assert_eq!(events.count(), 13); |
| 15 | 15 | } |
| 16 | + | |
| 17 | +#[test] | |
| 18 | +pub fn first_event() { | |
| 19 | + let event = EventRE::from_file("samples/events.txt").unwrap().unwrap(); | |
| 20 | + assert_eq!(event.val, "7:30–8:00 AM Wake up + hydrate + light stretch"); | |
| 21 | +} |