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

Add #[strum(transparent)] argument #258

Open
bobozaur opened this issue Mar 9, 2023 · 3 comments · May be fixed by #331
Open

Add #[strum(transparent)] argument #258

bobozaur opened this issue Mar 9, 2023 · 3 comments · May be fixed by #331

Comments

@bobozaur
Copy link

bobozaur commented Mar 9, 2023

This crate is amazing for tackling a lot of boilerplate code and also ensuring variant name changes propagate to the trait implementations. I use AsRefStr and EnumString quite a lot.

However, a lot of times I've found myself in the situation of having something like this:

enum MyEnum {
    A,
    B,
    C,
    Other(String)
}

Afaik there's no way of propagating the AsRef<str> or FromStr implementations to the underlying String. What do you think about implementing a transparent argument to the attribute, similar to #[serde(transparent)] - only for one field provided in an unnamed/named fashion.

So then we could do:

#[derive(AsRefStr, EnumStr)]
enum MyEnum {
    A,
    B,
    C,
    #[strum(transparent)]
    Other(String)
}

I could try and pick this up if you like the idea. I think it would be handy for a lot of people.

@srid
Copy link

srid commented Aug 18, 2023

This would be good to have, without which I have no choice but to write out the instances by hand:

pub enum System {
    Aarch64Linux,
    X86_64Linux,
    X86_64Darwin,
    Aarch64Darwin,
    Other(String),
}

impl From<&str> for System {
    fn from(s: &str) -> Self {
        match s {
            "aarch64-linux" => Self::Aarch64Linux,
            "x86_64-linux" => Self::X86_64Linux,
            "x86_64-darwin" => Self::X86_64Darwin,
            "aarch64-darwin" => Self::Aarch64Darwin,
            _ => Self::Other(s.to_string()),
        }
    }
}

impl From<System> for String {
    fn from(s: System) -> Self {
        match s {
            System::Aarch64Linux => "aarch64-linux".to_string(),
            System::X86_64Linux => "x86_64-linux".to_string(),
            System::X86_64Darwin => "x86_64-darwin".to_string(),
            System::Aarch64Darwin => "aarch64-darwin".to_string(),
            System::Other(s) => s,
        }
    }
}

@bobozaur
Copy link
Author

bobozaur commented Dec 8, 2023

Was hoping to get some feedback from a maintainer, but I really think this would be a useful feature so I might have a shot at it after the holidays.

Thanks for the encouragement @srid !

@bobozaur bobozaur changed the title Add #[strum(transparent)] argument Add #[strum(transparent)] argument Feb 4, 2024
@bobozaur bobozaur linked a pull request Feb 4, 2024 that will close this issue
@divagant-martian
Copy link

divagant-martian commented Apr 12, 2024

@bobozaur seems to not be documented, but from checking the code using #[strum(default)] on your variant works

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.

3 participants