Skip to content

Commit

Permalink
Fix serialize_all in AsRefStr, AsStaticStr and IntoStaticStr (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
xosdy authored and Peternator7 committed Feb 19, 2019
1 parent e58053b commit 7605c67
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
27 changes: 16 additions & 11 deletions strum_macros/src/as_ref_str.rs
@@ -1,7 +1,8 @@
use proc_macro2::TokenStream;
use syn;

use helpers::{extract_attrs, extract_meta, is_disabled, unique_attr};
use case_style::CaseStyle;
use helpers::{convert_case, extract_attrs, extract_meta, is_disabled, unique_attr};

fn get_arms(ast: &syn::DeriveInput) -> Vec<TokenStream> {
let name = &ast.ident;
Expand All @@ -11,6 +12,10 @@ fn get_arms(ast: &syn::DeriveInput) -> Vec<TokenStream> {
_ => panic!("This macro only works on Enums"),
};

let type_meta = extract_meta(&ast.attrs);
let case_style = unique_attr(&type_meta, "strum", "serialize_all")
.map(|style| CaseStyle::from(style.as_ref()));

for variant in variants {
use syn::Fields::*;
let ident = &variant.ident;
Expand All @@ -32,23 +37,23 @@ fn get_arms(ast: &syn::DeriveInput) -> Vec<TokenStream> {
if let Some(n) = attrs.pop() {
n
} else {
ident.to_string()
convert_case(ident, case_style)
}
};

let params = match variant.fields {
Unit => quote!{},
Unnamed(..) => quote!{ (..) },
Named(..) => quote!{ {..} },
Unit => quote! {},
Unnamed(..) => quote! { (..) },
Named(..) => quote! { {..} },
};

arms.push(quote!{ #name::#ident #params => #output });
arms.push(quote! { #name::#ident #params => #output });
}

if arms.len() < variants.len() {
arms.push(quote!{
_ => panic!("AsRef::<str>::as_ref() or AsStaticRef::<str>::as_static() \
called on disabled variant.")
arms.push(quote! {
_ => panic!("AsRef::<str>::as_ref() or AsStaticRef::<str>::as_static() \
called on disabled variant.")
})
}

Expand All @@ -59,7 +64,7 @@ pub fn as_ref_str_inner(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let arms = get_arms(ast);
quote!{
quote! {
impl #impl_generics ::std::convert::AsRef<str> for #name #ty_generics #where_clause {
fn as_ref(&self) -> &str {
match *self {
Expand Down Expand Up @@ -94,7 +99,7 @@ pub fn as_static_str_inner(
let arms3 = arms.clone();
match trait_variant {
GenerateTraitVariant::AsStaticStr => {
quote!{
quote! {
impl #impl_generics ::strum::AsStaticRef<str> for #name #ty_generics #where_clause {
fn as_static(&self) -> &'static str {
match *self {
Expand Down
26 changes: 26 additions & 0 deletions strum_tests/tests/as_ref_str.rs
Expand Up @@ -85,3 +85,29 @@ fn test_into_static_str() {
assert_eq!("B", <&'static str>::from(Moo::B::<String>));
assert_eq!("C", <&'static str>::from(Moo::C::<String>(&17)));
}

#[derive(Debug, Eq, PartialEq, AsRefStr, AsStaticStr, IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
enum Brightness {
DarkBlack,
Dim {
glow: usize,
},
#[strum(serialize = "Bright")]
BrightWhite,
}

#[test]
fn brightness_serialize_all() {
assert_eq!("dark_black", Brightness::DarkBlack.as_ref());
assert_eq!("dim", Brightness::Dim { glow: 0 }.as_ref());
assert_eq!("Bright", Brightness::BrightWhite.as_ref());

assert_eq!("dark_black", Brightness::DarkBlack.as_static());
assert_eq!("dim", Brightness::Dim { glow: 0 }.as_static());
assert_eq!("Bright", Brightness::BrightWhite.as_static());

assert_eq!("dark_black", <&'static str>::from(Brightness::DarkBlack));
assert_eq!("dim", <&'static str>::from(Brightness::Dim { glow: 0 }));
assert_eq!("Bright", <&'static str>::from(Brightness::BrightWhite));
}

0 comments on commit 7605c67

Please sign in to comment.