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};
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())?;
let mut chain = self.chain().skip(1).enumerate().peekable();
if let Some((n, error)) = chain.next() {
write!(f, "\n\nCaused by:\n ")?;
if chain.peek().is_some() {
write!(f, "{}: ", n)?;
}
write!(f, "{}", error)?;
for (n, error) in chain {
write!(f, "\n {}: {}", n, error)?;
if f.alternate() {
for cause in self.chain().skip(1) {
write!(f, ": {}", cause)?;
}
}
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 {
if f.alternate() {
return Debug::fmt(self.error(), f);
}
self.print_chain(f)?;
writeln!(f)?;
writeln!(f, "{}", self.error())?;
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)]
{

View File

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

View File

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