Skip to content

Commit

Permalink
init version
Browse files Browse the repository at this point in the history
  • Loading branch information
omid committed Nov 9, 2023
0 parents commit fdf279e
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: fail scenarios
run: |
cargo run --example main 2>&1 | grep -q 'exactly one'
cargo run --example main --features "a","b" 2>&1 | grep -q 'mutually exclusive'
- name: pass scenarios
run: |
cargo run --example main --features "a"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "mutually_exclusive_features"
version = "0.0.1"
description = "Macros to check that only none or one of a set of features is enabled at a time, as known as mutually exclusive features"
license = "MIT OR Apache-2.0"
authors = ["Omid Rad <[email protected]>"]
repository = "https://github.com/omid/mutually_exclusive_features"
keywords = ["feature", "macro", "mutually_exclusive", "mutually", "exclusive"]
categories = ["development-tools", "no-std", "rust-patterns"]
edition = "2021"
rust-version = "1.56.0"

[features]
a = []
b = []

[[example]]
name = "main"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Omid Rad

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Some macros to check mutually exclusive conditions of features in Rust.

The macros are `none_or_one_of` and `exactly_one_of`.

Both check mutually exclusive of features in Rust,
but `none_or_one_of` allows for no features to be enabled,
while `exactly_one_of` requires exactly one feature to be enabled.

To use `none_or_one_of`, call it with the list of features you want to be mutually exclusive:
```rust
use mutually_exclusive_features::none_or_one_of;
none_or_one_of!("feature1", "feature2", "feature3");
```

Which will generate the following code:
```rust
#[cfg(all(feature="feature1", feature="feature2"))]
compile_error!("The `feature1` and `feature2` features are mutually exclusive and cannot be enabled at the same time!");

#[cfg(all(feature="feature1", feature="feature3"))]
compile_error!("The `feature1` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");

#[cfg(all(feature="feature2", feature="feature3"))]
compile_error!("The `feature2` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");
```

And `exactly_one_of`, is the same, but requires exactly one feature to be enabled:
```
use mutually_exclusive_features::exactly_one_of;
exactly_one_of!("feature1", "feature2", "feature3");
```

Which will generate the following code:
```rust
#[cfg(all(feature="feature1", feature="feature2"))]
compile_error!("The `feature1` and `feature2` features are mutually exclusive and cannot be enabled at the same time!");

#[cfg(all(feature="feature1", feature="feature3"))]
compile_error!("The `feature1` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");

#[cfg(all(feature="feature2", feature="feature3"))]
compile_error!("The `feature2` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");

#[cfg(not(any(feature="feature1", feature="feature2", feature="feature3")))]
compile_error!("You must enable exactly one of `feature1`, `feature2`, `feature3` features!");
```
10 changes: 10 additions & 0 deletions examples/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use mutually_exclusive_features::{exactly_one_of, none_or_one_of};

fn main() {
none_or_one_of!("a", "b", "c");
none_or_one_of!("a", "b",);
none_or_one_of!("a");
none_or_one_of!();

exactly_one_of!("a", "b", "c");
}
95 changes: 95 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#![no_std]

/**
This macro allows you to specify that certain features are mutually exclusive.
It generates any combination of the selected features, by the combination formula:
`n!/(2!*(n - 2)!)`, where n is the number of features.
For example, if you have 5 features, you will get 5!/(3!*2!) which is 10 combinations
or for 10 features, you will get 10!/(8!*2!) which is 45 combinations
To use it, simply call it with the features you want to be mutually exclusive:
```
use mutually_exclusive_features::none_or_one_of;
none_or_one_of!("feature1", "feature2", "feature3");
```
Which will generate the following code:
```ignore
#[cfg(all(feature="feature1", feature="feature2"))]
compile_error!("The `feature1` and `feature2` features are mutually exclusive and cannot be enabled at the same time!");
#[cfg(all(feature="feature1", feature="feature3"))]
compile_error!("The `feature1` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");
#[cfg(all(feature="feature2", feature="feature3"))]
compile_error!("The `feature2` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");
```
**/
#[macro_export]
macro_rules! none_or_one_of {
// root call
($($F:literal),+ $(,)?) => {
none_or_one_of!(@recurs: [params: $($F,)*]);
};

// exactly two
(
@recurs:
[params: $F1:literal, $F2:literal,]
) => {
#[cfg(all(feature=$F1, feature=$F2))]
compile_error!(concat!("The `", $F1, "` and `", $F2, "` features are mutually exclusive and cannot be enabled at the same time!"));
};

// more than two
(
@recurs:
[params: $F1:literal, $($FS:literal,)+]
) => {
$(none_or_one_of!(@recurs: [params: $F1, $FS,]);)*
none_or_one_of!(@recurs: [params: $($FS,)*]);
};

// ignore cases
() => {};
(@recurs: [params: $F1:literal,]) => {};
}

/**
This is exactly like `none_or_one_of` except that it requires exactly one of the features to be selected.
To use it, simply call it with the features you want to be mutually exclusive:
```
use mutually_exclusive_features::exactly_one_of;
exactly_one_of!("feature1", "feature2", "feature3");
```
Which will generate the following code:
```ignore
#[cfg(all(feature="feature1", feature="feature2"))]
compile_error!("The `feature1` and `feature2` features are mutually exclusive and cannot be enabled at the same time!");
#[cfg(all(feature="feature1", feature="feature3"))]
compile_error!("The `feature1` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");
#[cfg(all(feature="feature2", feature="feature3"))]
compile_error!("The `feature2` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");
#[cfg(not(any(feature="feature1", feature="feature2", feature="feature3")))]
compile_error!("You must enable exactly one of `feature1`, `feature2`, `feature3` features!");
```
**/
#[macro_export]
macro_rules! exactly_one_of {
($($F:literal),+ $(,)?) => {
none_or_one_of!(@recurs: [params: $($F,)*]);

#[cfg(not(any($(feature=$F),*)))]
compile_error!(concat!("You must enable exactly one of ", exactly_one_of!(@comma_sep: $($F,)*), " features!"));
};

(@comma_sep: $last:literal $(,)?) => { concat!("`", $last, "`") };
(@comma_sep: $head:literal, $($rest:literal),+ $(,)?) => {
concat!("`", $head, "`, ", exactly_one_of!(@comma_sep: $($rest),+))
};
}

0 comments on commit fdf279e

Please sign in to comment.