A rust macro that allows regular expression creation through independent files. Allows fully typed named groups.
added iterators
4 files changed, +27 -7
+3-1README.md
| @@ -19,6 +19,8 @@ Allows for named groups in regular expressions to be generated from special file | ||
| 19 | 19 | 2. `from_file(filename: &str)` Finds a single match after opening the given filename. |
| 20 | 20 | 3. `vec_from_str(text: &str)` Extracts all the matches in a given string. |
| 21 | 21 | 4. `vec_from_file(filename: &str)` Extracts all the matches after opening the given filename. |
| 22 | +5. `iter_from_str(text: &str)` Extracts all the matches in a given string. | |
| 23 | +6. `iter_from_file(filename: &str)` Extracts all the matches after opening the given filename. | |
| 22 | 24 | |
| 23 | 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. |
| 24 | 26 | |
| @@ -39,9 +41,9 @@ The abstract structure for matches within the regex_macro library contains `star | ||
| 39 | 41 | |
| 40 | 42 | ```rust |
| 41 | 43 | use regex_macro::load_regex_files; |
| 44 | +load_regex_files!("tests/regex"); | |
| 42 | 45 | |
| 43 | 46 | fn main() { |
| 44 | - load_regex_files!("tests/regex"); | |
| 45 | 47 | let events = EventRE::vec_from_file("tests/samples/events.txt").unwrap(); |
| 46 | 48 | println!("{}", serde_json::to_string_pretty(&events).unwrap()); |
| 47 | 49 | } |
+1-1examples/event.rs
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | use regex_macro::load_regex_files; |
| 2 | +load_regex_files!("tests/regex"); | |
| 2 | 3 | |
| 3 | 4 | fn main() { |
| 4 | - load_regex_files!("tests/regex"); | |
| 5 | 5 | let events = EventRE::vec_from_file("tests/samples/events.txt").unwrap(); |
| 6 | 6 | println!("{}", serde_json::to_string_pretty(&events).unwrap()); |
| 7 | 7 | } |
+14-2src/lib.rs
| @@ -145,7 +145,7 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 145 | 145 | |
| 146 | 146 | /// Extracts all regex matches for the given string |
| 147 | 147 | /// Each match contains a `start_pos`, `end_pos`, and each field for the given class contains `start_pos`, `end_pos`, and `val` |
| 148 | - pub fn vec_from_str(text: &str) -> Vec<Self> { | |
| 148 | + pub fn iter_from_str(text: &str) -> impl Iterator<Item = Self> + '_ { | |
| 149 | 149 | let re = &RE.#ident; |
| 150 | 150 | re.captures_iter(text) |
| 151 | 151 | .map(|captures| { |
| @@ -155,7 +155,19 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 155 | 155 | #(#field_initializers),* |
| 156 | 156 | } |
| 157 | 157 | }) |
| 158 | - .collect() | |
| 158 | + } | |
| 159 | + | |
| 160 | + /// Extracts all regex matches for the given string | |
| 161 | + /// Each match contains a `start_pos`, `end_pos`, and each field for the given class contains `start_pos`, `end_pos`, and `val` | |
| 162 | + pub fn vec_from_str(text: &str) -> Vec<Self> { | |
| 163 | + Self::iter_from_str(text).collect() | |
| 164 | + } | |
| 165 | + | |
| 166 | + /// Extracts all regex matches for the text contents of a given file | |
| 167 | + /// Each match contains a `start_pos`, `end_pos`, and each field for the given class contains `start_pos`, `end_pos`, and `val` | |
| 168 | + pub fn iter_from_file(filename: &str) -> Result<impl Iterator<Item = Self> + '_, std::io::Error> { | |
| 169 | + let text = std::fs::read_to_string(filename)?; | |
| 170 | + Ok(Self::iter_from_str(Box::leak(text.into_boxed_str()))) | |
| 159 | 171 | } |
| 160 | 172 | |
| 161 | 173 | /// Extracts all regex matches for the text contents of a given file |
+9-3tests/test.rs
| @@ -1,8 +1,14 @@ | ||
| 1 | 1 | use regex_macro::load_regex_files; |
| 2 | +load_regex_files!("./tests/regex"); | |
| 2 | 3 | |
| 3 | 4 | #[test] |
| 4 | -pub fn events() { | |
| 5 | - load_regex_files!("./tests/regex"); | |
| 5 | +pub fn events_vec() { | |
| 6 | 6 | let events = EventRE::vec_from_file("./tests/samples/events.txt"); |
| 7 | - assert!(events.unwrap().len() == 13) | |
| 7 | + assert!(events.unwrap().len() == 13); | |
| 8 | +} | |
| 9 | + | |
| 10 | +#[test] | |
| 11 | +pub fn events_iter() { | |
| 12 | + let events = EventRE::iter_from_file("./tests/samples/events.txt").unwrap(); | |
| 13 | + assert_eq!(events.count(), 13); | |
| 8 | 14 | } |