1
0
mirror of https://github.com/danog/strum.git synced 2024-11-30 04:28:59 +01:00

Fix EnumCount with disabled variants (#268)

Do not include disabled variants in the count with EnumCount. Fixes #267
This commit is contained in:
Matt Schulte 2023-06-17 17:50:15 -07:00 committed by GitHub
parent 025b1b5687
commit 67f0d1947d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -2,11 +2,18 @@ use proc_macro2::TokenStream;
use quote::quote; use quote::quote;
use syn::{Data, DeriveInput}; use syn::{Data, DeriveInput};
use crate::helpers::variant_props::HasStrumVariantProperties;
use crate::helpers::{non_enum_error, HasTypeProperties}; use crate::helpers::{non_enum_error, HasTypeProperties};
pub(crate) fn enum_count_inner(ast: &DeriveInput) -> syn::Result<TokenStream> { pub(crate) fn enum_count_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
let n = match &ast.data { let n = match &ast.data {
Data::Enum(v) => v.variants.len(), 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)
} else {
Ok::<usize, syn::Error>(acc)
}
})?,
_ => return Err(non_enum_error()), _ => return Err(non_enum_error()),
}; };
let type_properties = ast.get_type_properties()?; let type_properties = ast.get_type_properties()?;

View File

@ -11,12 +11,29 @@ enum Week {
Saturday, Saturday,
} }
#[allow(dead_code)]
#[derive(Debug, EnumCount, EnumIter)]
enum Pets {
Dog,
Cat,
Fish,
Bird,
#[strum(disabled)]
Hamster,
}
#[test] #[test]
fn simple_test() { fn simple_test() {
assert_eq!(7, Week::COUNT); assert_eq!(7, Week::COUNT);
assert_eq!(Week::iter().count(), Week::COUNT); assert_eq!(Week::iter().count(), Week::COUNT);
} }
#[test]
fn disabled_test() {
assert_eq!(4, Pets::COUNT);
assert_eq!(Pets::iter().count(), Pets::COUNT);
}
#[test] #[test]
fn crate_module_path_test() { fn crate_module_path_test() {
pub mod nested { pub mod nested {