regex_folder_macro 0.1.6

Generate code from regular expressions inside a folder with files ending in .re.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# regex_folder_macro

Allows for named groups in regular expressions to be generated from special files ending in `.re`.

---

## Usage

### Initialization

- Call the `load_regex_files!` macro on the directory that contains regex files.
- Each file is loaded with the `m` and `x` flags
  - `m` (multiline): `^` and `$` match the start and end of lines, not just the entire input.
  - `x` (extended): allows whitespace and comments starting with `#` inside the regex.

### Each regex file is a structure with the following implementations

1. `from_str(text: &str)` Finds a single match within the given string.
2. `from_file(filename: &str)` Finds a single match after opening the given filename.
3. `vec_from_str(text: &str)` Extracts all the matches in a given string.
4. `vec_from_file(filename: &str)` Extracts all the matches after opening the given filename.
5. `iter_from_str(text: &str)` Extracts all the matches in a given string.
6. `iter_from_file(filename: &str)` Extracts all the matches after opening the given filename.

The abstract structure for matches within the regex_folder_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.

### Tips

- Use struct style naming convention for `.re` files. i.e. `Event.re` rather than `event.re`

### Benefits

- Files load automatically into precompiled code with macros which allows rust-analyzer to use provide type hints.
- Regular expressions can be split over multiple lines and use comments to improve readability for regular expressions.

---

## The events example

### event.rs

```rust
use regex_folder_macro::load_regex_files;
// Loads the regular expressions within the folder named regex
load_regex_files!("regex");

fn main() {
    let events = EventRE::vec_from_file("samples/events.txt").unwrap();
    println!("{}", serde_json::to_string_pretty(&events).unwrap());
}
```

### events.txt

```text
🧠 Focused Dev + Growth Schedule

Time Activity
7:30–8:00 AM Wake up + hydrate + light stretch
8:00–8:45 AM Gym (weights or intervals)
9:00–9:30 AM Breakfast + cooldown + planning
9:30–12:00PM Deep coding (core project)
12:00–12:45PM Lunch + decompress
12:45–2:30PM Coding or meetings if needed
2:30–3:00 PM Break or walk
3:00–5:00 PM Learning block (Rust, AI, etc)
5:00–6:00 PM Chill / side project / cleanup
6:00–7:00 PM Dinner + relax
7:00–9:00 PM Optional: build/test/hack ideas
9:00–10:00 PM Wind down (read, steam room, etc)
10:00–10:30PM Journal + sleep prep
```

### Event.re

```text
(?<start>\d{1,2}:\d{1,2})
.
(?<end>\d{1,2}:\d{1,2})
\s{0,2}
(?<ampm>[AMPM]{2})
.
(?<activity>.+)
```

### Parsed items

