forked from Peternator7/strum
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue Peternator7#33
- Loading branch information
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use proc_macro2::{Span, TokenStream}; | ||
use syn; | ||
|
||
use helpers::{extract_meta, extract_meta_attrs}; | ||
|
||
pub fn enum_discriminants_inner(ast: &syn::DeriveInput) -> TokenStream { | ||
let name = &ast.ident; | ||
let vis = &ast.vis; | ||
let enum_discriminants_name = syn::Ident::new( | ||
&format!("{}Discriminants", name.to_string()), | ||
Span::call_site(), | ||
); | ||
|
||
let variants = match ast.data { | ||
syn::Data::Enum(ref v) => &v.variants, | ||
_ => panic!("EnumDiscriminants only works on Enums"), | ||
}; | ||
|
||
let type_meta = extract_meta(&ast.attrs); | ||
let discriminant_derives = extract_meta_attrs(&type_meta, "strum_discriminants_derive"); | ||
let derives = quote! { | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, #(#discriminant_derives),*)] | ||
}; | ||
|
||
let mut discriminants = Vec::new(); | ||
for variant in variants { | ||
let ident = &variant.ident; | ||
|
||
// Don't copy across the "strum" meta attribute. | ||
let attrs = variant.attrs.iter().filter(|attr| { | ||
attr.interpret_meta().map_or(true, |meta| match meta { | ||
syn::Meta::List(ref metalist) => metalist.ident != "strum", | ||
_ => true, | ||
}) | ||
}); | ||
|
||
discriminants.push(quote!{ #(#attrs)* #ident }); | ||
} | ||
|
||
quote!{ | ||
/// Auto-generated discriminant enum variants | ||
#derives | ||
#vis enum #enum_discriminants_name { | ||
#(#discriminants),* | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
extern crate strum; | ||
#[macro_use] | ||
extern crate strum_macros; | ||
|
||
use strum::IntoEnumIterator; | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug, Eq, PartialEq, EnumDiscriminants)] | ||
#[strum_discriminants_derive(EnumIter)] | ||
enum Simple { | ||
Variant0, | ||
Variant1, | ||
} | ||
|
||
#[test] | ||
fn simple_test() { | ||
let discriminants = SimpleDiscriminants::iter().collect::<Vec<_>>(); | ||
let expected = vec![SimpleDiscriminants::Variant0, SimpleDiscriminants::Variant1]; | ||
|
||
assert_eq!(expected, discriminants); | ||
} | ||
|
||
#[derive(Debug)] | ||
struct NonDefault; | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug, EnumDiscriminants)] | ||
#[strum_discriminants_derive(EnumIter)] | ||
enum WithFields { | ||
Variant0(NonDefault), | ||
Variant1 { a: NonDefault }, | ||
} | ||
|
||
#[test] | ||
fn fields_test() { | ||
let discriminants = WithFieldsDiscriminants::iter().collect::<Vec<_>>(); | ||
let expected = vec![ | ||
WithFieldsDiscriminants::Variant0, | ||
WithFieldsDiscriminants::Variant1, | ||
]; | ||
|
||
assert_eq!(expected, discriminants); | ||
} | ||
|
||
trait Foo {} | ||
trait Bar {} | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug, Eq, PartialEq, EnumDiscriminants)] | ||
#[strum_discriminants_derive(EnumIter)] | ||
enum Complicated<U: Foo, V: Bar> { | ||
/// With Docs | ||
A(U), | ||
B { | ||
v: V, | ||
}, | ||
C, | ||
} | ||
|
||
#[test] | ||
fn complicated_test() { | ||
let discriminants = ComplicatedDiscriminants::iter().collect::<Vec<_>>(); | ||
let expected = vec![ | ||
ComplicatedDiscriminants::A, | ||
ComplicatedDiscriminants::B, | ||
ComplicatedDiscriminants::C, | ||
]; | ||
|
||
assert_eq!(expected, discriminants); | ||
} | ||
|
||
// This test exists to ensure that we do not copy across the `#[strum(default = "true")]` meta | ||
// attribute. If we do without deriving any `strum` derivations on the generated discriminant enum, | ||
// Rust will generate a compiler error saying it doesn't understand the `strum` attribute. | ||
#[allow(dead_code)] | ||
#[derive(Debug, EnumDiscriminants)] | ||
enum WithDefault { | ||
#[strum(default = "true")] | ||
A(String), | ||
B, | ||
} | ||
|
||
#[test] | ||
fn with_default_test() { | ||
assert!(WithDefaultDiscriminants::A != WithDefaultDiscriminants::B); | ||
} |