irongit

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

using buffers

huncholanehuncholaneauthored
parent 72613d6commit c3ebb213a881ef72356a8c53ce17799298e3e7ccBrowse files

2 files changed, +14 -12

+12-11src/lib.rs
@@ -125,10 +125,10 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream {
125125 }
126126
127127 /// 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))
132132 }
133133
134134 /// Gets the raw captures iter
@@ -138,10 +138,10 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream {
138138 }
139139
140140 /// 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))
145145 }
146146
147147 /// Extracts the first regex match for the given string
@@ -188,9 +188,10 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream {
188188
189189 /// Extracts all regex matches for the text contents of a given file
190190 /// 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))
194195 }
195196
196197 /// Extracts all regex matches for the text contents of a given file
+2-1tests/test.rs
@@ -10,7 +10,8 @@ pub fn events_vec() {
1010
1111 #[test]
1212 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();
1415 assert_eq!(events.count(), 13);
1516 }
1617