From 785b3952b9916c332df5026b462ad75b4ae159d4 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 23 Oct 2022 22:30:08 +0200 Subject: [PATCH] cfg_match --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cfg_match.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4e1a577..6c3634b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,5 +3,5 @@ version = 3 [[package]] -name = "haha" +name = "uwu" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 9ea908b..64cbcf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "haha" +name = "uwu" version = "0.1.0" edition = "2021" diff --git a/src/cfg_match.rs b/src/cfg_match.rs index e69de29..5703544 100644 --- a/src/cfg_match.rs +++ b/src/cfg_match.rs @@ -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!(); + } + _ => {} + } + } +}