```json
[
  {
    "start_pos": 50,
    "end_pos": 98,
    "start": {
      "start_pos": 50,
      "end_pos": 54,
      "val": "7:30"
    },
    "end": {
      "start_pos": 57,
      "end_pos": 61,
      "val": "8:00"
    },
    "ampm": {
      "start_pos": 62,
      "end_pos": 64,
      "val": "AM"
    },
    "activity": {
      "start_pos": 65,
      "end_pos": 98,
      "val": "Wake up + hydrate + light stretch"
    }
  },
  {
    "start_pos": 99,
    "end_pos": 140,
    "start": {
      "start_pos": 99,
      "end_pos": 103,
      "val": "8:00"
    },
    "end": {
      "start_pos": 106,
      "end_pos": 110,
      "val": "8:45"
    },
    "ampm": {
      "start_pos": 111,
      "end_pos": 113,
      "val": "AM"
    },
    "activity": {
      "start_pos": 114,
      "end_pos": 140,
      "val": "Gym (weights or intervals)"
    }
  },
  {
    "start_pos": 141,
    "end_pos": 187,
    "start": {
      "start_pos": 141,
      "end_pos": 145,
      "val": "9:00"
    },
    "end": {
      "start_pos": 148,
      "end_pos": 152,
      "val": "9:30"
    },
    "ampm": {
      "start_pos": 153,
      "end_pos": 155,
      "val": "AM"
    },
    "activity": {
      "start_pos": 156,
      "end_pos": 187,
      "val": "Breakfast + cooldown + planning"
    }
  },
  {
    "start_pos": 188,
    "end_pos": 229,
    "start": {
      "start_pos": 188,
      "end_pos": 192,
      "val": "9:30"
    },
    "end": {
      "start_pos": 195,
      "end_pos": 200,
      "val": "12:00"
    },
    "ampm": {
      "start_pos": 200,
      "end_pos": 202,
      "val": "PM"
    },
    "activity": {
      "start_pos": 203,
      "end_pos": 229,
      "val": "Deep coding (core project)"
    }
  },
  {
    "start_pos": 230,
    "end_pos": 264,
    "start": {
      "start_pos": 230,
      "end_pos": 235,
      "val": "12:00"
    },
    "end": {
      "start_pos": 238,
      "end_pos": 243,
      "val": "12:45"
    },
    "ampm": {
      "start_pos": 243,
      "end_pos": 245,
      "val": "PM"
    },
    "activity": {
      "start_pos": 246,
      "end_pos": 264,
      "val": "Lunch + decompress"
    }
  },
  {
    "start_pos": 265,
    "end_pos": 308,
    "start": {
      "start_pos": 265,
      "end_pos": 270,
      "val": "12:45"
    },
    "end": {
      "start_pos": 273,
      "end_pos": 277,
      "val": "2:30"
    },
    "ampm": {
      "start_pos": 277,
      "end_pos": 279,
      "val": "PM"
    },
    "activity": {
      "start_pos": 280,
      "end_pos": 308,
      "val": "Coding or meetings if needed"
    }
  },
  {
    "start_pos": 309,
    "end_pos": 337,
    "start": {
      "start_pos": 309,
      "end_pos": 313,
      "val": "2:30"
    },
    "end": {
      "start_pos": 316,
      "end_pos": 320,
      "val": "3:00"
    },
    "ampm": {
      "start_pos": 321,
      "end_pos": 323,
      "val": "PM"
    },
    "activity": {
      "start_pos": 324,
      "end_pos": 337,
      "val": "Break or walk"
    }
  },
  {
    "start_pos": 338,
    "end_pos": 383,
    "start": {
      "start_pos": 338,
      "end_pos": 342,
      "val": "3:00"
    },
    "end": {
      "start_pos": 345,
      "end_pos": 349,
      "val": "5:00"
    },
    "ampm": {
      "start_pos": 350,
      "end_pos": 352,
      "val": "PM"
    },
    "activity": {
      "start_pos": 353,
      "end_pos": 383,
      "val": "Learning block (Rust, AI, etc)"
    }
  },
  {
    "start_pos": 384,
    "end_pos": 429,
    "start": {
      "start_pos": 384,
      "end_pos": 388,
      "val": "5:00"
    },
    "end": {
      "start_pos": 391,
      "end_pos": 395,
      "val": "6:00"
    },
    "ampm": {
      "start_pos": 396,
      "end_pos": 398,
      "val": "PM"
    },
    "activity": {
      "start_pos": 399,
      "end_pos": 429,
      "val": "Chill / side project / cleanup"
    }
  },
  {
    "start_pos": 430,
    "end_pos": 459,
    "start": {
      "start_pos": 430,
      "end_pos": 434,
      "val": "6:00"
    },
    "end": {
      "start_pos": 437,
      "end_pos": 441,
      "val": "7:00"
    },
    "ampm": {
      "start_pos": 442,
      "end_pos": 444,
      "val": "PM"
    },
    "activity": {
      "start_pos": 445,
      "end_pos": 459,
      "val": "Dinner + relax"
    }
  },
  {
    "start_pos": 460,
    "end_pos": 506,
    "start": {
      "start_pos": 460,
      "end_pos": 464,
      "val": "7:00"
    },
    "end": {
      "start_pos": 467,
      "end_pos": 471,
      "val": "9:00"
    },
    "ampm": {
      "start_pos": 472,
      "end_pos": 474,
      "val": "PM"
    },
    "activity": {
      "start_pos": 475,
      "end_pos": 506,
      "val": "Optional: build/test/hack ideas"
    }
  },
  {
    "start_pos": 507,
    "end_pos": 556,
    "start": {
      "start_pos": 507,
      "end_pos": 511,
      "val": "9:00"
    },
    "end": {
      "start_pos": 514,
      "end_pos": 519,
      "val": "10:00"
    },
    "ampm": {
      "start_pos": 520,
      "end_pos": 522,
      "val": "PM"
    },
    "activity": {
      "start_pos": 523,
      "end_pos": 556,
      "val": "Wind down (read, steam room, etc)"
    }
  },
  {
    "start_pos": 557,
    "end_pos": 593,
    "start": {
      "start_pos": 557,
      "end_pos": 562,
      "val": "10:00"
    },
    "end": {
      "start_pos": 565,
      "end_pos": 570,
      "val": "10:30"
    },
    "ampm": {
      "start_pos": 570,
      "end_pos": 572,
      "val": "PM"
    },
    "activity": {
      "start_pos": 573,
      "end_pos": 593,
      "val": "Journal + sleep prep"
    }
  }
]
```