Skip to main content

string_enum

Macro string_enum 

Source
macro_rules! string_enum {
    (
        $(#[$meta:meta])*
        $vis:vis enum $name:ident($kind:literal) {
            $($(#[$variant_meta:meta])* $variant:ident = $text:literal,)+
        }
    ) => { ... };
}
Expand description

Define an enum whose variants have fixed wire names.

The enum decodes and encodes as its names. It gets as_str, Display and FromStr with UnknownValue as the error.

ephemeris_forge::string_enum! {
    /// A color.
    pub enum Color("color") {
        /// Red.
        Red = "red",
        /// Green.
        Green = "green",
    }
}

assert_eq!("red".parse::<Color>(), Ok(Color::Red));
assert_eq!(Color::Green.as_str(), "green");