irongit

A rust macro that allows regular expression creation through independent files. Allows fully typed named groups.

added iterators

huncholanehuncholaneauthored
parent 1cd02fecommit 4cd9043e4ca163a6d97a5ee63b98532099cd7ff0Browse files

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
1919 2. `from_file(filename: &str)` Finds a single match after opening the given filename.
2020 3. `vec_from_str(text: &str)` Extracts all the matches in a given string.
2121 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.
2224
2325 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.
2426
@@ -39,9 +41,9 @@ The abstract structure for matches within the regex_macro library contains `star
3941
4042 ```rust
4143 use regex_macro::load_regex_files;
44+load_regex_files!("tests/regex");
4245
4346 fn main() {
44- load_regex_files!("tests/regex");
4547 let events = EventRE::vec_from_file("tests/samples/events.txt").unwrap();
4648 println!("{}", serde_json::to_string_pretty(&events).unwrap());
4749 }
+1-1examples/event.rs
@@ -1,7 +1,7 @@
11 use regex_macro::load_regex_files;
2+load_regex_files!("tests/regex");
23
34 fn main() {
4- load_regex_files!("tests/regex");
55 let events = EventRE::vec_from_file("tests/samples/events.txt").unwrap();
66 println!("{}", serde_json::to_string_pretty(&events).unwrap());
77 }
+14-2src/lib.rs
@@ -145,7 +145,7 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream {
145145
146146 /// Extracts all regex matches for the given string
147147 /// 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> + '_ {
149149 let re = &RE.#ident;
150150 re.captures_iter(text)
151151 .map(|captures| {
@@ -155,7 +155,19 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream {
155155 #(#field_initializers),*
156156 }
157157 })
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())))
159171 }
160172
161173 /// Extracts all regex matches for the text contents of a given file
+9-3tests/test.rs
@@ -1,8 +1,14 @@
11 use regex_macro::load_regex_files;
2+load_regex_files!("./tests/regex");
23
34 #[test]
4-pub fn events() {
5- load_regex_files!("./tests/regex");
5+pub fn events_vec() {
66 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);
814 }