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 pull request Peternator7#285 from MendyBerger:try-as
Added EnumTryAs
- Loading branch information
Showing
7 changed files
with
183 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::helpers::{non_enum_error, snakify, HasStrumVariantProperties}; | ||
use proc_macro2::TokenStream; | ||
use quote::{format_ident, quote, ToTokens}; | ||
use syn::{Data, DeriveInput}; | ||
|
||
pub fn enum_try_as_inner(ast: &DeriveInput) -> syn::Result<TokenStream> { | ||
let variants = match &ast.data { | ||
Data::Enum(v) => &v.variants, | ||
_ => return Err(non_enum_error()), | ||
}; | ||
|
||
let enum_name = &ast.ident; | ||
|
||
let variants: Vec<_> = variants | ||
.iter() | ||
.filter_map(|variant| { | ||
if variant.get_variant_properties().ok()?.disabled.is_some() { | ||
return None; | ||
} | ||
|
||
match &variant.fields { | ||
syn::Fields::Unnamed(values) => { | ||
let variant_name = &variant.ident; | ||
let types: Vec<_> = values.unnamed.iter().map(|field| { | ||
field.to_token_stream() | ||
}).collect(); | ||
let field_names: Vec<_> = values.unnamed.iter().enumerate().map(|(i, _)| { | ||
let name = "x".repeat(i + 1); | ||
let name = format_ident!("{}", name); | ||
quote! {#name} | ||
}).collect(); | ||
|
||
let move_fn_name = format_ident!("try_as_{}", snakify(&variant_name.to_string())); | ||
let ref_fn_name = format_ident!("try_as_{}_ref", snakify(&variant_name.to_string())); | ||
let mut_fn_name = format_ident!("try_as_{}_mut", snakify(&variant_name.to_string())); | ||
|
||
Some(quote! { | ||
#[must_use] | ||
#[inline] | ||
pub fn #move_fn_name(self) -> ::core::option::Option<(#(#types),*)> { | ||
match self { | ||
#enum_name::#variant_name (#(#field_names),*) => Some((#(#field_names),*)), | ||
_ => None | ||
} | ||
} | ||
|
||
#[must_use] | ||
#[inline] | ||
pub const fn #ref_fn_name(&self) -> ::core::option::Option<(#(&#types),*)> { | ||
match self { | ||
#enum_name::#variant_name (#(#field_names),*) => Some((#(#field_names),*)), | ||
_ => None | ||
} | ||
} | ||
|
||
#[must_use] | ||
#[inline] | ||
pub fn #mut_fn_name(&mut self) -> ::core::option::Option<(#(&mut #types),*)> { | ||
match self { | ||
#enum_name::#variant_name (#(#field_names),*) => Some((#(#field_names),*)), | ||
_ => None | ||
} | ||
} | ||
}) | ||
}, | ||
_ => { | ||
return None; | ||
} | ||
} | ||
|
||
}) | ||
.collect(); | ||
|
||
Ok(quote! { | ||
impl #enum_name { | ||
#(#variants)* | ||
} | ||
} | ||
.into()) | ||
} |
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,48 @@ | ||
use strum::EnumTryAs; | ||
|
||
#[derive(EnumTryAs)] | ||
enum Foo { | ||
Unnamed0(), | ||
Unnamed1(u128), | ||
Unnamed2(bool, String), | ||
#[strum(disabled)] | ||
#[allow(dead_code)] | ||
Disabled(u32), | ||
#[allow(dead_code)] | ||
Unit, | ||
#[allow(dead_code)] | ||
Named { _a: u32, _b: String }, | ||
} | ||
|
||
#[test] | ||
fn unnamed_0() { | ||
let foo = Foo::Unnamed0(); | ||
assert_eq!(Some(()), foo.try_as_unnamed_0()); | ||
} | ||
|
||
#[test] | ||
fn unnamed_1() { | ||
let foo = Foo::Unnamed1(128); | ||
assert_eq!(Some(&128), foo.try_as_unnamed_1_ref()); | ||
} | ||
|
||
#[test] | ||
fn unnamed_2() { | ||
let foo = Foo::Unnamed2(true, String::from("Hay")); | ||
assert_eq!(Some((true, String::from("Hay"))), foo.try_as_unnamed_2()); | ||
} | ||
|
||
#[test] | ||
fn can_mutate() { | ||
let mut foo = Foo::Unnamed1(128); | ||
if let Some(value) = foo.try_as_unnamed_1_mut() { | ||
*value = 44_u128; | ||
} | ||
assert_eq!(foo.try_as_unnamed_1(), Some(44)); | ||
} | ||
|
||
#[test] | ||
fn doesnt_match_other_variations() { | ||
let foo = Foo::Unnamed1(66); | ||
assert_eq!(None, foo.try_as_unnamed_0()); | ||
} |