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

Display: Add a prefix for all enum variants #74

Closed
ErichDonGubler opened this issue Mar 22, 2019 · 3 comments
Closed

Display: Add a prefix for all enum variants #74

ErichDonGubler opened this issue Mar 22, 2019 · 3 comments

Comments

@ErichDonGubler
Copy link
Contributor

ErichDonGubler commented Mar 22, 2019

It'd be really nice to be able to derive Display and use an affix (prefix and/or suffix) that comes before each inner implementation of Display. This is particularly handy for errors; an example I'm hoping to work with might look like:

    #[derive(Debug, Display)]
    // Expects a single Display item corresponding to where an inner 
    // variant's Display will be written (`{}`), otherwise errors
    #[display(affix = "unable to read from /proc/stat: {}")]
    pub(crate) enum SnapshotReadError {
        Io(IoError),
        #[display(fmt = "expected CPU stat line for {}, got {:#?}", expected_cpu, line)]
        UnableToParseLine {
            expected_cpu: CpuComponent,
            line: String,
        }
    }

    #[derive(Debug, Display)]
    pub(crate) enum CpuComponent {
        #[display(fmt = "entire CPU")]
        EntireCpu,
        #[display(fmt = "CPU core {}", _0)]
        Core(usize),
    }

What do you think?

Please note: I specifically chose a terrible but concise word for this which I fully expect to be bikeshedded. Alternatives might include:

  • outer_fmt (I like this one the best.)
  • prefix (but it's not just a prefix!)
  • surround
@ErichDonGubler
Copy link
Contributor Author

Ping! I've implemented a PoC on my fork's affix branch. What do you think, @JelteF?

@JelteF
Copy link
Owner

JelteF commented May 24, 2019

Could you make a PR? Makes it much easier to review

@ErichDonGubler
Copy link
Contributor Author

Sure! I'll close this in favor of #79.

The reason I didn't simply file a PR initially is because I hacked the support together to demonstrate via test/ what this would look like, and don't particularly like the commit as-is. I'll just mark the PR [WIP] and be patient when it comes time to clean things up. :)

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