forked from Peternator7/strum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Syndelis-feat/static-variants-array'
- Loading branch information
Showing
8 changed files
with
179 additions
and
3 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
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
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,36 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::{Data, DeriveInput, Fields}; | ||
|
||
use crate::helpers::{non_enum_error, HasTypeProperties, non_unit_variant_error}; | ||
|
||
pub fn static_variants_array_inner(ast: &DeriveInput) -> syn::Result<TokenStream> { | ||
let name = &ast.ident; | ||
let gen = &ast.generics; | ||
let (impl_generics, ty_generics, where_clause) = gen.split_for_impl(); | ||
|
||
let variants = match &ast.data { | ||
Data::Enum(v) => &v.variants, | ||
_ => return Err(non_enum_error()), | ||
}; | ||
|
||
let type_properties = ast.get_type_properties()?; | ||
let strum_module_path = type_properties.crate_module_path(); | ||
|
||
let idents = variants | ||
.iter() | ||
.cloned() | ||
.map(|v| { | ||
match v.fields { | ||
Fields::Unit => Ok(v.ident), | ||
_ => Err(non_unit_variant_error()) | ||
} | ||
}) | ||
.collect::<syn::Result<Vec<_>>>()?; | ||
|
||
Ok(quote! { | ||
impl #impl_generics #strum_module_path::StaticVariantsArray for #name #ty_generics #where_clause { | ||
const ALL_VARIANTS: &'static [Self] = &[ #(#name::#idents),* ]; | ||
} | ||
}) | ||
} |
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,87 @@ | ||
use strum::{StaticVariantsArray, EnumDiscriminants}; | ||
|
||
mod core {} // ensure macros call `::core` | ||
|
||
#[test] | ||
fn simple() { | ||
#[derive(StaticVariantsArray, PartialEq, Eq, Debug)] | ||
enum Operation { | ||
Add, | ||
Sub, | ||
Mul, | ||
Div, | ||
} | ||
|
||
assert_eq!( | ||
Operation::ALL_VARIANTS, | ||
&[ | ||
Operation::Add, | ||
Operation::Sub, | ||
Operation::Mul, | ||
Operation::Div, | ||
] | ||
); | ||
} | ||
|
||
#[test] | ||
fn in_enum_discriminants() { | ||
#[allow(dead_code)] | ||
#[derive(EnumDiscriminants)] | ||
#[strum_discriminants(derive(StaticVariantsArray))] | ||
#[strum_discriminants(name(GeometricShapeDiscriminants))] | ||
enum GeometricShape { | ||
Point, | ||
Circle(i32), | ||
Rectangle { | ||
width: i32, | ||
height: i32, | ||
} | ||
} | ||
|
||
assert_eq!( | ||
GeometricShapeDiscriminants::ALL_VARIANTS, | ||
&[ | ||
GeometricShapeDiscriminants::Point, | ||
GeometricShapeDiscriminants::Circle, | ||
GeometricShapeDiscriminants::Rectangle, | ||
] | ||
); | ||
} | ||
|
||
#[test] | ||
fn empty_enum() { | ||
#[derive(StaticVariantsArray, PartialEq, Eq, Debug)] | ||
enum Empty {} | ||
|
||
assert_eq!( | ||
Empty::ALL_VARIANTS, | ||
&[], | ||
); | ||
} | ||
|
||
#[test] | ||
fn variants_with_values() { | ||
#[derive(StaticVariantsArray, PartialEq, Eq, Debug)] | ||
enum WeekDay { | ||
Sunday = 0, | ||
Monday = 1, | ||
Tuesday = 2, | ||
Wednesday = 3, | ||
Thursday = 4, | ||
Friday = 5, | ||
Saturday = 6, | ||
} | ||
|
||
assert_eq!( | ||
WeekDay::ALL_VARIANTS, | ||
&[ | ||
WeekDay::Sunday, | ||
WeekDay::Monday, | ||
WeekDay::Tuesday, | ||
WeekDay::Wednesday, | ||
WeekDay::Thursday, | ||
WeekDay::Friday, | ||
WeekDay::Saturday, | ||
], | ||
); | ||
} |