Skip to content
Myr

Render alt display on one line with colons

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent bba6726 · Browse files · 3 files+20 added−33 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+20 added−33 removed

  • tests:+2 added−9 removed
  • other:+18 added−24 removed

3 files+20 added−33 removed

modified:src/fmt.rs+17 added-20 removed
1use crate::error::ErrorImpl;2use std::fmt::{self, Debug};34impl ErrorImpl<()> {5 fn print_chain(&self, f: &mut fmt::Formatter) -> fmt::Result {5 pub(crate) fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {6 write!(f, "{}", self.error())?;78 let mut chain = self.chain().skip(1).enumerate().peekable();9 if let Some((n, error)) = chain.next() {10 write!(f, "\n\nCaused by:\n ")?;11 if chain.peek().is_some() {12 write!(f, "{}: ", n)?;8 if f.alternate() {9 for cause in self.chain().skip(1) {10 write!(f, ": {}", cause)?;11 }14 write!(f, "{}", error)?;15 for (n, error) in chain {16 write!(f, "\n {}: {}", n, error)?;17 }12 }1314 Ok(())15 }1623 pub(crate) fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {24 if f.alternate() {25 self.print_chain(f)26 } else {27 write!(f, "{}", self.error())28 }29 }3017 pub(crate) fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {18 if f.alternate() {19 return Debug::fmt(self.error(), f);20 }2136 self.print_chain(f)?;37 writeln!(f)?;22 writeln!(f, "{}", self.error())?;2324 let mut chain = self.chain().skip(1).enumerate().peekable();25 if let Some((n, error)) = chain.next() {26 write!(f, "\nCaused by:\n ")?;27 if chain.peek().is_some() {28 write!(f, "{}: ", n)?;29 }30 writeln!(f, "{}", error)?;31 for (n, error) in chain {32 writeln!(f, " {}: {}", n, error)?;33 }34 }3536 #[cfg(backtrace)]37 {38 use std::backtrace::BacktraceStatus;39
23 unmodified lines
modified:src/lib.rs+1 added-4 removed
211 unmodified lines
212/// To print causes as well using anyhow's default formatting of causes, use the213/// alternate selector "{:#}".214///215/// ```console216/// failed to read instrs from ./path/to/instrs.jsox217///218/// Caused by:219/// No such file or directory (os error 2)216/// failed to read instrs from ./path/to/instrs.jsox: No such file or directory (os error 2)217/// ```218///219/// The Debug format "{:?}" includes your backtrace if one was captured. Note220/// that this is the representation you get by default if you return an error
311 unmodified lines
modified:tests/test_fmt.rs+2 added-9 removed
16 unmodified lines
17oh no!\18";1920const EXPECTED_ALTDISPLAY_G: &str = "\21f failed2223Caused by:24 oh no!\21f failed: oh no!\22";2324const EXPECTED_ALTDISPLAY_H: &str = "\28g failed2930Caused by:31 0: f failed32 1: oh no!\25g failed: f failed: oh no!\26";2728const EXPECTED_DEBUG_F: &str = "\29oh no!
82 unmodified lines