launchkey_sdk/launchkey/
macros.rs

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