irongit

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

388 lines7.5 KBMarkdown
1# regex_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 class with 4 main functions
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.
22
23The abstract struct 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.
24
25### Tips
26
27- Use struct style naming convention for `.re` files. i.e. `Event.re` rather than `event.re`
28
29### Benefits
30
31- Files load automatically into precompiled code with macros which allows rust-analyzer to use provide type hints.
32- Regular expressions can be split over multiple lines and use comments to improve readability for regular expressions.
33
34---
35
36## The events example
37
38### events.txt
39
40```text
41๐Ÿง  Focused Dev + Growth Schedule
42
43Time Activity
447:30โ€“8:00 AM Wake up + hydrate + light stretch
458:00โ€“8:45 AM Gym (weights or intervals)
469:00โ€“9:30 AM Breakfast + cooldown + planning
479:30โ€“12:00PM Deep coding (core project)
4812:00โ€“12:45PM Lunch + decompress
4912:45โ€“2:30PM Coding or meetings if needed
502:30โ€“3:00 PM Break or walk
513:00โ€“5:00 PM Learning block (Rust, AI, etc)
525:00โ€“6:00 PM Chill / side project / cleanup
536:00โ€“7:00 PM Dinner + relax
547:00โ€“9:00 PM Optional: build/test/hack ideas
559:00โ€“10:00 PM Wind down (read, steam room, etc)
5610:00โ€“10:30PM Journal + sleep prep
57```
58
59### Event.re
60
61```text
62(?<start>\d{1,2}:\d{1,2})
63.
64(?<end>\d{1,2}:\d{1,2})
65\s{0,2}
66(?<ampm>[AMPM]{2})
67.
68(?<activity>.+)
69```
70
71### Parsed items
72
73```json
74[
75 {
76 "start_pos": 50,
77 "end_pos": 98,
78 "start": {
79 "start_pos": 50,
80 "end_pos": 54,
81 "val": "7:30"
82 },
83 "end": {
84 "start_pos": 57,
85 "end_pos": 61,
86 "val": "8:00"
87 },
88 "ampm": {
89 "start_pos": 62,
90 "end_pos": 64,
91 "val": "AM"
92 },
93 "activity": {
94 "start_pos": 65,
95 "end_pos": 98,
96 "val": "Wake up + hydrate + light stretch"
97 }
98 },
99 {
100 "start_pos": 99,
101 "end_pos": 140,
102 "start": {
103 "start_pos": 99,
104 "end_pos": 103,
105 "val": "8:00"
106 },
107 "end": {
108 "start_pos": 106,
109 "end_pos": 110,
110 "val": "8:45"
111 },
112 "ampm": {
113 "start_pos": 111,
114 "end_pos": 113,
115 "val": "AM"
116 },
117 "activity": {
118 "start_pos": 114,
119 "end_pos": 140,
120 "val": "Gym (weights or intervals)"
121 }
122 },
123 {
124 "start_pos": 141,
125 "end_pos": 187,
126 "start": {
127 "start_pos": 141,
128 "end_pos": 145,
129 "val": "9:00"
130 },
131 "end": {
132 "start_pos": 148,
133 "end_pos": 152,
134 "val": "9:30"
135 },
136 "ampm": {
137 "start_pos": 153,
138 "end_pos": 155,
139 "val": "AM"
140 },
141 "activity": {
142 "start_pos": 156,
143 "end_pos": 187,
144 "val": "Breakfast + cooldown + planning"
145 }
146 },
147 {
148 "start_pos": 188,
149 "end_pos": 229,
150 "start": {
151 "start_pos": 188,
152 "end_pos": 192,
153 "val": "9:30"
154 },
155 "end": {
156 "start_pos": 195,
157 "end_pos": 200,
158 "val": "12:00"
159 },
160 "ampm": {
161 "start_pos": 200,
162 "end_pos": 202,
163 "val": "PM"
164 },
165 "activity": {
166 "start_pos": 203,
167 "end_pos": 229,
168 "val": "Deep coding (core project)"
169 }
170 },
171 {
172 "start_pos": 230,
173 "end_pos": 264,
174 "start": {
175 "start_pos": 230,
176 "end_pos": 235,
177 "val": "12:00"
178 },
179 "end": {
180 "start_pos": 238,
181 "end_pos": 243,
182 "val": "12:45"
183 },
184 "ampm": {
185 "start_pos": 243,
186 "end_pos": 245,
187 "val": "PM"
188 },
189 "activity": {
190 "start_pos": 246,
191 "end_pos": 264,
192 "val": "Lunch + decompress"
193 }
194 },
195 {
196 "start_pos": 265,
197 "end_pos": 308,
198 "start": {
199 "start_pos": 265,
200 "end_pos": 270,
201 "val": "12:45"
202 },
203 "end": {
204 "start_pos": 273,
205 "end_pos": 277,
206 "val": "2:30"
207 },
208 "ampm": {
209 "start_pos": 277,
210 "end_pos": 279,
211 "val": "PM"
212 },
213 "activity": {
214 "start_pos": 280,
215 "end_pos": 308,
216 "val": "Coding or meetings if needed"
217 }
218 },
219 {
220 "start_pos": 309,
221 "end_pos": 337,
222 "start": {
223 "start_pos": 309,
224 "end_pos": 313,
225 "val": "2:30"
226 },
227 "end": {
228 "start_pos": 316,
229 "end_pos": 320,
230 "val": "3:00"
231 },
232 "ampm": {
233 "start_pos": 321,
234 "end_pos": 323,
235 "val": "PM"
236 },
237 "activity": {
238 "start_pos": 324,
239 "end_pos": 337,
240 "val": "Break or walk"
241 }
242 },
243 {
244 "start_pos": 338,
245 "end_pos": 383,
246 "start": {
247 "start_pos": 338,
248 "end_pos": 342,
249 "val": "3:00"
250 },
251 "end": {
252 "start_pos": 345,
253 "end_pos": 349,
254 "val": "5:00"
255 },
256 "ampm": {
257 "start_pos": 350,
258 "end_pos": 352,
259 "val": "PM"
260 },
261 "activity": {
262 "start_pos": 353,
263 "end_pos": 383,
264 "val": "Learning block (Rust, AI, etc)"
265 }
266 },
267 {
268 "start_pos": 384,
269 "end_pos": 429,
270 "start": {
271 "start_pos": 384,
272 "end_pos": 388,
273 "val": "5:00"
274 },
275 "end": {
276 "start_pos": 391,
277 "end_pos": 395,
278 "val": "6:00"
279 },
280 "ampm": {
281 "start_pos": 396,
282 "end_pos": 398,
283 "val": "PM"
284 },
285 "activity": {
286 "start_pos": 399,
287 "end_pos": 429,
288 "val": "Chill / side project / cleanup"
289 }
290 },
291 {
292 "start_pos": 430,
293 "end_pos": 459,
294 "start": {
295 "start_pos": 430,
296 "end_pos": 434,
297 "val": "6:00"
298 },
299 "end": {
300 "start_pos": 437,
301 "end_pos": 441,
302 "val": "7:00"
303 },
304 "ampm": {
305 "start_pos": 442,
306 "end_pos": 444,
307 "val": "PM"
308 },
309 "activity": {
310 "start_pos": 445,
311 "end_pos": 459,
312 "val": "Dinner + relax"
313 }
314 },
315 {
316 "start_pos": 460,
317 "end_pos": 506,
318 "start": {
319 "start_pos": 460,
320 "end_pos": 464,
321 "val": "7:00"
322 },
323 "end": {
324 "start_pos": 467,
325 "end_pos": 471,
326 "val": "9:00"
327 },
328 "ampm": {
329 "start_pos": 472,
330 "end_pos": 474,
331 "val": "PM"
332 },
333 "activity": {
334 "start_pos": 475,
335 "end_pos": 506,
336 "val": "Optional: build/test/hack ideas"
337 }
338 },
339 {
340 "start_pos": 507,
341 "end_pos": 556,
342 "start": {
343 "start_pos": 507,
344 "end_pos": 511,
345 "val": "9:00"
346 },
347 "end": {
348 "start_pos": 514,
349 "end_pos": 519,
350 "val": "10:00"
351 },
352 "ampm": {
353 "start_pos": 520,
354 "end_pos": 522,
355 "val": "PM"
356 },
357 "activity": {
358 "start_pos": 523,
359 "end_pos": 556,
360 "val": "Wind down (read, steam room, etc)"
361 }
362 },
363 {
364 "start_pos": 557,
365 "end_pos": 593,
366 "start": {
367 "start_pos": 557,
368 "end_pos": 562,
369 "val": "10:00"
370 },
371 "end": {
372 "start_pos": 565,
373 "end_pos": 570,
374 "val": "10:30"
375 },
376 "ampm": {
377 "start_pos": 570,
378 "end_pos": 572,
379 "val": "PM"
380 },
381 "activity": {
382 "start_pos": 573,
383 "end_pos": 593,
384 "val": "Journal + sleep prep"
385 }
386 }
387]
388```