irongit

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

using markdown file for docs

huncholanehuncholaneauthored
parent 5423d78commit 4405def4b845c4dcd3c23ca0abb4b522e23ca5ffBrowse files

2 files changed, +383 -25

+382-0docs/load_regex_files.md
@@ -0,0 +1,382 @@
1+# load_regex_files
2+
3+Loads a folder filled with regular expression files ending with `.re`.
4+
5+All regex files are loaded with the `m` and `x` flags.
6+
7+- `m` (multi-line): `^` and `$` match the start and end of lines, not just the entire input.
8+- `x` (extended): allows whitespace and comments starting with `#` inside the regex.
9+
10+## Event Example
11+
12+### regex/event.re
13+
14+ ```text
15+# This will create a struct called EventRE with the fields start, end, ampm, and activity
16+
17+# Get the start
18+^(?<start>\d{1,2}:\d{1,2})
19+.
20+# Get the end time
21+(?<end>\d{1,2}:\d{1,2})
22+\s{0,2}
23+
24+# Get the AM or PM after the times
25+(?<ampm>[AMPM]{2})
26+.
27+# Get the activity string
28+(?<activity>.+)
29+```
30+
31+### samples/events.txt
32+
33+```text
34+๐Ÿง  Focused Dev + Growth Schedule
35+
36+Time Activity
37+7:30โ€“8:00 AM Wake up + hydrate + light stretch
38+8:00โ€“8:45 AM Gym (weights or intervals)
39+9:00โ€“9:30 AM Breakfast + cooldown + planning
40+9:30โ€“12:00PM Deep coding (core project)
41+12:00โ€“12:45PM Lunch + decompress
42+12:45โ€“2:30PM Coding or meetings if needed
43+2:30โ€“3:00 PM Break or walk
44+3:00โ€“5:00 PM Learning block (Rust, AI, etc)
45+5:00โ€“6:00 PM Chill / side project / cleanup
46+6:00โ€“7:00 PM Dinner + relax
47+7:00โ€“9:00 PM Optional: build/test/hack ideas
48+9:00โ€“10:00 PM Wind down (read, steam room, etc)
49+10:00โ€“10:30PM Journal + sleep prep
50+```
51+
52+### examples/event.rs
53+
54+```rust
55+use regex_folder_macro::load_regex_files;
56+
57+load_regex_files!("regex");
58+
59+fn main() {
60+ let events = EventRE::vec_from_file("samples/events.txt").unwrap();
61+ println!("{}", serde_json::to_string_pretty(&events).unwrap());
62+}
63+```
64+
65+### Results
66+
67+```json
68+[
69+ {
70+ "start_pos": 50,
71+ "end_pos": 98,
72+ "start": {
73+ "start_pos": 50,
74+ "end_pos": 54,
75+ "val": "7:30"
76+ },
77+ "end": {
78+ "start_pos": 57,
79+ "end_pos": 61,
80+ "val": "8:00"
81+ },
82+ "ampm": {
83+ "start_pos": 62,
84+ "end_pos": 64,
85+ "val": "AM"
86+ },
87+ "activity": {
88+ "start_pos": 65,
89+ "end_pos": 98,
90+ "val": "Wake up + hydrate + light stretch"
91+ }
92+ },
93+ {
94+ "start_pos": 99,
95+ "end_pos": 140,
96+ "start": {
97+ "start_pos": 99,
98+ "end_pos": 103,
99+ "val": "8:00"
100+ },
101+ "end": {
102+ "start_pos": 106,
103+ "end_pos": 110,
104+ "val": "8:45"
105+ },
106+ "ampm": {
107+ "start_pos": 111,
108+ "end_pos": 113,
109+ "val": "AM"
110+ },
111+ "activity": {
112+ "start_pos": 114,
113+ "end_pos": 140,
114+ "val": "Gym (weights or intervals)"
115+ }
116+ },
117+ {
118+ "start_pos": 141,
119+ "end_pos": 187,
120+ "start": {
121+ "start_pos": 141,
122+ "end_pos": 145,
123+ "val": "9:00"
124+ },
125+ "end": {
126+ "start_pos": 148,
127+ "end_pos": 152,
128+ "val": "9:30"
129+ },
130+ "ampm": {
131+ "start_pos": 153,
132+ "end_pos": 155,
133+ "val": "AM"
134+ },
135+ "activity": {
136+ "start_pos": 156,
137+ "end_pos": 187,
138+ "val": "Breakfast + cooldown + planning"
139+ }
140+ },
141+ {
142+ "start_pos": 188,
143+ "end_pos": 229,
144+ "start": {
145+ "start_pos": 188,
146+ "end_pos": 192,
147+ "val": "9:30"
148+ },
149+ "end": {
150+ "start_pos": 195,
151+ "end_pos": 200,
152+ "val": "12:00"
153+ },
154+ "ampm": {
155+ "start_pos": 200,
156+ "end_pos": 202,
157+ "val": "PM"
158+ },
159+ "activity": {
160+ "start_pos": 203,
161+ "end_pos": 229,
162+ "val": "Deep coding (core project)"
163+ }
164+ },
165+ {
166+ "start_pos": 230,
167+ "end_pos": 264,
168+ "start": {
169+ "start_pos": 230,
170+ "end_pos": 235,
171+ "val": "12:00"
172+ },
173+ "end": {
174+ "start_pos": 238,
175+ "end_pos": 243,
176+ "val": "12:45"
177+ },
178+ "ampm": {
179+ "start_pos": 243,
180+ "end_pos": 245,
181+ "val": "PM"
182+ },
183+ "activity": {
184+ "start_pos": 246,
185+ "end_pos": 264,
186+ "val": "Lunch + decompress"
187+ }
188+ },
189+ {
190+ "start_pos": 265,
191+ "end_pos": 308,
192+ "start": {
193+ "start_pos": 265,
194+ "end_pos": 270,
195+ "val": "12:45"
196+ },
197+ "end": {
198+ "start_pos": 273,
199+ "end_pos": 277,
200+ "val": "2:30"
201+ },
202+ "ampm": {
203+ "start_pos": 277,
204+ "end_pos": 279,
205+ "val": "PM"
206+ },
207+ "activity": {
208+ "start_pos": 280,
209+ "end_pos": 308,
210+ "val": "Coding or meetings if needed"
211+ }
212+ },
213+ {
214+ "start_pos": 309,
215+ "end_pos": 337,
216+ "start": {
217+ "start_pos": 309,
218+ "end_pos": 313,
219+ "val": "2:30"
220+ },
221+ "end": {
222+ "start_pos": 316,
223+ "end_pos": 320,
224+ "val": "3:00"
225+ },
226+ "ampm": {
227+ "start_pos": 321,
228+ "end_pos": 323,
229+ "val": "PM"
230+ },
231+ "activity": {
232+ "start_pos": 324,
233+ "end_pos": 337,
234+ "val": "Break or walk"
235+ }
236+ },
237+ {
238+ "start_pos": 338,
239+ "end_pos": 383,
240+ "start": {
241+ "start_pos": 338,
242+ "end_pos": 342,
243+ "val": "3:00"
244+ },
245+ "end": {
246+ "start_pos": 345,
247+ "end_pos": 349,
248+ "val": "5:00"
249+ },
250+ "ampm": {
251+ "start_pos": 350,
252+ "end_pos": 352,
253+ "val": "PM"
254+ },
255+ "activity": {
256+ "start_pos": 353,
257+ "end_pos": 383,
258+ "val": "Learning block (Rust, AI, etc)"
259+ }
260+ },
261+ {
262+ "start_pos": 384,
263+ "end_pos": 429,
264+ "start": {
265+ "start_pos": 384,
266+ "end_pos": 388,
267+ "val": "5:00"
268+ },
269+ "end": {
270+ "start_pos": 391,
271+ "end_pos": 395,
272+ "val": "6:00"
273+ },
274+ "ampm": {
275+ "start_pos": 396,
276+ "end_pos": 398,
277+ "val": "PM"
278+ },
279+ "activity": {
280+ "start_pos": 399,
281+ "end_pos": 429,
282+ "val": "Chill / side project / cleanup"
283+ }
284+ },
285+ {
286+ "start_pos": 430,
287+ "end_pos": 459,
288+ "start": {
289+ "start_pos": 430,
290+ "end_pos": 434,
291+ "val": "6:00"
292+ },
293+ "end": {
294+ "start_pos": 437,
295+ "end_pos": 441,
296+ "val": "7:00"
297+ },
298+ "ampm": {
299+ "start_pos": 442,
300+ "end_pos": 444,
301+ "val": "PM"
302+ },
303+ "activity": {
304+ "start_pos": 445,
305+ "end_pos": 459,
306+ "val": "Dinner + relax"
307+ }
308+ },
309+ {
310+ "start_pos": 460,
311+ "end_pos": 506,
312+ "start": {
313+ "start_pos": 460,
314+ "end_pos": 464,
315+ "val": "7:00"
316+ },
317+ "end": {
318+ "start_pos": 467,
319+ "end_pos": 471,
320+ "val": "9:00"
321+ },
322+ "ampm": {
323+ "start_pos": 472,
324+ "end_pos": 474,
325+ "val": "PM"
326+ },
327+ "activity": {
328+ "start_pos": 475,
329+ "end_pos": 506,
330+ "val": "Optional: build/test/hack ideas"
331+ }
332+ },
333+ {
334+ "start_pos": 507,
335+ "end_pos": 556,
336+ "start": {
337+ "start_pos": 507,
338+ "end_pos": 511,
339+ "val": "9:00"
340+ },
341+ "end": {
342+ "start_pos": 514,
343+ "end_pos": 519,
344+ "val": "10:00"
345+ },
346+ "ampm": {
347+ "start_pos": 520,
348+ "end_pos": 522,
349+ "val": "PM"
350+ },
351+ "activity": {
352+ "start_pos": 523,
353+ "end_pos": 556,
354+ "val": "Wind down (read, steam room, etc)"
355+ }
356+ },
357+ {
358+ "start_pos": 557,
359+ "end_pos": 593,
360+ "start": {
361+ "start_pos": 557,
362+ "end_pos": 562,
363+ "val": "10:00"
364+ },
365+ "end": {
366+ "start_pos": 565,
367+ "end_pos": 570,
368+ "val": "10:30"
369+ },
370+ "ampm": {
371+ "start_pos": 570,
372+ "end_pos": 572,
373+ "val": "PM"
374+ },
375+ "activity": {
376+ "start_pos": 573,
377+ "end_pos": 593,
378+ "val": "Journal + sleep prep"
379+ }
380+ }
381+]
382+```
+1-25src/lib.rs
@@ -5,31 +5,7 @@ use std::fs;
55 use syn::{parse_macro_input, Ident, LitStr};
66
77 #[proc_macro]
8-/// Loads a folder filled with regular expression files ending with `.re`.
9-///
10-/// All regex files are loaded with the `m` and `x` flags.
11-///
12-/// - `m` (multi-line): `^` and `$` match the start and end of lines, not just the entire input.
13-/// - `x` (extended): allows whitespace and comments starting with `#` inside the regex.
14-///
15-/// Event Example:
16-/// ```text
17-/// ./
18-/// # This will create a struct called EventRE with the fields start, end, ampm, and activity
19-///
20-/// # Get the start
21-/// ^(?<start>\d{1,2}:\d{1,2})
22-/// .
23-/// # Get the end time
24-/// (?<end>\d{1,2}:\d{1,2})
25-/// \s{0,2}
26-///
27-/// # Get the AM or PM after the times
28-/// (?<ampm>[AMPM]{2})
29-/// .
30-/// # Get the activity string
31-/// (?<activity>.+)
32-/// ```
8+#[doc = include_str!("../docs/load_regex_files.md")]
339 pub fn load_regex_files(input: TokenStream) -> TokenStream {
3410 let folder_path = parse_macro_input!(input as LitStr).value();
3511