irongit

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

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