Derive Macro string_enum::StringEnum

source ·
#[derive(StringEnum)]
{
    // Attributes available to this derive:
    #[string_enum]
}
Expand description

Creates .as_str() and then implements Debug and Display using it.

§Input

Enum with `str_value`-style doc comment for each variant.

e.g.

 pub enum BinOp {
     /// `+`
     Add,
     /// `-`
     Minus,
 }

Currently, `str_value` must be live in it’s own line.

§Output

  • pub fn as_str(&self) -> &'static str
  • impl serde::Serialize with cfg(feature = "serde")
  • impl serde::Deserialize with cfg(feature = "serde")
  • impl FromStr
  • impl Debug
  • impl Display

§Example

 #[macro_use]
 extern crate string_enum;
 extern crate serde;

 #[derive(StringEnum)]
 pub enum Tokens {
     /// `a`
     A,
     /// `bar`
     B,
 }

 assert_eq!(Tokens::A.as_str(), "a");
 assert_eq!(Tokens::B.as_str(), "bar");

 assert_eq!(Tokens::A.to_string(), "a");
 assert_eq!(format!("{:?}", Tokens::A), format!("{:?}", "a"));

All formatting flags are handled correctly.