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

how to round trip Display and EnumString with default="true" #86

Closed
thedavidmeister opened this issue Feb 15, 2020 · 4 comments
Closed

Comments

@thedavidmeister
Copy link

I have some default variant that contains a string, like:

#[derive(Display, EnumString)]
FooEnum {
  #[strum(default="true")]
  Bar(String)
}

but if i do:

FooEnum::Bar("baz".into()).to_string();

i get "Bar" not "baz" so then there's no way to round trip it back into the original FooEnum

@Peternator7
Copy link
Owner

This is a breaking change, but it seems internally consistent so I'd be open to fixing/changing it. Is this something you're opening to doing?

@thedavidmeister
Copy link
Author

not sure... it came up for me during a refactor of a crate where this was being used... but i'm not sure if i'll be using this crate moving forward on that project as we might not even be using the enum it was relevant to any more

joshka added a commit to joshka/strum that referenced this issue May 16, 2023
This attribute causes the to_string to use the value of a
tuple-like variant to be used as the to_string value.
E.g. Color::Green("lime").to_string() will equal "lime" not "Green"

Fixes: how to round trip Display and EnumString with default="true" Peternator7#86
joshka added a commit to joshka/strum that referenced this issue May 16, 2023
This attribute causes the to_string to use the value of a
tuple-like variant to be used as the to_string value.
E.g. Color::Green("lime").to_string() will equal "lime" not "Green"

Fixes: how to round trip Display and EnumString with default="true" Peternator7#86
joshka added a commit to joshka/strum that referenced this issue May 27, 2023
The default attribute on a tuple like variant now causes the to_string
and display format to use the value of the tuple rather than the name
of the variant.

E.g. Color::Green("lime").to_string() will equal "lime" not "Green"

Fixes: how to round trip Display and EnumString with default="true" Peternator7#86

BREAKING CHANGE
This changes how Display and ToString cause the following to renders:
```rust
#[strum(default)]
Green(String)
```
To maintain the previous behavior (use the variant name):
```rust
#[strum(default, to_string("Green"))]
Green(String)
```
Peternator7 pushed a commit that referenced this issue Jun 18, 2023
The default attribute on a tuple like variant now causes the to_string
and display format to use the value of the tuple rather than the name
of the variant.

E.g. Color::Green("lime").to_string() will equal "lime" not "Green"

Fixes: how to round trip Display and EnumString with default="true" #86

BREAKING CHANGE
This changes how Display and ToString cause the following to renders:
```rust
#[strum(default)]
Green(String)
```
To maintain the previous behavior (use the variant name):
```rust
#[strum(default, to_string("Green"))]
Green(String)
```
@fullermd
Copy link

IWBNI this could be extended to AsRefStr. As it currently stands, (foo.to_string() != foo.as_ref()), which is confusing (and fertile ground for confusion you don't notice right away).

@Peternator7
Copy link
Owner

This is implemented in the linked PR #270

@fullermd, can you open a new issue for that request. The issue is that for MyEnum::Variant(T), T doesn't have to be a string so it might implement Display but not AsRef<str>. That makes it a breaking change which might be fine to be more consisent, but it'd be nice to discuss in a new thread that isn't 3 years old

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants