Crates.io | enum_from_str |
lib.rs | enum_from_str |
version | 0.1.0 |
source | src |
created_at | 2019-02-05 10:30:33.032891 |
updated_at | 2019-02-05 10:30:33.032891 |
description | Enable deriving FromStr for enums |
homepage | https://github.com/dam4rus/enum-from-str-rs |
repository | https://github.com/dam4rus/enum-from-str-rs.git |
max_upload_size | |
id | 112889 |
size | 4,668 |
Allow derive FromStr for enums
This library adds the #[derive(FromStr)]
attribute for enums, thus allowing parsing strings into enum variants. Even though there are other libraries that allows this, like enum_derive, it only allows you to parse from strings with the exact name of a variant. This library allows you to define a custom string for each enum variant.
use enum_from_str::ParseEnumVariantError;
use enum_from_str_derive::FromStr;
#[derive(FromStr)]
enum SomeEnum {
#[from_str="foo"]
Foo,
Bar, // equals to #[from_str="Bar"]
}
fn example() {
"foo".parse::<SomeEnum>().unwrap();
"Bar".parse::<SomeEnum>().unwrap();
}
Currently, proc-macro crates doesn't allow exporting anything other than the proc-macro function. That's why ParseEnumVariantError is in a different crate. When Rust allows it, it should be moved into a single crate.