| 1 |
# What `#[derive(FromStr)]` generates |
| 2 |
|
| 3 |
Deriving `FromStr` only works for enums with no fields |
| 4 |
or newtypes, i.e structs with only a single |
| 5 |
field. The result is that you will be able to call the `parse()` method on a |
| 6 |
string to convert it to your newtype. This only works when the type that is |
| 7 |
contained in the type implements `FromStr`. |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
## Example usage |
| 13 |
|
| 14 |
```rust |
| 15 |
# use derive_more::FromStr; |
| 16 |
# |
| 17 |
#[derive(FromStr, Debug, Eq, PartialEq)] |
| 18 |
struct MyInt(i32); |
| 19 |
|
| 20 |
#[derive(FromStr, Debug, Eq, PartialEq)] |
| 21 |
struct Point1D{ |
| 22 |
x: i32, |
| 23 |
} |
| 24 |
|
| 25 |
assert_eq!(MyInt(5), "5".parse().unwrap()); |
| 26 |
assert_eq!(Point1D{x: 100}, "100".parse().unwrap()); |
| 27 |
``` |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
## Tuple structs |
| 33 |
|
| 34 |
When deriving `FromStr` for a tuple struct with one field: |
| 35 |
|
| 36 |
```rust |
| 37 |
# use derive_more::FromStr; |
| 38 |
# |
| 39 |
#[derive(FromStr)] |
| 40 |
struct MyInt(i32); |
| 41 |
``` |
| 42 |
|
| 43 |
Code like this will be generated: |
| 44 |
|
| 45 |
```rust |
| 46 |
# struct MyInt(i32); |
| 47 |
impl ::core::str::FromStr for MyInt { |
| 48 |
type Err = <i32 as ::core::str::FromStr>::Err; |
| 49 |
fn from_str(src: &str) -> Result<Self, Self::Err> { |
| 50 |
return Ok(MyInt(i32::from_str(src)?)); |
| 51 |
} |
| 52 |
} |
| 53 |
``` |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
## Regular structs |
| 59 |
|
| 60 |
When deriving `FromStr` for a regular struct with one field: |
| 61 |
|
| 62 |
```rust |
| 63 |
# use derive_more::FromStr; |
| 64 |
# |
| 65 |
#[derive(FromStr)] |
| 66 |
struct Point1D { |
| 67 |
x: i32, |
| 68 |
} |
| 69 |
``` |
| 70 |
|
| 71 |
Code like this will be generated: |
| 72 |
|
| 73 |
```rust |
| 74 |
# struct Point1D { |
| 75 |
# x: i32, |
| 76 |
# } |
| 77 |
impl ::core::str::FromStr for Point1D { |
| 78 |
type Err = <i32 as ::core::str::FromStr>::Err; |
| 79 |
fn from_str(src: &str) -> Result<Self, Self::Err> { |
| 80 |
return Ok(Point1D { |
| 81 |
x: i32::from_str(src)?, |
| 82 |
}); |
| 83 |
} |
| 84 |
} |
| 85 |
``` |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
## Enums |
| 91 |
|
| 92 |
When deriving `FromStr` for an enums with variants with no fields it will |
| 93 |
generate a `from_str` method that converts strings that match the variant name |
| 94 |
to the variant. If using a case insensitive match would give a unique variant |
| 95 |
(i.e you dont have both a `MyEnum::Foo` and a `MyEnum::foo` variant) then case |
| 96 |
insensitive matching will be used, otherwise it will fall back to exact string |
| 97 |
matching. |
| 98 |
|
| 99 |
Since the string may not match any variants an error type is needed, so the |
| 100 |
`derive_more::FromStrError` will be used for that purpose. |
| 101 |
|
| 102 |
e.g. Given the following enum: |
| 103 |
|
| 104 |
```rust |
| 105 |
# use derive_more::FromStr; |
| 106 |
# |
| 107 |
#[derive(FromStr)] |
| 108 |
enum EnumNoFields { |
| 109 |
Foo, |
| 110 |
Bar, |
| 111 |
Baz, |
| 112 |
} |
| 113 |
``` |
| 114 |
|
| 115 |
Code like this will be generated: |
| 116 |
|
| 117 |
```rust |
| 118 |
# enum EnumNoFields { |
| 119 |
# Foo, |
| 120 |
# Bar, |
| 121 |
# Baz, |
| 122 |
# } |
| 123 |
# |
| 124 |
impl ::core::str::FromStr for EnumNoFields { |
| 125 |
type Err = ::derive_more::FromStrError; |
| 126 |
fn from_str(src: &str) -> Result<Self, Self::Err> { |
| 127 |
Ok(match src.to_lowercase().as_str() { |
| 128 |
"foo" => EnumNoFields::Foo, |
| 129 |
"bar" => EnumNoFields::Bar, |
| 130 |
"baz" => EnumNoFields::Baz, |
| 131 |
_ => return Err(::derive_more::FromStrError::new("EnumNoFields")), |
| 132 |
}) |
| 133 |
} |
| 134 |
} |
| 135 |
``` |