Skip to content

Commit 1484202

Browse files
author
andrew
committed
Update readme with new traits, discussion on
1 parent 545ad3b commit 1484202

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

readme.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cargo add structre
2121

2222
# Use
2323

24-
Define a structure and use this macro to implement `from_str`:
24+
Define a structure and use this macro to implement `TryFrom` (and as `FromStr` if the type has no lifetimes):
2525

2626
```
2727
#[structre("(?P<key>[^:]+): (?P<value>\\d+)")]
@@ -32,10 +32,10 @@ struct KV {
3232
```
3333

3434
```
35-
let m = KV::from_str("hi: 39393")?;
35+
let m = KV::try_from("hi: 39393")?;
3636
```
3737

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`.
38+
Both `try_from` and `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
# Expressing regexes with types
4141

@@ -81,7 +81,13 @@ let m = KV::from_str("hi: 39393")?;
8181

8282
The following types are supported for fields:
8383

84-
- Simple types: any type implementing `std::str::FromStr`
84+
- Simple types: any type implementing `std::convert::TryFrom<&str>`
85+
86+
This includes `&str` if you want non-allocating parsing.
87+
88+
- Simple types: Standard library/core types that implement `std::str::FromStr`
89+
90+
The standard library doesn't implement `TryFrom<&str>` for any core types currently so an internal database is used to (roughly) identify that a field has a core type and switches to `FromStr` for that.
8591

8692
- Options with a simple type inside
8793

@@ -96,3 +102,11 @@ I was hoping to be able to ensure that the regex has valid characters for number
96102
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!
97103

98104
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.
105+
106+
~~String references and other reference types~~ Reference types are now supported via `TryFrom<&T>` starting in `0.2.0`! There was a large discussion
107+
at <https://www.reddit.com/r/rust/comments/1h2f6lt/structre_staticchecked_parsing_of_regexes_into/>. In the end I decided to go with basing all use around `TryFrom` instead of `FromStr` with special cases:
108+
109+
- Both approaches have special cases: The former has a database of standard library/core types that don't support `TryFrom` to switch to `FromStr`, the latter has carveouts for `&str` and possibly other types (`Cow`?)
110+
- I think the code for identifing special cases in the latter is more difficult; in the former, all the types are non-generic non-reference types, most without `::` paths
111+
- Hopefully `TryFrom` support will grow, and at some point the carveouts won't be needed - it seems to be the future-facing choice
112+
- `TryFrom` should allow users to wrap more types than `FromStr`, without needing annotations to explicitly switch the parsing method/trait

0 commit comments

Comments
 (0)