1 unstable release

0.1.0 Feb 5, 2019

#20 in #from-str

Download history 41/week @ 2024-03-11 87/week @ 2024-03-18 26/week @ 2024-03-25 49/week @ 2024-04-01 14/week @ 2024-04-08 22/week @ 2024-04-15 72/week @ 2024-04-22 63/week @ 2024-04-29 114/week @ 2024-05-06 56/week @ 2024-05-13 80/week @ 2024-05-20 21/week @ 2024-05-27 24/week @ 2024-06-03 29/week @ 2024-06-10 33/week @ 2024-06-17 46/week @ 2024-06-24

136 downloads per month
Used in 3 crates

MIT license

4KB
52 lines

enum-from-str-rs

Allow derive FromStr for enums

Overview

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.

Basic usage

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();
}

Known issues

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.

Dependencies

~2MB
~47K SLoC