A rust macro that allows regular expression creation through independent files. Allows fully typed named groups.
added docstrings
1 file changed, +8 -0
+8-0src/lib.rs
| @@ -111,6 +111,8 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | impl #ident { |
| 114 | + /// Extracts the first regex match for the given string | |
| 115 | + /// This match contains `start_pos`, `end_pos`, and `val` | |
| 114 | 116 | pub fn from_str(text: &str) -> Option<Self> { |
| 115 | 117 | let re = &RE.#ident; |
| 116 | 118 | let captures = re.captures(text); |
| @@ -125,11 +127,15 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 125 | 127 | } |
| 126 | 128 | } |
| 127 | 129 | |
| 130 | + /// Extracts the first regex match for the text content of the given filename | |
| 131 | + /// This match contains `start_pos`, `end_pos`, and `val` | |
| 128 | 132 | pub fn from_file(filename: &str) -> Result<Option<Self>, std::io::Error> { |
| 129 | 133 | let text = std::fs::read_to_string(filename)?; |
| 130 | 134 | Ok(Self::from_str(&text)) |
| 131 | 135 | } |
| 132 | 136 | |
| 137 | + /// Extracts all regex matches for the given string | |
| 138 | + /// Each match contains a `start_pos`, `end_pos`, and each field for the given class contains `start_pos`, `end_pos`, and `val` | |
| 133 | 139 | pub fn vec_from_str(text: &str) -> Vec<Self> { |
| 134 | 140 | let re = &RE.#ident; |
| 135 | 141 | re.captures_iter(text) |
| @@ -143,6 +149,8 @@ pub fn load_regex_files(input: TokenStream) -> TokenStream { | ||
| 143 | 149 | .collect() |
| 144 | 150 | } |
| 145 | 151 | |
| 152 | + /// Extracts all regex matches for the text contents of a given file | |
| 153 | + /// Each match contains a `start_pos`, `end_pos`, and each field for the given class contains `start_pos`, `end_pos`, and `val` | |
| 146 | 154 | pub fn vec_from_file(filename: &str) -> Result<Vec<Self>, std::io::Error> { |
| 147 | 155 | let text = std::fs::read_to_string(filename)?; |
| 148 | 156 | Ok(Self::vec_from_str(&text)) |