A rust macro that allows regular expression creation through independent files. Allows fully typed named groups.
using buffers
2 files changed, +14 -12
+12-11src/lib.rs
| @@ -125,10 +125,10 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /// Gets the raw captures from the contents of a file |
| 128 | - pub fn captures_from_file<'a>(buffer: &'a mut String, filename: &str) -> Result<Option<regex::Captures<'a>>, std::io::Error> { | |
| 129 | - buffer.clear(); | |
| 130 | - buffer.push_str(&std::fs::read_to_string(filename)?); | |
| 131 | - Ok(Self::captures(buffer)) | |
| 128 | + pub fn captures_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<Option<regex::Captures<'a>>, std::io::Error> { | |
| 129 | + buf.clear(); | |
| 130 | + buf.push_str(&std::fs::read_to_string(filename)?); | |
| 131 | + Ok(Self::captures(buf)) | |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /// Gets the raw captures iter |
| @@ -138,10 +138,10 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /// Gets the raw captures iter from the contents of a file |
| 141 | - pub fn captures_iter_from_file<'a>(buffer: &'a mut String, filename: &str) -> Result<impl Iterator<Item = regex::Captures<'a>> + 'a, std::io::Error> { | |
| 142 | - buffer.clear(); | |
| 143 | - buffer.push_str(&std::fs::read_to_string(filename)?); | |
| 144 | - Ok(Self::captures_iter(buffer)) | |
| 141 | + pub fn captures_iter_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<impl Iterator<Item = regex::Captures<'a>> + 'a, std::io::Error> { | |
| 142 | + buf.clear(); | |
| 143 | + buf.push_str(&std::fs::read_to_string(filename)?); | |
| 144 | + Ok(Self::captures_iter(buf)) | |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /// Extracts the first regex match for the given string |
| @@ -188,9 +188,10 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 188 | 188 | |
| 189 | 189 | /// Extracts all regex matches for the text contents of a given file |
| 190 | 190 | /// Each match contains a `start_pos`, `end_pos`, and each field for the given class contains `start_pos`, `end_pos`, and `val` |
| 191 | - pub fn iter_from_file(filename: &str) -> Result<impl Iterator<Item = Self> + '_, std::io::Error> { | |
| 192 | - let text = std::fs::read_to_string(filename)?; | |
| 193 | - Ok(Self::iter_from_str(Box::leak(text.into_boxed_str()))) | |
| 191 | + pub fn iter_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<impl Iterator<Item = Self> + 'a, std::io::Error> { | |
| 192 | + buf.clear(); | |
| 193 | + buf.push_str(&std::fs::read_to_string(filename)?); | |
| 194 | + Ok(Self::iter_from_str(buf)) | |
| 194 | 195 | } |
| 195 | 196 | |
| 196 | 197 | /// Extracts all regex matches for the text contents of a given file |
+2-1tests/test.rs
| @@ -10,7 +10,8 @@ pub fn events_vec() { | ||
| 10 | 10 | |
| 11 | 11 | #[test] |
| 12 | 12 | pub fn events_iter() { |
| 13 | - let events = EventRE::iter_from_file("./samples/events.txt").unwrap(); | |
| 13 | + let mut buf = String::new(); | |
| 14 | + let events = EventRE::iter_from_file(&mut buf, "./samples/events.txt").unwrap(); | |
| 14 | 15 | assert_eq!(events.count(), 13); |
| 15 | 16 | } |
| 16 | 17 |