1 unstable release

0.1.0 Feb 5, 2019

#885 in Rust patterns

Download history 13/week @ 2021-01-16 14/week @ 2021-01-23 7/week @ 2021-01-30 35/week @ 2021-02-06 14/week @ 2021-02-13 99/week @ 2021-02-20 59/week @ 2021-02-27 45/week @ 2021-03-06 117/week @ 2021-03-13 82/week @ 2021-03-20 37/week @ 2021-03-27 20/week @ 2021-04-03 71/week @ 2021-04-10 29/week @ 2021-04-17 43/week @ 2021-04-24 44/week @ 2021-05-01

163 downloads per month
Used in 2 crates

MIT license

4KB

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

~1.5MB
~42K SLoC