irongit

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

add readme

huncholanehuncholaneauthored
parent b714491commit 1744669c3cb77fd925074cf98e59b1e8349cf6f6Browse files

2 files changed, +382 -4

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