cfg_table/lib.rs
1//! [](https://crates.io/crates/cfg_table)
2//!
3//! # ✨ `cfg_table`
4//!
5//! A simple macro that expands to different values across compilation targets.
6//!
7//! ## Panics
8//!
9//! This macro will panic at runtime if no matching value is found.
10//!
11//! ## Example
12//!
13//! ```ignore
14//! #[macro_use] extern crate cfg_table;
15//!
16//! let var = cfg_table! {
17//! [all(target_os = "freebsd", target_pointer_width = "64", feature = "my-feature")] => 1337, // custom
18//!
19//! // common platforms
20//! win32 => 32,
21//! win64 => 64,
22//! linux32 => 32,
23//! linux64 => 64,
24//! macos32 => 32,
25//! macos64 => 64,
26//!
27//! // pointer widths
28//! 32 => 1985,
29//! "32" => 1985,
30//! 64 => 2003,
31//! "64" => 2003,
32//!
33//! _ => 123, // default value if nothing matches, this must be at the bottom
34//! };
35//!
36//! cfg_table! {
37//! win32 => {
38//! println!("You're on Windows 32-bit!");
39//! },
40//!
41//! win64 => {
42//! println!("You're on Windows 64-bit!");
43//! },
44//!
45//! _ => {
46//! panic!("What the heck is a \"Linux\"?");
47//! },
48//! };
49
50#[macro_export]
51/// A simple macro that expands to different values across compilation targets.
52///
53/// **Note, that you must have a leading `,` in the macro invocation.**
54///
55/// ## Panics
56///
57/// This macro will panic at runtime if no matching value is found.
58///
59/// ## Example
60///
61/// ```rust
62/// # use ::cfg_table::cfg_table;
63/// let var = cfg_table! {
64/// [all(target_os = "freebsd", target_pointer_width = "64", feature = "my-feature")] => 1337, // custom
65///
66/// // common platforms
67/// win32 => 32,
68/// win64 => 64,
69/// linux32 => 32,
70/// linux64 => 64,
71/// macos32 => 32,
72/// macos64 => 64,
73///
74/// // pointer widths
75/// 32 => 1985,
76/// "32" => 1985,
77/// 64 => 2003,
78/// "64" => 2003,
79///
80/// _ => 123, // default value if nothing matches, this must be at the bottom
81/// };
82///
83/// cfg_table! {
84/// win32 => {
85/// println!("You're on Windows 32-bit!");
86/// },
87///
88/// win64 => {
89/// println!("You're on Windows 64-bit!");
90/// },
91///
92/// _ => {
93/// panic!("What the heck is a \"Linux\"?");
94/// },
95/// };
96macro_rules! cfg_table {
97 () => {
98 unreachable!()
99 };
100
101 (_ => $expr:expr,) => {{
102 $expr
103 }};
104
105 ([$cfg:meta] => $expr:expr, $($tail:tt)*) => {{
106 #[cfg($cfg)] {
107 $expr
108 }
109 #[cfg(not($cfg))] {
110 $crate::cfg_table!($($tail)*)
111 }
112 }};
113
114 (32 => $expr:expr, $($tail:tt)*) => {{
115 #[cfg(target_pointer_width = "32")] {
116 $expr
117 }
118 #[cfg(not(target_pointer_width = "32"))] {
119 $crate::cfg_table!($($tail)*)
120 }
121 }};
122
123 (64 => $expr:expr, $($tail:tt)*) => {{
124 #[cfg(target_pointer_width = "64")] {
125 $expr
126 }
127 #[cfg(not(target_pointer_width = "64"))] {
128 $crate::cfg_table!($($tail)*)
129 }
130 }};
131
132 ("32" => $expr:expr, $($tail:tt)*) => {{
133 #[cfg(target_pointer_width = "32")] {
134 $expr
135 }
136 #[cfg(not(target_pointer_width = "32"))] {
137 $crate::cfg_table!($($tail)*)
138 }
139 }};
140
141 ("64" => $expr:expr, $($tail:tt)*) => {{
142 #[cfg(target_pointer_width = "64")] {
143 $expr
144 }
145 #[cfg(not(target_pointer_width = "64"))] {
146 $crate::cfg_table!($($tail)*)
147 }
148 }};
149
150 (macos => $expr:expr, $($tail:tt)*) => {{
151 #[cfg(target_os = "macos")] {
152 $expr
153 }
154 #[cfg(not(target_os = "macos"))] {
155 $crate::cfg_table!($($tail)*)
156 }
157 }};
158
159 (linux => $expr:expr, $($tail:tt)*) => {{
160 #[cfg(target_os = "linux")] {
161 $expr
162 }
163 #[cfg(not(target_os = "linux"))] {
164 $crate::cfg_table!($($tail)*)
165 }
166 }};
167
168 (windows => $expr:expr, $($tail:tt)*) => {{
169 #[cfg(target_os = "windows")] {
170 $expr
171 }
172 #[cfg(not(target_os = "windows"))] {
173 $crate::cfg_table!($($tail)*)
174 }
175 }};
176
177 (macos32 => $expr:expr, $($tail:tt)*) => {{
178 #[cfg(all(target_os = "macos", target_pointer_width = "32"))] {
179 $expr
180 }
181 #[cfg(not(all(target_os = "macos", target_pointer_width = "32")))] {
182 $crate::cfg_table!($($tail)*)
183 }
184 }};
185
186 (macos64 => $expr:expr, $($tail:tt)*) => {{
187 #[cfg(all(target_os = "macos", target_pointer_width = "64"))] {
188 $expr
189 }
190 #[cfg(not(all(target_os = "macos", target_pointer_width = "64")))] {
191 $crate::cfg_table!($($tail)*)
192 }
193 }};
194
195 (linux32 => $expr:expr, $($tail:tt)*) => {{
196 #[cfg(all(target_os = "linux", target_pointer_width = "32"))] {
197 $expr
198 }
199 #[cfg(not(all(target_os = "linux", target_pointer_width = "32")))] {
200 $crate::cfg_table!($($tail)*)
201 }
202 }};
203
204 (linux64 => $expr:expr, $($tail:tt)*) => {{
205 #[cfg(all(target_os = "linux", target_pointer_width = "64"))] {
206 $expr
207 }
208 #[cfg(not(all(target_os = "linux", target_pointer_width = "64")))] {
209 $crate::cfg_table!($($tail)*)
210 }
211 }};
212
213 (win32 => $expr:expr, $($tail:tt)*) => {{
214 #[cfg(all(target_os = "windows", target_pointer_width = "32"))] {
215 $expr
216 }
217 #[cfg(not(all(target_os = "windows", target_pointer_width = "32")))] {
218 $crate::cfg_table!($($tail)*)
219 }
220 }};
221
222 (win64 => $expr:expr, $($tail:tt)*) => {{
223 #[cfg(all(target_os = "windows", target_pointer_width = "64"))] {
224 $expr
225 }
226 #[cfg(not(all(target_os = "windows", target_pointer_width = "64")))] {
227 $crate::cfg_table!($($tail)*)
228 }
229 }};
230}