Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: use the tuple value for to_string on default #270

Merged
merged 1 commit into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion strum_macros/src/helpers/variant_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct StrumVariantProperties {
pub documentation: Vec<LitStr>,
pub string_props: Vec<(LitStr, LitStr)>,
serialize: Vec<LitStr>,
to_string: Option<LitStr>,
pub to_string: Option<LitStr>,
ident: Option<Ident>,
}

Expand Down
16 changes: 15 additions & 1 deletion strum_macros/src/macros/strings/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@ pub fn display_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
Fields::Named(..) => quote! { {..} },
};

arms.push(quote! { #name::#ident #params => f.pad(#output) });
if variant_properties.to_string.is_none() && variant_properties.default.is_some() {
match &variant.fields {
Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
arms.push(quote! { #name::#ident(ref s) => f.pad(s) });
}
_ => {
return Err(syn::Error::new_spanned(
variant,
"Default only works on newtype structs with a single String field",
))
}
}
} else {
arms.push(quote! { #name::#ident #params => f.pad(#output) });
}
}

if arms.len() < variants.len() {
Expand Down
16 changes: 16 additions & 0 deletions strum_macros/src/macros/strings/to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ pub fn to_string_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
continue;
}

// display variants like Green("lime") as "lime"
if variant_properties.to_string.is_none() && variant_properties.default.is_some() {
match &variant.fields {
Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
arms.push(quote! { #name::#ident(ref s) => ::std::string::String::from(s) });
continue;
}
_ => {
return Err(syn::Error::new_spanned(
variant,
"Default only works on newtype structs with a single String field",
))
}
}
}

// Look at all the serialize attributes.
let output = variant_properties.get_preferred_name(type_properties.case_style);

Expand Down
22 changes: 22 additions & 0 deletions strum_tests/tests/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ fn to_red_string() {
assert_eq!(String::from("RedRed"), format!("{}", Color::Red));
}

#[test]
fn to_green_string() {
assert_eq!(
String::from("lime"),
format!("{}", Color::Green("lime".into()))
);
}

#[derive(Debug, Eq, PartialEq, EnumString, Display)]
enum ColorWithDefaultAndToString {
#[strum(default, to_string = "GreenGreen")]
Green(String),
}

#[test]
fn to_green_with_default_and_to_string() {
assert_eq!(
String::from("GreenGreen"),
format!("{}", ColorWithDefaultAndToString::Green("lime".into()))
);
}

#[derive(Display, Debug, Eq, PartialEq)]
#[strum(serialize_all = "snake_case")]
enum Brightness {
Expand Down
30 changes: 30 additions & 0 deletions strum_tests/tests/to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ fn to_red_string() {
);
}

#[test]
fn to_green_string_with_default() {
assert_eq!(
String::from("lime"),
(Color::Green("lime".into())).to_string()
);
assert_eq!(
Color::Green("lime".into()),
Color::from_str("lime").unwrap()
);
}

#[derive(Debug, Eq, PartialEq, EnumString, ToString)]
enum ColorWithDefaultAndToString {
#[strum(default, to_string = "GreenGreen")]
Green(String),
}

#[test]
fn to_green_with_default_and_to_string() {
assert_eq!(
String::from("GreenGreen"),
(ColorWithDefaultAndToString::Green("lime".into())).to_string()
);
assert_eq!(
ColorWithDefaultAndToString::Green("lime".into()),
ColorWithDefaultAndToString::from_str("lime").unwrap()
);
}

#[derive(Debug, Eq, PartialEq, ToString)]
#[strum(serialize_all = "snake_case")]
enum Brightness {
Expand Down