Run `cargo fmt`

This commit is contained in:
Michael Pfaff 2024-03-20 23:15:32 -04:00
parent fb08be31d4
commit cab16130f7
2 changed files with 37 additions and 13 deletions

View File

@ -1,16 +1,19 @@
use std::borrow::Cow;
#[cfg(feature = "backtrace")]
use std::backtrace::BacktraceStatus;
use std::borrow::Cow;
use std::fmt;
use std::panic::Location;
use std::sync::Arc;
use crate::ReportOpts;
use crate::report::report_write;
use crate::ReportOpts;
/// Provides context furthering the explanation of *how* you got to an error.
#[derive(Debug)]
#[cfg_attr(any(feature = "arc-backtrace", feature = "clone-with-caveats"), derive(Clone))]
#[cfg_attr(
any(feature = "arc-backtrace", feature = "clone-with-caveats"),
derive(Clone)
)]
pub struct Context {
pub(crate) detail: Detail,
pub(crate) extra: Vec<Detail>,
@ -18,7 +21,10 @@ pub struct Context {
impl Context {
pub(crate) fn new(detail: Detail) -> Self {
Self { detail, extra: Vec::new() }
Self {
detail,
extra: Vec::new(),
}
}
pub fn detail(&self) -> &Detail {
@ -65,7 +71,10 @@ impl fmt::Display for Context {
}
#[derive(Debug)]
#[cfg_attr(any(feature = "arc-backtrace", feature = "clone-with-caveats"), derive(Clone))]
#[cfg_attr(
any(feature = "arc-backtrace", feature = "clone-with-caveats"),
derive(Clone)
)]
#[non_exhaustive]
pub enum Detail {
Str(&'static str),
@ -97,7 +106,10 @@ pub struct PrivateError(pub(crate) Arc<dyn std::error::Error + Send + Sync>);
#[cfg(feature = "backtrace")]
#[derive(Debug)]
#[cfg_attr(any(feature = "arc-backtrace", feature = "clone-with-caveats"), derive(Clone))]
#[cfg_attr(
any(feature = "arc-backtrace", feature = "clone-with-caveats"),
derive(Clone)
)]
pub struct PrivateBacktrace(pub(crate) Backtrace);
// will be replaced with std::backtrace::Backtrace if and when it is Clone
@ -113,12 +125,16 @@ pub(crate) enum Backtrace {
Other(std::backtrace::Backtrace),
}
#[cfg(all(feature = "backtrace", feature = "clone-with-caveats", not(feature = "arc-backtrace")))]
#[cfg(all(
feature = "backtrace",
feature = "clone-with-caveats",
not(feature = "arc-backtrace")
))]
impl Clone for Backtrace {
fn clone(&self) -> Self {
match self {
Self::Unsupported => Self::Unsupported,
_ => Self::Disabled
_ => Self::Disabled,
}
}
}
@ -130,9 +146,13 @@ impl fmt::Display for Detail {
Self::String(s) => f.write_str(s),
Self::Location(l) => write!(f, "at {l}"),
#[cfg(feature = "backtrace")]
Self::Backtrace(PrivateBacktrace(Backtrace::Unsupported)) => f.write_str("I'd like to show you a backtrace,\n but it's not supported on your platform"),
Self::Backtrace(PrivateBacktrace(Backtrace::Unsupported)) => f.write_str(
"I'd like to show you a backtrace,\n but it's not supported on your platform",
),
#[cfg(feature = "backtrace")]
Self::Backtrace(PrivateBacktrace(Backtrace::Disabled)) => f.write_str("If you'd like a backtrace,\n try again with RUST_BACKTRACE=1"),
Self::Backtrace(PrivateBacktrace(Backtrace::Disabled)) => {
f.write_str("If you'd like a backtrace,\n try again with RUST_BACKTRACE=1")
}
#[cfg(feature = "backtrace")]
Self::Backtrace(PrivateBacktrace(Backtrace::Other(bt))) => {
f.write_str(if bt.status() == BacktraceStatus::Captured {
@ -141,7 +161,7 @@ impl fmt::Display for Detail {
"I can't tell if backtraces are working,\n but I'll give it a go:\n"
})?;
write!(f, "{}", bt)
},
}
Self::Error(PrivateError(e)) => e.fmt(f),
Self::Context(c) => c.fmt(f),
}

View File

@ -33,7 +33,9 @@ impl ReportOpts {
macro_rules! report_write {
($f:expr, $opts:expr, $msg:literal$(, $($tt:tt)+)?) => {
<::std::fmt::Arguments<'_> as $crate::Report>::fmt(&::std::format_args!($msg$(, $($tt)+)?), $f, $opts)
<::std::fmt::Arguments<'_> as $crate::Report>::fmt(&::std::format_args!(
$msg$(, $($tt)+)?), $f, $opts
)
};
}
pub(crate) use report_write;
@ -46,7 +48,9 @@ fn write_indent(n: usize, f: &mut impl Write) -> std::fmt::Result {
Ok(())
}
/// `Report` should format the output in a manner suitable for debugging, similar to [`std::fmt::Debug`] in terms of detail, but similar to [`std::fmt::Display`] in terms of readability. The options passed to [`Self::fmt`] must be respected by all implementations.
/// `Report` should format the output in a manner suitable for debugging, similar to
/// [`std::fmt::Debug`] in terms of detail, but similar to [`std::fmt::Display`] in terms of
/// readability. The options passed to [`Self::fmt`] must be respected by all implementations.
pub trait Report {
/// Formats the value using the given formatter and options.
fn fmt(&self, f: &mut impl Write, opts: &ReportOpts) -> std::fmt::Result;