pub trait EnumProperty {
// Required methods
fn get_str(&self, prop: &str) -> Option<&'static str>;
fn get_int(&self, _prop: &str) -> Option<i64>;
fn get_bool(&self, _prop: &str) -> Option<bool>;
}
Expand description
EnumProperty
is a trait that makes it possible to store additional information
with enum variants. This trait is designed to be used with the macro of the same
name in the strum_macros
crate. Currently, the string, integer and bool literals
are supported in attributes.
§Example
// You need to bring the type into scope to use it!!!
use strum::EnumProperty;
#[derive(PartialEq, Eq, Debug, EnumProperty)]
enum Class {
#[strum(props(Teacher="Ms.Frizzle", Room="201", students=16, mandatory=true))]
History,
#[strum(props(Teacher="Mr.Smith"))]
#[strum(props(Room="103", students=10))]
Mathematics,
#[strum(props(Time="2:30", mandatory=true))]
Science,
}
let history = Class::History;
assert_eq!("Ms.Frizzle", history.get_str("Teacher").unwrap());
assert_eq!(16, history.get_int("students").unwrap());
assert!(history.get_bool("mandatory").unwrap());