Skip to content

Commit 91aaeeb

Browse files
author
andrew
committed
Readme improvements
1 parent 94ac04e commit 91aaeeb

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

readme.md

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
<td><a href="https://docs.rs/structre"><img alt="docs.rs" src="https://img.shields.io/docsrs/structre"></td></a>
44
</tr></table>
55

6-
Statically-checked regex parsing into structs.
6+
Statically-checked regex parsing into structs/enums.
77

88
This avoids common regex pitfalls like
99

1010
- Off by one capture indexes
1111
- Trying to get nonexistent captures
1212
- Desync of capture names in regex and the names used to fetch fields
1313

14-
(Note this isn't like serde: you start with a regex, then write a struct to 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.
1515

1616
# Installation
1717

@@ -35,27 +35,53 @@ struct KV {
3535
let m = KV::from_str("hi: 39393")?;
3636
```
3737

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`.
3939

4040
# Supported structures
4141

4242
Structs and enums both work, although there are some slight nuances.
4343

4444
- In structures:
4545

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.
4747

4848
- In enums:
4949

5050
All variants must either
5151

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:
5353

54-
- Be a 1-tuple with a non-optional type. In this case, the variant name must match a named capture
54+
Ex:
5555

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+
enum AOrB {
59+
A {
60+
a_field: String,
61+
},
62+
B {
63+
b_field: String,
64+
}
65+
}
66+
```
5767

58-
Generally speaking the following types are suppored:
68+
The enum variant is determined by the presence of either the `a_field` capture or `b_field` capture.
69+
70+
- Be a 1-tuple with a non-optional type. In this case, the variant name must match a named capture:
71+
72+
Ex:
73+
74+
```rust
75+
#[structre("(?<A>.*)|(?<B>.*)")]
76+
enum AOrB {
77+
A(String),
78+
B(String),
79+
}
80+
```
81+
82+
The enum variant is determined by the presence of either the `A` capture or `B` capture.
83+
84+
The following types are supported for fields:
5985

6086
- Simple types: any type implementing `std::str::FromStr`
6187

@@ -69,16 +95,6 @@ See the [./crates/structre/tests/tests.rs](tests) for some simple examples.
6995

7096
I was hoping to be able to ensure that the regex has valid characters for numbers, but due to the above and the difficulty of reasoning about the contents of regex ASTs I had to scrap that.
7197

72-
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!
73-
74-
# Design
75-
76-
The main goals are, in order of importance:
77-
78-
1. Safety
79-
80-
2. Ease of use
81-
82-
3. Performance
98+
Non-unicode parsing isn't currently supported. One issue is 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!
8399

84-
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.
100+
The regex is lazily compiled and stored statically. 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

Comments
 (0)