#enums #string #macro #conversion #string-representation

enum_str

Creates a unitary enum and conversions from enum variants to string and vice versa

3 releases

Uses old Rust 2015

0.1.2 Apr 12, 2018
0.1.1 Apr 11, 2018
0.1.0 Apr 11, 2018

#46 in #string-representation

MIT/Apache

7KB

This crate provides a macro to create a unitary enum and conversions from enum variants to a string representation and vice versa.

Why another crate?

The string representation does not need to be the same as the enum variant's identifier. See the example below for clarification.

Example

#[macro_use] extern crate enum_str;

use std::str::FromStr;
use enum_str::{Error, AsStr};

fn main() {
    enum_str! {
       Fruit,
       (Apple, "🍎"),
       (Pineapple, "🍍"),
       (Strawberry, "🍓"),
    }
    assert_eq!("🍎", Fruit::Apple.as_str());
    assert_eq!(Fruit::Apple, Fruit::from_str("🍎").unwrap());
}

lib.rs:

This crate provides a macro to create a unitary enum and conversions from enum variants to a string representation and vice versa.

Why another crate?

The string representation does not need to be the same as the enum variant's identifier. See the example below for clarification.

Example

#[macro_use] extern crate enum_str;

use std::str::FromStr;
use enum_str::{Error, AsStr};

fn main() {
enum_str! {
Fruit,
(Apple, "🍎"),
(Pineapple, "🍍"),
(Strawberry, "🍓"),
}
assert_eq!("🍎", Fruit::Apple.as_str());
assert_eq!(Fruit::Apple, Fruit::from_str("🍎").unwrap());
}

Dependencies

~1.5MB
~40K SLoC