1 unstable release

0.1.0 Feb 5, 2019

#20 in #from-str

Download history 40/week @ 2024-07-29 36/week @ 2024-08-05 17/week @ 2024-08-12 20/week @ 2024-08-19 43/week @ 2024-08-26 34/week @ 2024-09-02 37/week @ 2024-09-09 23/week @ 2024-09-16 37/week @ 2024-09-23 15/week @ 2024-09-30 21/week @ 2024-10-07 23/week @ 2024-10-14 47/week @ 2024-10-21 37/week @ 2024-10-28 42/week @ 2024-11-04 14/week @ 2024-11-11

142 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
~48K SLoC