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

Implement FromStr for Enum #59

Closed
svenstaro opened this issue Nov 9, 2018 · 3 comments · Fixed by #176
Closed

Implement FromStr for Enum #59

svenstaro opened this issue Nov 9, 2018 · 3 comments · Fixed by #176

Comments

@svenstaro
Copy link

svenstaro commented Nov 9, 2018

I have an enum with a long list like this:

enum ThingType {
    A,
    B,
    ...
    Z,
}

I want to be able to do

ThingType::from_str("A")

and get ThingType::A.

@JelteF
Copy link
Owner

JelteF commented Nov 9, 2018 via email

@Razican
Copy link

Razican commented Feb 3, 2020

It would be nice to also have the option to rename all to lowercase, camelcase and more, like serde.

@Luro02
Copy link

Luro02 commented Mar 26, 2020

It would be nice to also have the option to rename all to lowercase, camelcase and more, like serde.

You might be interested in strum, where the Display and EnumString derive macros are what you want:

use std::str::FromStr;
use strum::{Display, EnumString};

#[derive(Display, EnumString)]
#[strum(serialize_all = "SCREAMING-KEBAB-CASE")]
enum MediaType {
    Audio,
    Video,
    Subtitles,
    ClosedCaptions,
}

fn main() {
    assert_eq!(format!(MediaType::ClosedCaptions), "CLOSED-CAPTIONS".to_string());
    assert_eq!(MediaType::from_str("CLOSED-CAPTIONS"), MediaType::ClosedCaptions);
}

aj-bagwell added a commit to aj-bagwell/derive_more that referenced this issue Dec 3, 2021
This allows deriving FromStr for enums with no fields in the obvious way

fixes JelteF#59
aj-bagwell added a commit to aj-bagwell/derive_more that referenced this issue Dec 3, 2021
This allows deriving FromStr for enums with no fields in the obvious way

fixes JelteF#59
aj-bagwell added a commit to aj-bagwell/derive_more that referenced this issue Dec 3, 2021
This allows deriving FromStr for enums with no fields in the obvious way

fixes JelteF#59
aj-bagwell added a commit to aj-bagwell/derive_more that referenced this issue Dec 3, 2021
This allows deriving FromStr for enums with no fields in the obvious way

fixes JelteF#59
aj-bagwell added a commit to aj-bagwell/derive_more that referenced this issue Dec 3, 2021
This allows deriving FromStr for enums with no fields in the obvious way

fixes JelteF#59
aj-bagwell added a commit to aj-bagwell/derive_more that referenced this issue Dec 3, 2021
This allows deriving FromStr for enums with no fields in the obvious way

fixes JelteF#59
aj-bagwell added a commit to aj-bagwell/derive_more that referenced this issue Dec 3, 2021
This allows deriving FromStr for enums with no fields in the obvious way

fixes JelteF#59
aj-bagwell added a commit to aj-bagwell/derive_more that referenced this issue Feb 8, 2022
This allows deriving FromStr for enums with no fields in the obvious way

fixes JelteF#59
JelteF pushed a commit that referenced this issue Feb 8, 2022
This allows deriving FromStr for enums with no fields in the obvious way

fixes #59
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

Successfully merging a pull request may close this issue.

4 participants