1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use TokenStream;
use proc_macro_error;
use table_enum_core;
/// Creates a table-like enum.
///
/// # Example:
///
/// ```ignore
/// 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:
///
/// ```ignore
/// 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<EnumType>
/// No more than one field can have this attribute. All values of this field must be different.