Macro table_enum

Source
table_enum!() { /* proc-macro */ }
Expand description

Creates a table-like enum.

§Example:

use table_enum::table_enum;

table_enum! {
    enum BinaryOp(text: &'static str, precedence: i32, right_assoc: bool) {
        Add("+", 10, false),
        Sub("-", 10, false),
        Mul("*", 20, false),
        Div("/", 20, false),
        Pow("**", 30, true),
        ...
    }
}

fn example() {
  println!("{}", BinaryOp.Add.text());
}

There are three convenience attributes that can be added for each field:

use table_enum::table_enum;

table_enum! {
    enum BinaryOp(#[constructor] text: &'static str, #[option] precedence: i32, #[default] right_assoc: bool) {
        Add("+", _, _),
        Sub("-", _, _),
        Mul("*", 20, _),
        Div("/", 20, _),
        Pow("**", 30, true),
        ...
    }
}

The #[option] convenience attribute lets you write the values directly but wraps them implicitly into Some(value). If instead of a value you write _, it becomes None. The return type when you invoke that field’s getter is also changed to Option<FieldType>.

The #[default] field works the same way but instead of an Option type, _ maps to Default::default(). Because Default::default() is not const fn, neither is the generated function.

The #[constructor] attribute allows generation of the constructor (called new) that takes a value of the field’s type and returns Option No more than one field can have this attribute. All values of this field must be different.