irongit

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

updated readme

huncholanehuncholaneauthored
parent c3ebb21commit 3616694c409ebc9c77250d3dde6aeb282928a44aBrowse files

3 files changed, +27 -14

+17-7README.md
@@ -15,12 +15,16 @@ Allows for named groups in regular expressions to be generated from special file
1515
1616 ### Each regex file is a structure with the following implementations
1717
18-1. `from_str(text: &str)` Finds a single match within the given string.
19-2. `from_file(filename: &str)` Finds a single match after opening the given filename.
20-3. `vec_from_str(text: &str)` Extracts all the matches in a given string.
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.
18+1. `pub fn from_str(text: &str) -> Option<Self>` Finds a single precompiled struct within the given string.
19+2. `pub fn from_file(filename: &str) -> Result<Option<Self>, std::io::Error>` Finds a single precompiled struct after opening the given filename.
20+3. `pub fn vec_from_str(text: &str) -> Vec<Self>` Extracts all the precompiled structs in a given string.
21+4. `pub fn vec_from_file(filename: &str) -> Result<Vec<Self>, std::io::Error>` Extracts all the precompiled structs after opening the given filename.
22+5. `pub fn iter_from_str(text: &str) -> impl Iterator<Item = Self> + '_` Extracts all the precompiled structs in a given string.
23+6. `pub fn iter_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<impl Iterator<Item = Self> + 'a, std::io::Error>` Extracts all the precompiled structs after opening the given filename.
24+7. `pub fn captures(text:&str) -> Option<regex::Captures<'_>>` Extracts the first match as raw captures.
25+8. `pub fn captures_iter(text: &str) -> impl Iterator<Item = regex::Captures<'_>> + '_` Extracts an iterator of raw captures.
26+9. `pub fn captures_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<Option<regex::Captures<'a>>, std::io::Error>` Extracts the first match as raw captures from the given file.
27+10. `pub fn captures_iter_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<impl Iterator<Item = regex::Captures<'a>> + 'a, std::io::Error>` Extracts raw captures iter from contents of the given file.
2428
2529 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.
2630
@@ -30,9 +34,15 @@ The abstract structure for matches within the regex_folder_macro library contain
3034
3135 ### Benefits
3236
33-- Files load automatically into precompiled code with macros which allows rust-analyzer to use provide type hints.
37+- Files load automatically into precompiled struct with macros which allows rust-analyzer to use provide type hints.
3438 - Regular expressions can be split over multiple lines and use comments to improve readability for regular expressions.
3539
40+### Breaking changes
41+
42+#### 0.0.6 -> 1.0.0
43+
44+- Functions from files require a buffer
45+
3646 ---
3747
3848 ## The events example
+3-0src/lib.rs
@@ -125,6 +125,7 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream {
125125 }
126126
127127 /// Gets the raw captures from the contents of a file
128+ /// Buffer is required because captures use references to the original text.
128129 pub fn captures_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<Option<regex::Captures<'a>>, std::io::Error> {
129130 buf.clear();
130131 buf.push_str(&std::fs::read_to_string(filename)?);
@@ -138,6 +139,7 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream {
138139 }
139140
140141 /// Gets the raw captures iter from the contents of a file
142+ /// Buffer is required because captures use references to the original text.
141143 pub fn captures_iter_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<impl Iterator<Item = regex::Captures<'a>> + 'a, std::io::Error> {
142144 buf.clear();
143145 buf.push_str(&std::fs::read_to_string(filename)?);
@@ -188,6 +190,7 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream {
188190
189191 /// Extracts all regex matches for the text contents of a given file
190192 /// Each match contains a `start_pos`, `end_pos`, and each field for the given class contains `start_pos`, `end_pos`, and `val`
193+ /// Buffer is required because the iterator refers to the original text.
191194 pub fn iter_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<impl Iterator<Item = Self> + 'a, std::io::Error> {
192195 buf.clear();
193196 buf.push_str(&std::fs::read_to_string(filename)?);
+7-7tests/test.rs
@@ -23,22 +23,22 @@ pub fn first_event() {
2323
2424 #[test]
2525 pub fn first_event_captures() {
26- let mut content = String::new();
27- let captures = EventRE::captures_from_file(&mut content, "samples/events.txt")
26+ let mut buf = String::new();
27+ let captures = EventRE::captures_from_file(&mut buf, "samples/events.txt")
2828 .unwrap()
2929 .unwrap();
3030 assert_eq!(
3131 captures.get(0).unwrap().as_str(),
3232 "7:30–8:00 AM Wake up + hydrate + light stretch"
3333 );
34- assert_eq!(captures.name("end").unwrap().as_str(), "8:00");
35- assert_eq!(captures.name("end").unwrap().start(), 57);
34+ let end = captures.name("end").unwrap();
35+ assert_eq!(end.as_str(), "8:00");
36+ assert_eq!(end.start(), 57);
3637 }
3738
3839 #[test]
3940 pub fn event_captures_iter() {
40- let mut content = String::new();
41- let captures_iter =
42- EventRE::captures_iter_from_file(&mut content, "samples/events.txt").unwrap();
41+ let mut buf = String::new();
42+ let captures_iter = EventRE::captures_iter_from_file(&mut buf, "samples/events.txt").unwrap();
4343 assert_eq!(captures_iter.count(), 13);
4444 }