failure icon indicating copy to clipboard operation
failure copied to clipboard

Add option to derive Debug identical to Display

Open ypoluektovich opened this issue 6 years ago • 1 comments

Feature request: in failure_derive, add an option to fail attribute that would automatically generate a Debug implementation that is identical to (or, perhaps more appropriately, delegating to) the Display implementation (as generated by #[fail(display=...)]).

Context: I am defining my own Fail types, for example:

#[derive(Fail)]
#[fail(display = "Aborting by user request")]
pub struct AbortedByUser;

I am also using fn main() -> Result<(), failure::Error>. This means that the std::process::Termination trait comes into play, and its implementation for Result ends up calling eprintln!("Error: {:?}", err);. If I derive Debug for my type, I get the output Error: AbortedByUser, which is not ideal UX-wise. So I ended up doing this:

impl Debug for AbortedByUser {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        Display::fmt(self, f)
    }
}

Now the program prints: Error: Aborting by user request. This was fine while I only had one such failure type, but then I added the second one for which I wanted the same kind of reporting experience. Obviously, I can remove most of the boilerpaste with a macro, but that's still a macro that I'd have to write and put somewhere in my code. It would be much nicer if I could just write something like #[fail(display = "Aborting by user request", debug_as_display)].

ypoluektovich avatar Oct 03 '19 01:10 ypoluektovich

To increase the web's connectivity:

  • relevant discussion in the tracking issue for Termination: https://github.com/rust-lang/rust/issues/43301#issuecomment-388575730
  • mentions exitfailure crate

ypoluektovich avatar Oct 06 '19 00:10 ypoluektovich