Skip to content

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

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

lambdasqd/enum_str-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages