A rust macro that allows regular expression creation through independent files. Allows fully typed named groups.
updated readme
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 | ||
| 15 | 15 | |
| 16 | 16 | ### Each regex file is a structure with the following implementations |
| 17 | 17 | |
| 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. | |
| 24 | 28 | |
| 25 | 29 | 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. |
| 26 | 30 | |
| @@ -30,9 +34,15 @@ The abstract structure for matches within the regex_folder_macro library contain | ||
| 30 | 34 | |
| 31 | 35 | ### Benefits |
| 32 | 36 | |
| 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. | |
| 34 | 38 | - Regular expressions can be split over multiple lines and use comments to improve readability for regular expressions. |
| 35 | 39 | |
| 40 | +### Breaking changes | |
| 41 | + | |
| 42 | +#### 0.0.6 -> 1.0.0 | |
| 43 | + | |
| 44 | +- Functions from files require a buffer | |
| 45 | + | |
| 36 | 46 | --- |
| 37 | 47 | |
| 38 | 48 | ## The events example |
+3-0src/lib.rs
| @@ -125,6 +125,7 @@ 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 | + /// Buffer is required because captures use references to the original text. | |
| 128 | 129 | pub fn captures_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<Option<regex::Captures<'a>>, std::io::Error> { |
| 129 | 130 | buf.clear(); |
| 130 | 131 | buf.push_str(&std::fs::read_to_string(filename)?); |
| @@ -138,6 +139,7 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 138 | 139 | } |
| 139 | 140 | |
| 140 | 141 | /// Gets the raw captures iter from the contents of a file |
| 142 | + /// Buffer is required because captures use references to the original text. | |
| 141 | 143 | 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 | 144 | buf.clear(); |
| 143 | 145 | buf.push_str(&std::fs::read_to_string(filename)?); |
| @@ -188,6 +190,7 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 188 | 190 | |
| 189 | 191 | /// Extracts all regex matches for the text contents of a given file |
| 190 | 192 | /// 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. | |
| 191 | 194 | pub fn iter_from_file<'a>(buf: &'a mut String, filename: &str) -> Result<impl Iterator<Item = Self> + 'a, std::io::Error> { |
| 192 | 195 | buf.clear(); |
| 193 | 196 | buf.push_str(&std::fs::read_to_string(filename)?); |
+7-7tests/test.rs
| @@ -23,22 +23,22 @@ pub fn first_event() { | ||
| 23 | 23 | |
| 24 | 24 | #[test] |
| 25 | 25 | 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") | |
| 28 | 28 | .unwrap() |
| 29 | 29 | .unwrap(); |
| 30 | 30 | assert_eq!( |
| 31 | 31 | captures.get(0).unwrap().as_str(), |
| 32 | 32 | "7:30–8:00 AM Wake up + hydrate + light stretch" |
| 33 | 33 | ); |
| 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); | |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | 39 | #[test] |
| 39 | 40 | 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(); | |
| 43 | 43 | assert_eq!(captures_iter.count(), 13); |
| 44 | 44 | } |