Macro serializable_enum::impl_as_ref_from_str [] [src]

macro_rules! impl_as_ref_from_str {
    ($name:ident {
        $($variant:ident => $str:expr,)+
    }
    $err:ident::$err_variant:ident
    ) => { ... };
}

Generate AsRef and FromStr impls for the given type with the variant / string pairs specified.

Example

#[macro_use] extern crate serializable_enum;

// your error type
#[derive(Debug)]
enum Error {
    Parse(String),
}

// You will need display implemented (you should already have this).
impl ::std::fmt::Display for Error {
   fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
       write!(f, "{:?}", self)
   }
}

enum Color {
    /// Red
    Red,
    /// Blue
    Blue,
    /// Green
    Green,
}

impl_as_ref_from_str! {
    Color {
        Red => "red",
        Blue => "blue",
        Green => "green",
    }
    Error::Parse
}