Skip to content

Commit

Permalink
Add feature flag to switch functionality of count
Browse files Browse the repository at this point in the history
This feature flag is off by default meaning excluding disabled variants
from the COUNT is disabled by default.

Sometime in the future, this flag could be switched on by default and
thus introduce the breaking change.
  • Loading branch information
schultetwin1 committed May 23, 2023
1 parent 7f476b7 commit 0e75987
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions strum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ travis-ci = { repository = "Peternator7/strum" }
[features]
default = ["std"]
derive = ["strum_macros"]
enum-count-excludes-disabled = ["strum_macros/enum-count-excludes-disabled"]
std = []

[package.metadata.docs.rs]
Expand Down
4 changes: 4 additions & 0 deletions strum_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ readme = "../README.md"
proc-macro = true
name = "strum_macros"

[features]
default = []
enum-count-excludes-disabled = []

[dependencies]
heck = "0.4.1"
proc-macro2 = "1.0"
Expand Down
4 changes: 4 additions & 0 deletions strum_macros/src/macros/enum_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput};

#[cfg(feature = "enum-count-excludes-disabled")]
use crate::helpers::variant_props::HasStrumVariantProperties;
use crate::helpers::{non_enum_error, HasTypeProperties};

pub(crate) fn enum_count_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
let n = match &ast.data {
#[cfg(not(feature = "enum-count-excludes-disabled"))]
Data::Enum(v) => v.variants.len(),
#[cfg(feature = "enum-count-excludes-disabled")]
Data::Enum(v) => v.variants.iter().try_fold(0usize, |acc, v| {
if v.get_variant_properties()?.disabled.is_none() {
Ok::<usize, syn::Error>(acc + 1usize)
Expand Down
2 changes: 1 addition & 1 deletion strum_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test_phf = ["strum/phf"]

[dependencies]
strum = { path = "../strum", features = ["derive"] }
strum_macros = { path = "../strum_macros", features = [] }
strum_macros = { path = "../strum_macros", features = ["enum-count-excludes-disabled"] }
clap = "=2.33.0"
enum_variant_type = "=0.2.0"
structopt = "0.2.18"
Expand Down

0 comments on commit 0e75987

Please sign in to comment.