irongit

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

402 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;
44load_regex_files!("tests/regex");
45
46fn main() {
47 let events = EventRE::vec_from_file("tests/samples/events.txt").unwrap();
48 println!("{}", serde_json::to_string_pretty(&events).unwrap());
49}
50```
51
52### events.txt
53
54```text
55๐Ÿง  Focused Dev + Growth Schedule
56
57Time Activity
587:30โ€“8:00 AM Wake up + hydrate + light stretch
598:00โ€“8:45 AM Gym (weights or intervals)
609:00โ€“9:30 AM Breakfast + cooldown + planning
619:30โ€“12:00PM Deep coding (core project)
6212:00โ€“12:45PM Lunch + decompress
6312:45โ€“2:30PM Coding or meetings if needed
642:30โ€“3:00 PM Break or walk
653:00โ€“5:00 PM Learning block (Rust, AI, etc)
665:00โ€“6:00 PM Chill / side project / cleanup
676:00โ€“7:00 PM Dinner + relax
687:00โ€“9:00 PM Optional: build/test/hack ideas
699:00โ€“10:00 PM Wind down (read, steam room, etc)
7010:00โ€“10:30PM Journal + sleep prep
71```
72
73### Event.re
74
75```text
76(?<start>\d{1,2}:\d{1,2})
77.
78(?<end>\d{1,2}:\d{1,2})
79\s{0,2}
80(?<ampm>[AMPM]{2})
81.
82(?<activity>.+)
83```
84
85### Parsed items
86
87```json
88[
89 {
90 "start_pos": 50,
91 "end_pos": 98,
92 "start": {
93 "start_pos": 50,
94 "end_pos": 54,
95 "val": "7:30"
96 },
97 "end": {
98 "start_pos": 57,
99 "end_pos": 61,
100 "val": "8:00"
101 },
102 "ampm": {
103 "start_pos": 62,
104 "end_pos": 64,
105 "val": "AM"
106 },
107 "activity": {
108 "start_pos": 65,
109 "end_pos": 98,
110 "val": "Wake up + hydrate + light stretch"
111 }
112 },
113 {
114 "start_pos": 99,
115 "end_pos": 140,
116 "start": {
117 "start_pos": 99,
118 "end_pos": 103,
119 "val": "8:00"
120 },
121 "end": {
122 "start_pos": 106,
123 "end_pos": 110,
124 "val": "8:45"
125 },
126 "ampm": {
127 "start_pos": 111,
128 "end_pos": 113,
129 "val": "AM"
130 },
131 "activity": {
132 "start_pos": 114,
133 "end_pos": 140,
134 "val": "Gym (weights or intervals)"
135 }
136 },
137 {
138 "start_pos": 141,
139 "end_pos": 187,
140 "start": {
141 "start_pos": 141,
142 "end_pos": 145,
143 "val": "9:00"
144 },
145 "end": {
146 "start_pos": 148,
147 "end_pos": 152,
148 "val": "9:30"
149 },
150 "ampm": {
151 "start_pos": 153,
152 "end_pos": 155,
153 "val": "AM"
154 },
155 "activity": {
156 "start_pos": 156,
157 "end_pos": 187,
158 "val": "Breakfast + cooldown + planning"
159 }
160 },
161 {
162 "start_pos": 188,
163 "end_pos": 229,
164 "start": {
165 "start_pos": 188,
166 "end_pos": 192,
167 "val": "9:30"
168 },
169 "end": {
170 "start_pos": 195,
171 "end_pos": 200,
172 "val": "12:00"
173 },
174 "ampm": {
175 "start_pos": 200,
176 "end_pos": 202,
177 "val": "PM"
178 },
179 "activity": {
180 "start_pos": 203,
181 "end_pos": 229,
182 "val": "Deep coding (core project)"
183 }
184 },
185 {
186 "start_pos": 230,
187 "end_pos": 264,
188 "start": {
189 "start_pos": 230,
190 "end_pos": 235,
191 "val": "12:00"
192 },
193 "end": {
194 "start_pos": 238,
195 "end_pos": 243,
196 "val": "12:45"
197 },
198 "ampm": {
199 "start_pos": 243,
200 "end_pos": 245,
201 "val": "PM"
202 },
203 "activity": {
204 "start_pos": 246,
205 "end_pos": 264,
206 "val": "Lunch + decompress"
207 }
208 },
209 {
210 "start_pos": 265,
211 "end_pos": 308,
212 "start": {
213 "start_pos": 265,
214 "end_pos": 270,
215 "val": "12:45"
216 },
217 "end": {
218 "start_pos": 273,
219 "end_pos": 277,
220 "val": "2:30"
221 },
222 "ampm": {
223 "start_pos": 277,
224 "end_pos": 279,
225 "val": "PM"
226 },
227 "activity": {
228 "start_pos": 280,
229 "end_pos": 308,
230 "val": "Coding or meetings if needed"
231 }
232 },
233 {
234 "start_pos": 309,
235 "end_pos": 337,
236 "start": {
237 "start_pos": 309,
238 "end_pos": 313,
239 "val": "2:30"
240 },
241 "end": {
242 "start_pos": 316,
243 "end_pos": 320,
244 "val": "3:00"
245 },
246 "ampm": {
247 "start_pos": 321,
248 "end_pos": 323,
249 "val": "PM"
250 },
251 "activity": {
252 "start_pos": 324,
253 "end_pos": 337,
254 "val": "Break or walk"
255 }
256 },
257 {
258 "start_pos": 338,
259 "end_pos": 383,
260 "start": {
261 "start_pos": 338,
262 "end_pos": 342,
263 "val": "3:00"
264 },
265 "end": {
266 "start_pos": 345,
267 "end_pos": 349,
268 "val": "5:00"
269 },
270 "ampm": {
271 "start_pos": 350,
272 "end_pos": 352,
273 "val": "PM"
274 },
275 "activity": {
276 "start_pos": 353,
277 "end_pos": 383,
278 "val": "Learning block (Rust, AI, etc)"
279 }
280 },
281 {
282 "start_pos": 384,
283 "end_pos": 429,
284 "start": {
285 "start_pos": 384,
286 "end_pos": 388,
287 "val": "5:00"
288 },
289 "end": {
290 "start_pos": 391,
291 "end_pos": 395,
292 "val": "6:00"
293 },
294 "ampm": {
295 "start_pos": 396,
296 "end_pos": 398,
297 "val": "PM"
298 },
299 "activity": {
300 "start_pos": 399,
301 "end_pos": 429,
302 "val": "Chill / side project / cleanup"
303 }
304 },
305 {
306 "start_pos": 430,
307 "end_pos": 459,
308 "start": {
309 "start_pos": 430,
310 "end_pos": 434,
311 "val": "6:00"
312 },
313 "end": {
314 "start_pos": 437,
315 "end_pos": 441,
316 "val": "7:00"
317 },
318 "ampm": {
319 "start_pos": 442,
320 "end_pos": 444,
321 "val": "PM"
322 },
323 "activity": {
324 "start_pos": 445,
325 "end_pos": 459,
326 "val": "Dinner + relax"
327 }
328 },
329 {
330 "start_pos": 460,
331 "end_pos": 506,
332 "start": {
333 "start_pos": 460,
334 "end_pos": 464,
335 "val": "7:00"
336 },
337 "end": {
338 "start_pos": 467,
339 "end_pos": 471,
340 "val": "9:00"
341 },
342 "ampm": {
343 "start_pos": 472,
344 "end_pos": 474,
345 "val": "PM"
346 },
347 "activity": {
348 "start_pos": 475,
349 "end_pos": 506,
350 "val": "Optional: build/test/hack ideas"
351 }
352 },
353 {
354 "start_pos": 507,
355 "end_pos": 556,
356 "start": {
357 "start_pos": 507,
358 "end_pos": 511,
359 "val": "9:00"
360 },
361 "end": {
362 "start_pos": 514,
363 "end_pos": 519,
364 "val": "10:00"
365 },
366 "ampm": {
367 "start_pos": 520,
368 "end_pos": 522,
369 "val": "PM"
370 },
371 "activity": {
372 "start_pos": 523,
373 "end_pos": 556,
374 "val": "Wind down (read, steam room, etc)"
375 }
376 },
377 {
378 "start_pos": 557,
379 "end_pos": 593,
380 "start": {
381 "start_pos": 557,
382 "end_pos": 562,
383 "val": "10:00"
384 },
385 "end": {
386 "start_pos": 565,
387 "end_pos": 570,
388 "val": "10:30"
389 },
390 "ampm": {
391 "start_pos": 570,
392 "end_pos": 572,
393 "val": "PM"
394 },
395 "activity": {
396 "start_pos": 573,
397 "end_pos": 593,
398 "val": "Journal + sleep prep"
399 }
400 }
401]
402```