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
Copy file name to clipboardExpand all lines: readme.md
+18-4Lines changed: 18 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ cargo add structre
21
21
22
22
# Use
23
23
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):
25
25
26
26
```
27
27
#[structre("(?P<key>[^:]+): (?P<value>\\d+)")]
@@ -32,10 +32,10 @@ struct KV {
32
32
```
33
33
34
34
```
35
-
let m = KV::from_str("hi: 39393")?;
35
+
let m = KV::try_from("hi: 39393")?;
36
36
```
37
37
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`.
39
39
40
40
# Expressing regexes with types
41
41
@@ -81,7 +81,13 @@ let m = KV::from_str("hi: 39393")?;
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:
0 commit comments