mirror of
https://github.com/Noratrieb/the-good-stuff.git
synced 2026-01-16 17:45:03 +01:00
cfg_match
This commit is contained in:
parent
8fadf72227
commit
785b3952b9
3 changed files with 60 additions and 2 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -3,5 +3,5 @@
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "haha"
|
name = "uwu"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "haha"
|
name = "uwu"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! cfg_match {
|
||||||
|
() => {};
|
||||||
|
(_ => { $($tt:tt)* }) => {
|
||||||
|
$($tt)*
|
||||||
|
};
|
||||||
|
(
|
||||||
|
$head_pattern:meta => { $($head_body:tt)* }
|
||||||
|
$($rest:tt)*
|
||||||
|
) => {
|
||||||
|
|
||||||
|
#[cfg($head_pattern)]
|
||||||
|
$crate::cfg_match! { _ => { $($head_body)* } }
|
||||||
|
|
||||||
|
#[cfg(not($head_pattern))]
|
||||||
|
$crate::cfg_match! {
|
||||||
|
$($rest)*
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn correct_one_selected() {
|
||||||
|
crate::cfg_match! {
|
||||||
|
any() => {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
all() => {
|
||||||
|
|
||||||
|
}
|
||||||
|
any() => {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn underscore() {
|
||||||
|
crate::cfg_match! {
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fallback() {
|
||||||
|
crate::cfg_match! {
|
||||||
|
any() => {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
any() => {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue