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
Provenance
- Commit
c30a32b- Tree
1639b41- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
bba6726
3 files changed+20 added−33 removed
modified:src/fmt.rs+17 added-20 removed
1 use crate::error::ErrorImpl;2 use std::fmt::{self, Debug};3 4 impl 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())?;7 8- 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 }13 14 Ok(())15 }16 23- 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- }30-17 pub(crate) fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {18 if f.alternate() {19 return Debug::fmt(self.error(), f);20 }21 36- self.print_chain(f)?;37- writeln!(f)?;22+ writeln!(f, "{}", self.error())?;23 24+ 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+ }35+36 #[cfg(backtrace)]37 {38 use std::backtrace::BacktraceStatus;39 23 unmodified linesmodified:src/lib.rs+1 added-4 removed
211 unmodified lines212 /// 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 error311 unmodified linesmodified:tests/test_fmt.rs+2 added-9 removed
16 unmodified lines17 oh no!\18 ";19 20 const EXPECTED_ALTDISPLAY_G: &str = "\21-f failed22-23-Caused by:24- oh no!\21+f failed: oh no!\22 ";23 24 const EXPECTED_ALTDISPLAY_H: &str = "\28-g failed29-30-Caused by:31- 0: f failed32- 1: oh no!\25+g failed: f failed: oh no!\26 ";27 28 const EXPECTED_DEBUG_F: &str = "\29 oh no!82 unmodified lines