Skip to main content

rgc_chart/
macros.rs

1#[macro_export]
2/// define an "enum" (struct) of a singular type
3/// ```
4/// def_const_type_enum! (pub enumName => Type {
5///     field1 => value,
6///     field2 => value,
7/// });
8macro_rules! def_const_type_enum {
9    ($vis:vis $name:ident => $ty:ty {
10        $($variant:ident => $val:expr),+
11        $(,)?
12    }) => {
13        #[non_exhaustive]
14        $vis struct $name;
15
16        impl $name {
17            $(
18                pub const $variant: $ty = $val;
19            )+
20
21            #[allow(dead_code)]
22            pub const VARIANTS: &'static [$ty] = &[$(Self::$variant),+];
23        }
24    };
25}
26
27#[macro_export]
28/// define an "enum" (struct) of mutliple variable types
29/// ```
30/// def_const_type_enum! (pub enumName {
31///     field1: Type => value,
32///     field2: Type => value,
33/// });
34macro_rules! def_varied_type_enum {
35    ($vis:vis $name:ident {
36        $($variant:ident : $ty:ty => $val:expr),+
37        $(,)?
38    }) => {
39        #[non_exhaustive]
40        $vis struct $name;
41
42        impl $name {
43            $(
44                pub const $variant: $ty = $val;
45            )+
46
47            #[allow(dead_code)]
48            pub const VARIANTS: &'static [(&'static str, &'static dyn std::any::Any)] = &[
49                $((stringify!($variant), &$val as &dyn std::any::Any)),+
50            ];
51        }
52    };
53}