launchkey_sdk/launchkey/
macros.rs

1// Macro for single bidirectional mappings.
2#[macro_export]
3macro_rules! bidirectional_enum_mappings {
4    ($name:ident, $type:ty, { $($variant:ident => $value:expr),* $(,)? }) => {
5        impl $name {
6            // Generates O(1) lookup for single mapping
7            pub fn to_value(&self) -> $type {
8                match self {
9                    $(
10                        $name::$variant => $value,
11                    )*
12                }
13            }
14
15            // Reverse lookup for single mapping (value -> enum variant)
16            pub fn from_value(value: $type) -> Option<$name> {
17                match value {
18                    $(
19                        $value => Some($name::$variant),
20                    )*
21                    _ => None,
22                }
23            }
24        }
25    };
26}
27
28// Macro for bidirectional mappings with a custom Mode enum.
29#[macro_export]
30macro_rules! bidirectional_enum_mappings_with_mode {
31    // With custom mode enum name (mode is required)
32    ($name:ident, $type:ty, $mode_name:ident, { $($mode:ident => {$($variant:ident => $value:expr),* $(,)?}),* $(,)? }) => {
33        // Mode enum
34        #[derive(Debug, Clone, Copy)]
35        pub enum $mode_name {
36            $($mode),*
37        }
38
39        impl $name {
40            // Generates O(1) lookup for each mode
41            pub fn to_value_mode(&self, mode: $mode_name) -> $type {
42                match mode {
43                    $(
44                        $mode_name::$mode => match self {
45                            $(
46                                $name::$variant => $value,
47                            )*
48                        },
49                    )*
50                }
51            }
52
53            // Reverse lookup for each mode (value -> enum variant)
54            pub fn from_value_mode(value: $type, mode: $mode_name) -> Option<$name> {
55                match mode {
56                    $(
57                        $mode_name::$mode => Some(match value {
58                            $(
59                                $value => $name::$variant,
60                            )*
61                            _ => return None
62                        }),
63                    )*
64                }
65            }
66
67            pub fn from_value(value: $type) -> Option<($name, $mode_name)> {
68                match value {
69                    $(
70                        $( $value => Some(($name::$variant, $mode_name::$mode)), )*
71                    )*
72                    _ => None,
73                }
74            }
75        }
76    };
77}
78
79#[macro_export]
80macro_rules! bidirectional_enum_mappings_with_existing_mode {
81    ($name:ident, $type:ty, $mode_name:ident, { $($mode:ident => { $($variant:ident => $value:expr),* $(,)? }),* $(,)? }) => {
82        impl $name {
83            // Maps enum variant to value based on mode
84            pub fn to_value_mode(&self, mode: $mode_name) -> $type {
85                match mode {
86                    $(
87                        $mode_name::$mode => match self {
88                            $(
89                                $name::$variant => $value,
90                            )*
91                        },
92                    )*
93                }
94            }
95
96            // Maps value back to enum variant based on mode
97            pub fn from_value_mode(value: $type, mode: $mode_name) -> Option<$name> {
98                match mode {
99                    $(
100                        $mode_name::$mode => Some(match value {
101                            $(
102                                $value => $name::$variant,
103                            )*
104                            _ => return None,
105                        }),
106                    )*
107                }
108            }
109
110            // Find enum variant and its mode from a value
111            pub fn from_value(value: $type) -> Option<($name, $mode_name)> {
112                match value {
113                    $(
114                        $(
115                            $value => Some(($name::$variant, $mode_name::$mode)),
116                        )*
117                    )*
118                    _ => None,
119                }
120            }
121        }
122    };
123}