From 433b0c02145e042cd741b47e587c91989fe08950 Mon Sep 17 00:00:00 2001 From: Sathwik Matsa Date: Fri, 14 Jun 2024 15:47:42 +0530 Subject: [PATCH] feat: relax Ident constraint to allow all valid strings for argument values --- src/attributes.rs | 6 +++--- src/lib.rs | 10 +++++----- tests/rename.rs | 25 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/attributes.rs b/src/attributes.rs index 8554e21..29b5dae 100644 --- a/src/attributes.rs +++ b/src/attributes.rs @@ -214,12 +214,12 @@ impl Attributes { self.variant_renames.insert(variant.ident.clone(), rename); } - pub(crate) fn apply(&self) -> Vec<(syn::Ident, syn::Ident)> { + pub(crate) fn apply(&self) -> Vec<(syn::Ident, String)> { let mut new_names = Vec::new(); for (name, rename) in &self.variant_renames { if let Some(rename) = rename { - new_names.push(syn::Ident::new(&rename.0, name.span())); + new_names.push(rename.0.clone()); continue; } let mut new_name = String::new(); @@ -241,7 +241,7 @@ impl Attributes { }; } - new_names.push(syn::Ident::new(&new_name, name.span())); + new_names.push(new_name); } let tmp = self diff --git a/src/lib.rs b/src/lib.rs index ee19d4f..fa81ddb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,7 +172,7 @@ fn impl_enum_to_string(ast: &syn::DeriveInput) -> TokenStream { let coplues = attributes.apply(); let identifiers: Vec<&syn::Ident> = coplues.iter().map(|(i, _)| i).collect(); - let names: Vec = coplues.iter().map(|(_, n)| n.clone()).collect(); + let names: Vec = coplues.iter().map(|(_, n)| n.clone()).collect(); let mut gen = impl_display(name, &identifiers, &names); gen.extend(impl_from_str(name, &identifiers, &names)); @@ -186,13 +186,13 @@ fn impl_enum_to_string(ast: &syn::DeriveInput) -> TokenStream { fn impl_display( name: &syn::Ident, identifiers: &Vec<&syn::Ident>, - names: &Vec, + names: &Vec, ) -> TokenStream { let gen = quote! { impl ::std::fmt::Display for #name { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - #(Self::#identifiers=> write!(f, stringify!(#names))),* + #(Self::#identifiers=> write!(f, #names)),* } } } @@ -205,7 +205,7 @@ fn impl_display( fn impl_from_str( name: &syn::Ident, identifiers: &Vec<&syn::Ident>, - names: &Vec, + names: &Vec, ) -> TokenStream { let gen = quote! { impl TryFrom<&str> for #name { @@ -213,7 +213,7 @@ fn impl_from_str( fn try_from(s: &str) -> Result { match s { - #(stringify!(#names) => Ok(Self::#identifiers),)* + #(#names => Ok(Self::#identifiers),)* _ => Err(()), } } diff --git a/tests/rename.rs b/tests/rename.rs index 88691f7..a06e316 100644 --- a/tests/rename.rs +++ b/tests/rename.rs @@ -65,3 +65,28 @@ fn test_double_rename_from_str() { Ok(DoubleAniurRename::Maiar) ); } + +#[derive(PartialEq, Debug, enum_stringify::EnumStringify)] +enum Seperator { + #[enum_stringify(rename = " ")] + Space, + #[enum_stringify(rename = "-")] + Hyphen, + #[enum_stringify(rename = "")] + Empty, +} + +#[test] +fn test_seperator_rename() { + assert_eq!(Seperator::Space.to_string(), " "); + assert_eq!(Seperator::Hyphen.to_string(), "-"); + assert_eq!(Seperator::Empty.to_string(), ""); +} + +#[test] +fn test_seperator_rename_from_str() { + assert_eq!(Seperator::from_str(" "), Ok(Seperator::Space)); + assert_eq!(Seperator::from_str("-"), Ok(Seperator::Hyphen)); + assert_eq!(Seperator::from_str(""), Ok(Seperator::Empty)); + assert!(Seperator::from_str("|").is_err()); +}