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

EnumString: implement ToString/Display as well? #2

Closed
lucab opened this issue Apr 12, 2017 · 5 comments
Closed

EnumString: implement ToString/Display as well? #2

lucab opened this issue Apr 12, 2017 · 5 comments

Comments

@lucab
Copy link

lucab commented Apr 12, 2017

I was briefly looking at this crate, and I think I would love to see an automatic to_string() implementation and some way to link it to the current attributes-based approach employed for from_string(). This would allow for roundtrips Enum <-> String.

I'm actually thinking about something similar to serde ones plus additional aliases:

#[derive(EnumString)]
enum Color {
    #[strum(rename="blue",alias="b")]
    Blue,
    ...
}

impl std::str::FromStr for Color {
    fn from_str(s: &str) -> ::std::result::Result<Color, Self::Error> {
        match s {
               "blue" | "b" => ::std::result::Result::Ok(Color::Blue),
               ...
        }
    }
}

impl fmt::Display for Color {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
               Color::Blue => write!(f, "blue"),
               ...
        }
    }
}

There are also a bunch more interesting methods in https://docs.rs/enum_derive/0.1.7/enum_derive/#overview.

@Peternator7
Copy link
Owner

That's a good idea, and it shouldn't be too hard to add a ToString implementation. I might be able to get to it this weekend.

The rename and alias feature mostly already exists in the form of serialize. It would be nice to have some way of identifying the preferred serialization though. I'll think about this as well.

@lucab
Copy link
Author

lucab commented Apr 13, 2017

Oh great 👍 I'll watch for this as I prefer to have a single library doing this instead of rolling it by myself n-times.

I've read about the multi-entry serialize, but here you need some kind of "leader-selection" to string-ify. rename comes from serde while alias it's just a random suggestion of mine. Feel free to pick whatever you think suits better, I'd just like to be able to have a single field for both ToString and FromString.

@Peternator7
Copy link
Owner

Hey @lucab,

I merged changes into master. I couldn't find a keyword I was in love with; I wanted to stay in the spirit of serialize, but nothing felt super good so I settled on to_string="..." The updated docs explain how it works, but here's the short version. FromString will include both serialize and to_string (a bit confusing I know) when it's attempting to parse a string. ToString will prefer to_string if it is present when converting an enum into a string.

@lucab
Copy link
Author

lucab commented Apr 16, 2017

Thanks, I'll give 0.6.0 a try.

@lucab
Copy link
Author

lucab commented Apr 18, 2017

Adopted in camallo/dkregistry-rs#17, this suits what I was looking for. Closing.

@lucab lucab closed this as completed Apr 18, 2017
Peternator7 pushed a commit that referenced this issue Jan 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants