You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Statically-checked regex parsing into structs/enums.
7
7
8
8
This avoids common regex pitfalls like
9
9
10
10
- Off by one capture indexes
11
11
- Trying to get nonexistent captures
12
12
- Desync of capture names in regex and the names used to fetch fields
13
13
14
-
(Note this isn't like serde: you start with a regex, then write a structto match the regex. It doesn't work on arbitrary structures)
14
+
Note: This isn't like serde in that it doesn't work on arbitrary structs/enums. The struct/enum definition must be written to match the regex.
15
15
16
16
# Installation
17
17
@@ -35,27 +35,53 @@ struct KV {
35
35
let m = KV::from_str("hi: 39393")?;
36
36
```
37
37
38
-
The `structre::Error::Field` result only occurs if a field's `from_str` method fails - if all of your fields are strings, you can only get `Error::NoMatch`.
38
+
`from_str` returns a result with error type `structre::Error`. The `structre::Error::Field` result only occurs if a field's `from_str` method fails - if all of your fields are strings, you can only get `structre::Error::NoMatch`.
39
39
40
40
# Supported structures
41
41
42
42
Structs and enums both work, although there are some slight nuances.
43
43
44
44
- In structures:
45
45
46
-
All captures must correspond to a field. Named captures correspond to named fields, unnamed captures correspond to unnamed fields (ex: tuple elements). Repetitions, `?`, and `|` will make a capture optional, and the corresponding field must also be optional.
46
+
All captures must correspond to a field. Named captures correspond to named fields, unnamed captures correspond to unnamed fields (ex: tuple elements). Repetitions, `?`, and `|` will make a capture optional so the corresponding field must also be optional.
47
47
48
48
- In enums:
49
49
50
50
All variants must either
51
51
52
-
- Have at least one non-optional named field where the name matches a named capture
52
+
- Have at least one non-optional uniquely-named field where the name matches a named capture:
53
53
54
-
- Be a 1-tuple with a non-optional type. In this case, the variant name must match a named capture
54
+
Ex:
55
55
56
-
The presence of the matching named capture in the result will be used to determine which variant was parsed.
56
+
```rust
57
+
#[structre("(?<a_field>.*)|(?<b_field>.*)")]
58
+
enumAOrB {
59
+
A {
60
+
a_field:String,
61
+
},
62
+
B {
63
+
b_field:String,
64
+
}
65
+
}
66
+
```
57
67
58
-
Generally speaking the following types are suppored:
Non-unicode parsing isn't currently supported. I couldn't find an ascii float parsing library. If this is important and you have a vision of how it could work please raise an issue!
Originally I made the regex compilation manual and explicit, but this made the ergonomics much worse (managing parsers) and prevented things like implementing `FromStr`. In `0.1.0` I changed it to statically instantiate the regex. I'd be open to making this configurable in the future, either having an option to manually manage the compiled regex or else compiling on every parse for rarely used regexes.
0 commit comments