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

Allow usage of enum variant name in Display format #37

Open
NightEule5 opened this issue Feb 9, 2023 · 1 comment
Open

Allow usage of enum variant name in Display format #37

NightEule5 opened this issue Feb 9, 2023 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@NightEule5
Copy link

I have an Displayed enum with a custom format on each variant, like this:

#[derive(Display)]
enum SomeEnum {
    #[display("valuea:{0}")]
    ValueA(String),
    #[display("valueb:{0}")]
    ValueB(String),
    #[display("valuec:{0}")]
    ValueC(String),
}

It'd be nice to make this more readable by using the lowercase term in the format, such as #[display("{lowercase}:{0}")] or #[display("{}:{0}", lowercase)]. A drawback of the former is ambiguity if the variant contains a lowercase field (positional format args don't appear to be accepted currently), so the latter is likely the preferred form.

Even better would be this:

#[derive(Display)]
#[display("{}:{0}", lowercase)]
enum SomeEnum {
    ValueA(String),
    ValueB(String),
    ValueC(String),
    #[display(lowercase)]
    Other // breaks above convention
}
@dr-orlovsky dr-orlovsky added the enhancement New feature or request label Feb 10, 2023
@dr-orlovsky
Copy link
Member

I see. Not sure what would be the best way not to make {lowercase} literal not to conflict with variable names which may come from associated enum data or structure fields.

I am planning improvements to Display derivation, like support for vectors/colection types with some separator etc, so will keep this in mind during the design.

@dr-orlovsky dr-orlovsky added this to the 5.0 milestone Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

No branches or pull requests

2 participants