Render alt display on one line with colons

This commit is contained in:
David Tolnay 2019-11-15 21:24:46 -08:00
parent bba67262ed
commit c30a32bdda
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 20 additions and 33 deletions

View File

@ -2,39 +2,36 @@ use crate::error::ErrorImpl;
use std::fmt::{self, Debug}; use std::fmt::{self, Debug};
impl ErrorImpl<()> { impl ErrorImpl<()> {
fn print_chain(&self, f: &mut fmt::Formatter) -> fmt::Result { pub(crate) fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.error())?; write!(f, "{}", self.error())?;
let mut chain = self.chain().skip(1).enumerate().peekable(); if f.alternate() {
if let Some((n, error)) = chain.next() { for cause in self.chain().skip(1) {
write!(f, "\n\nCaused by:\n ")?; write!(f, ": {}", cause)?;
if chain.peek().is_some() {
write!(f, "{}: ", n)?;
}
write!(f, "{}", error)?;
for (n, error) in chain {
write!(f, "\n {}: {}", n, error)?;
} }
} }
Ok(()) Ok(())
} }
pub(crate) fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {
if f.alternate() {
self.print_chain(f)
} else {
write!(f, "{}", self.error())
}
}
pub(crate) fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result { pub(crate) fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {
if f.alternate() { if f.alternate() {
return Debug::fmt(self.error(), f); return Debug::fmt(self.error(), f);
} }
self.print_chain(f)?; writeln!(f, "{}", self.error())?;
writeln!(f)?;
let mut chain = self.chain().skip(1).enumerate().peekable();
if let Some((n, error)) = chain.next() {
write!(f, "\nCaused by:\n ")?;
if chain.peek().is_some() {
write!(f, "{}: ", n)?;
}
writeln!(f, "{}", error)?;
for (n, error) in chain {
writeln!(f, " {}: {}", n, error)?;
}
}
#[cfg(backtrace)] #[cfg(backtrace)]
{ {

View File

@ -213,10 +213,7 @@ pub use anyhow as format_err;
/// alternate selector "{:#}". /// alternate selector "{:#}".
/// ///
/// ```console /// ```console
/// failed to read instrs from ./path/to/instrs.jsox /// failed to read instrs from ./path/to/instrs.jsox: No such file or directory (os error 2)
///
/// Caused by:
/// No such file or directory (os error 2)
/// ``` /// ```
/// ///
/// The Debug format "{:?}" includes your backtrace if one was captured. Note /// The Debug format "{:?}" includes your backtrace if one was captured. Note

View File

@ -18,18 +18,11 @@ oh no!\
"; ";
const EXPECTED_ALTDISPLAY_G: &str = "\ const EXPECTED_ALTDISPLAY_G: &str = "\
f failed f failed: oh no!\
Caused by:
oh no!\
"; ";
const EXPECTED_ALTDISPLAY_H: &str = "\ const EXPECTED_ALTDISPLAY_H: &str = "\
g failed g failed: f failed: oh no!\
Caused by:
0: f failed
1: oh no!\
"; ";
const EXPECTED_DEBUG_F: &str = "\ const EXPECTED_DEBUG_F: &str = "\