[][src]Derive Macro strum::EnumString

#[derive(EnumString)]
{
    // Attributes available to this derive:
    #[strum]
}

Converts strings to enum variants based on their name.

auto-derives std::str::FromStr on the enum. Each variant of the enum will match on it's own name. This can be overridden using serialize="DifferentName" or to_string="DifferentName" on the attribute as shown below. Multiple deserializations can be added to the same variant. If the variant contains additional data, they will be set to their default values upon deserialization.

The default attribute can be applied to a tuple variant with a single data parameter. When a match isn't found, the given variant will be returned and the input string will be captured in the parameter.

Note that the implementation of FromStr by default only matches on the name of the variant. There is an option to match on different case conversions through the #[strum(serialize_all = "snake_case")] type attribute.

See the Additional Attributes Section for more information on using this feature.

Example howto use EnumString

use std::str::FromStr;
use strum_macros::EnumString;

#[derive(Debug, PartialEq, EnumString)]
enum Color {
    Red,
    // The Default value will be inserted into range if we match "Green".
    Green {
        range: usize,
    },

    // We can match on multiple different patterns.
    #[strum(serialize = "blue", serialize = "b")]
    Blue(usize),

    // Notice that we can disable certain variants from being found
    #[strum(disabled)]
    Yellow,
}

/*
//The generated code will look like:
impl std::str::FromStr for Color {
    type Err = ::strum::ParseError;

    fn from_str(s: &str) -> ::std::result::Result<Color, Self::Err> {
        match s {
            "Red" => ::std::result::Result::Ok(Color::Red),
            "Green" => ::std::result::Result::Ok(Color::Green { range:Default::default() }),
            "blue" | "b" => ::std::result::Result::Ok(Color::Blue(Default::default())),
            _ => ::std::result::Result::Err(::strum::ParseError::VariantNotFound),
        }
    }
}
*/

// simple from string
let color_variant = Color::from_str("Red").unwrap();
assert_eq!(Color::Red, color_variant);
// short version works too
let color_variant = Color::from_str("b").unwrap();
assert_eq!(Color::Blue(0), color_variant);
// was disabled for parsing = returns parse-error
let color_variant = Color::from_str("Yellow");
assert!(color_variant.is_err());
// however the variant is still normally usable
println!("{:?}", Color::Yellow);