how.rs/src/lib.rs

124 lines
3.3 KiB
Rust
Raw Normal View History

2022-07-21 20:40:44 -04:00
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
2022-07-21 20:39:47 -04:00
#![cfg_attr(feature = "backtrace", feature(backtrace))]
2022-07-21 20:40:44 -04:00
mod sealed;
pub(crate) use sealed::seal;
2022-07-21 20:39:47 -04:00
mod context;
2022-07-21 20:40:44 -04:00
pub use context::{Context, IntoContext};
2022-07-21 20:39:47 -04:00
mod report;
2022-07-21 20:40:44 -04:00
use report::report_write;
pub use report::{Report, ReportOpts};
mod into;
pub(crate) use into::IntoResultHow;
2022-07-21 20:39:47 -04:00
mod explain;
pub use explain::Explain;
2022-07-21 20:40:44 -04:00
#[cfg(feature = "termination")]
mod termination;
#[cfg(feature = "termination")]
pub use termination::TerminationResult;
2022-07-21 20:39:47 -04:00
pub type Result<T, E = How> = std::result::Result<T, E>;
/// Does not implement [`std::error::Error`] to allow a [`From`] implementation for all other error types.
2022-07-21 20:40:44 -04:00
pub struct How(Box<HowInner>);
struct HowInner {
2022-07-21 20:39:47 -04:00
/// When true, the error will cause branchers to abort.
classified: bool,
2022-07-21 20:40:44 -04:00
// TODO: consider storing this vec inline (sharing the allocation with rest of the struct.
// Probably move after `backtrace`)
2022-07-21 20:39:47 -04:00
context: Vec<Context>,
#[cfg(feature = "backtrace")]
backtrace: std::backtrace::Backtrace,
}
2022-07-21 20:40:44 -04:00
impl std::fmt::Debug for How {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut b = f.debug_struct(std::any::type_name::<Self>());
let b = b
.field("classified", &self.0.classified)
.field("context", &self.0.context);
#[cfg(feature = "backtrace")]
let b = b.field("backtrace", &self.0.backtrace);
b.finish()
}
}
2022-07-21 20:39:47 -04:00
impl How {
2022-07-21 20:40:44 -04:00
#[must_use]
2022-07-21 20:39:47 -04:00
pub fn new(context: impl IntoContext) -> Self {
2022-07-21 20:40:44 -04:00
Self(Box::new(HowInner {
2022-07-21 20:39:47 -04:00
classified: false,
2022-07-21 20:40:44 -04:00
context: {
let mut vec = Vec::with_capacity(2);
vec.push(context.into_context());
vec
},
2022-07-21 20:39:47 -04:00
#[cfg(feature = "backtrace")]
backtrace: std::backtrace::Backtrace::capture(),
2022-07-21 20:40:44 -04:00
}))
2022-07-21 20:39:47 -04:00
}
2022-07-21 20:40:44 -04:00
#[must_use]
2022-07-21 20:39:47 -04:00
pub fn clone_without_backtrace(&self) -> Self {
2022-07-21 20:40:44 -04:00
Self(Box::new(HowInner {
classified: self.0.classified,
context: self.0.context.clone(),
2022-07-21 20:39:47 -04:00
#[cfg(feature = "backtrace")]
backtrace: std::backtrace::Backtrace::disabled(),
2022-07-21 20:40:44 -04:00
}))
2022-07-21 20:39:47 -04:00
}
#[inline]
2022-07-21 20:40:44 -04:00
#[must_use]
pub fn classified(mut self) -> Self {
self.0.classified = true;
2022-07-21 20:39:47 -04:00
self
}
2022-07-21 20:40:44 -04:00
#[inline]
#[must_use]
pub const fn is_classified(&self) -> bool {
self.0.classified
2022-07-21 20:39:47 -04:00
}
2022-07-21 20:40:44 -04:00
}
impl explain::Sealed for How {}
impl Explain for How {
type Output = Self;
2022-07-21 20:39:47 -04:00
#[inline]
2022-07-21 20:40:44 -04:00
#[must_use]
fn context(mut self, context: impl IntoContext) -> Self {
self.0.context.push(context.into_context());
self
2022-07-21 20:39:47 -04:00
}
}
impl std::fmt::Display for How {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2022-07-21 20:40:44 -04:00
let mut opts = ReportOpts::default();
let mut ctxs = self.0.context.iter().rev();
let ctx = ctxs.next().expect("`How` created with no context.");
report_write!(f, &opts, "{ctx}")?;
for ctx in ctxs {
report_write!(f, &opts, "\n")?;
report_write!(f, &opts.indent().next(), "{ctx}")?;
2022-07-21 20:39:47 -04:00
opts = opts.indent();
}
#[cfg(feature = "backtrace")]
{
2022-07-21 20:40:44 -04:00
opts = opts.indent();
report_write!(f, &opts, "\n{}", self.0.backtrace)?;
}
2022-07-21 20:39:47 -04:00
Ok(())
}
}