irongit

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

403 lines8.0 KBMarkdown
1# regex_folder_macro
2
3Allows for named groups in regular expressions to be generated from special files ending in `.re`.
4
5---
6
7## Usage
8
9### Initialization
10
11- Call the `load_regex_files!` macro on the directory that contains regex files.
12- Each file is loaded with the `m` and `x` flags
13 - `m` (multiline): `^` and `$` match the start and end of lines, not just the entire input.
14 - `x` (extended): allows whitespace and comments starting with `#` inside the regex.
15
16### Each regex file is a structure with 4 main implementations
17
181. `from_str(text: &str)` Finds a single match within the given string.
192. `from_file(filename: &str)` Finds a single match after opening the given filename.
203. `vec_from_str(text: &str)` Extracts all the matches in a given string.
214. `vec_from_file(filename: &str)` Extracts all the matches after opening the given filename.
225. `iter_from_str(text: &str)` Extracts all the matches in a given string.
236. `iter_from_file(filename: &str)` Extracts all the matches after opening the given filename.
24
25The abstract structure for matches within the regex_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
27### Tips
28
29- Use struct style naming convention for `.re` files. i.e. `Event.re` rather than `event.re`
30
31### Benefits
32
33- Files load automatically into precompiled code with macros which allows rust-analyzer to use provide type hints.
34- Regular expressions can be split over multiple lines and use comments to improve readability for regular expressions.
35
36---
37
38## The events example
39
40### event.rs
41
42```rust
43use regex_macro::load_regex_files;
44// Loads the regular expressions within the folder named regex
45load_regex_files!("regex");
46
47fn main() {
48 let events = EventRE::vec_from_file("tests/samples/events.txt").unwrap();
49 println!("{}", serde_json::to_string_pretty(&events).unwrap());
50}
51```
52
53### events.txt
54
55```text
56๐Ÿง  Focused Dev + Growth Schedule
57
58Time Activity
597:30โ€“8:00 AM Wake up + hydrate + light stretch
608:00โ€“8:45 AM Gym (weights or intervals)
619:00โ€“9:30 AM Breakfast + cooldown + planning
629:30โ€“12:00PM Deep coding (core project)
6312:00โ€“12:45PM Lunch + decompress
6412:45โ€“2:30PM Coding or meetings if needed
652:30โ€“3:00 PM Break or walk
663:00โ€“5:00 PM Learning block (Rust, AI, etc)
675:00โ€“6:00 PM Chill / side project / cleanup
686:00โ€“7:00 PM Dinner + relax
697:00โ€“9:00 PM Optional: build/test/hack ideas
709:00โ€“10:00 PM Wind down (read, steam room, etc)
7110:00โ€“10:30PM Journal + sleep prep
72```
73
74### Event.re
75
76```text
77(?<start>\d{1,2}:\d{1,2})
78.
79(?<end>\d{1,2}:\d{1,2})
80\s{0,2}
81(?<ampm>[AMPM]{2})
82.
83(?<activity>.+)
84```
85
86### Parsed items
87
88```json
89[
90 {
91 "start_pos": 50,
92 "end_pos": 98,
93 "start": {
94 "start_pos": 50,
95 "end_pos": 54,
96 "val": "7:30"
97 },
98 "end": {
99 "start_pos": 57,
100 "end_pos": 61,
101 "val": "8:00"
102 },
103 "ampm": {
104 "start_pos": 62,
105 "end_pos": 64,
106 "val": "AM"
107 },
108 "activity": {
109 "start_pos": 65,
110 "end_pos": 98,
111 "val": "Wake up + hydrate + light stretch"
112 }
113 },
114 {
115 "start_pos": 99,
116 "end_pos": 140,
117 "start": {
118 "start_pos": 99,
119 "end_pos": 103,
120 "val": "8:00"
121 },
122 "end": {
123 "start_pos": 106,
124 "end_pos": 110,
125 "val": "8:45"
126 },
127 "ampm": {
128 "start_pos": 111,
129 "end_pos": 113,
130 "val": "AM"
131 },
132 "activity": {
133 "start_pos": 114,
134 "end_pos": 140,
135 "val": "Gym (weights or intervals)"
136 }
137 },
138 {
139 "start_pos": 141,
140 "end_pos": 187,
141 "start": {
142 "start_pos": 141,
143 "end_pos": 145,
144 "val": "9:00"
145 },
146 "end": {
147 "start_pos": 148,
148 "end_pos": 152,
149 "val": "9:30"
150 },
151 "ampm": {
152 "start_pos": 153,
153 "end_pos": 155,
154 "val": "AM"
155 },
156 "activity": {
157 "start_pos": 156,
158 "end_pos": 187,
159 "val": "Breakfast + cooldown + planning"
160 }
161 },
162 {
163 "start_pos": 188,
164 "end_pos": 229,
165 "start": {
166 "start_pos": 188,
167 "end_pos": 192,
168 "val": "9:30"
169 },
170 "end": {
171 "start_pos": 195,
172 "end_pos": 200,
173 "val": "12:00"
174 },
175 "ampm": {
176 "start_pos": 200,
177 "end_pos": 202,
178 "val": "PM"
179 },
180 "activity": {
181 "start_pos": 203,
182 "end_pos": 229,
183 "val": "Deep coding (core project)"
184 }
185 },
186 {
187 "start_pos": 230,
188 "end_pos": 264,
189 "start": {
190 "start_pos": 230,
191 "end_pos": 235,
192 "val": "12:00"
193 },
194 "end": {
195 "start_pos": 238,
196 "end_pos": 243,
197 "val": "12:45"
198 },
199 "ampm": {
200 "start_pos": 243,
201 "end_pos": 245,
202 "val": "PM"
203 },
204 "activity": {
205 "start_pos": 246,
206 "end_pos": 264,
207 "val": "Lunch + decompress"
208 }
209 },
210 {
211 "start_pos": 265,
212 "end_pos": 308,
213 "start": {
214 "start_pos": 265,
215 "end_pos": 270,
216 "val": "12:45"
217 },
218 "end": {
219 "start_pos": 273,
220 "end_pos": 277,
221 "val": "2:30"
222 },
223 "ampm": {
224 "start_pos": 277,
225 "end_pos": 279,
226 "val": "PM"
227 },
228 "activity": {
229 "start_pos": 280,
230 "end_pos": 308,
231 "val": "Coding or meetings if needed"
232 }
233 },
234 {
235 "start_pos": 309,
236 "end_pos": 337,
237 "start": {
238 "start_pos": 309,
239 "end_pos": 313,
240 "val": "2:30"
241 },
242 "end": {
243 "start_pos": 316,
244 "end_pos": 320,
245 "val": "3:00"
246 },
247 "ampm": {
248 "start_pos": 321,
249 "end_pos": 323,
250 "val": "PM"
251 },
252 "activity": {
253 "start_pos": 324,
254 "end_pos": 337,
255 "val": "Break or walk"
256 }
257 },
258 {
259 "start_pos": 338,
260 "end_pos": 383,
261 "start": {
262 "start_pos": 338,
263 "end_pos": 342,
264 "val": "3:00"
265 },
266 "end": {
267 "start_pos": 345,
268 "end_pos": 349,
269 "val": "5:00"
270 },
271 "ampm": {
272 "start_pos": 350,
273 "end_pos": 352,
274 "val": "PM"
275 },
276 "activity": {
277 "start_pos": 353,
278 "end_pos": 383,
279 "val": "Learning block (Rust, AI, etc)"
280 }
281 },
282 {
283 "start_pos": 384,
284 "end_pos": 429,
285 "start": {
286 "start_pos": 384,
287 "end_pos": 388,
288 "val": "5:00"
289 },
290 "end": {
291 "start_pos": 391,
292 "end_pos": 395,
293 "val": "6:00"
294 },
295 "ampm": {
296 "start_pos": 396,
297 "end_pos": 398,
298 "val": "PM"
299 },
300 "activity": {
301 "start_pos": 399,
302 "end_pos": 429,
303 "val": "Chill / side project / cleanup"
304 }
305 },
306 {
307 "start_pos": 430,
308 "end_pos": 459,
309 "start": {
310 "start_pos": 430,
311 "end_pos": 434,
312 "val": "6:00"
313 },
314 "end": {
315 "start_pos": 437,
316 "end_pos": 441,
317 "val": "7:00"
318 },
319 "ampm": {
320 "start_pos": 442,
321 "end_pos": 444,
322 "val": "PM"
323 },
324 "activity": {
325 "start_pos": 445,
326 "end_pos": 459,
327 "val": "Dinner + relax"
328 }
329 },
330 {
331 "start_pos": 460,
332 "end_pos": 506,
333 "start": {
334 "start_pos": 460,
335 "end_pos": 464,
336 "val": "7:00"
337 },
338 "end": {
339 "start_pos": 467,
340 "end_pos": 471,
341 "val": "9:00"
342 },
343 "ampm": {
344 "start_pos": 472,
345 "end_pos": 474,
346 "val": "PM"
347 },
348 "activity": {
349 "start_pos": 475,
350 "end_pos": 506,
351 "val": "Optional: build/test/hack ideas"
352 }
353 },
354 {
355 "start_pos": 507,
356 "end_pos": 556,
357 "start": {
358 "start_pos": 507,
359 "end_pos": 511,
360 "val": "9:00"
361 },
362 "end": {
363 "start_pos": 514,
364 "end_pos": 519,
365 "val": "10:00"
366 },
367 "ampm": {
368 "start_pos": 520,
369 "end_pos": 522,
370 "val": "PM"
371 },
372 "activity": {
373 "start_pos": 523,
374 "end_pos": 556,
375 "val": "Wind down (read, steam room, etc)"
376 }
377 },
378 {
379 "start_pos": 557,
380 "end_pos": 593,
381 "start": {
382 "start_pos": 557,
383 "end_pos": 562,
384 "val": "10:00"
385 },
386 "end": {
387 "start_pos": 565,
388 "end_pos": 570,
389 "val": "10:30"
390 },
391 "ampm": {
392 "start_pos": 570,
393 "end_pos": 572,
394 "val": "PM"
395 },
396 "activity": {
397 "start_pos": 573,
398 "end_pos": 593,
399 "val": "Journal + sleep prep"
400 }
401 }
402]
403```