Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch report handler to a global hook #29

Merged
merged 8 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Switch report handler to a global hook
  • Loading branch information
yaahc committed Jun 21, 2020
commit 608a16aa2c2c27eca6c88001cc94c6973c18f1d5
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ anyhow = "1.0.28"

[dependencies]
indenter = "0.3.0"
once_cell = "1.4.0"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
56 changes: 22 additions & 34 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::ContextError;
use crate::{ContextCompat, EyreHandler, Report, StdError, WrapErr};
use crate::{ContextCompat, Report, StdError, WrapErr};
use core::fmt::{self, Debug, Display, Write};

#[cfg(backtrace)]
Expand All @@ -8,34 +8,27 @@ use std::backtrace::Backtrace;
mod ext {
use super::*;

pub trait StdError<H>
where
H: EyreHandler,
{
fn ext_report<D>(self, msg: D) -> Report<H>
pub trait StdError {
fn ext_report<D>(self, msg: D) -> Report
where
D: Display + Send + Sync + 'static;
}

#[cfg(feature = "std")]
impl<E, H> StdError<H> for E
impl<E> StdError for E
where
H: EyreHandler,
E: std::error::Error + Send + Sync + 'static,
{
fn ext_report<D>(self, msg: D) -> Report<H>
fn ext_report<D>(self, msg: D) -> Report
where
D: Display + Send + Sync + 'static,
{
Report::from_msg(msg, self)
}
}

impl<H> StdError<H> for Report<H>
where
H: EyreHandler,
{
fn ext_report<D>(self, msg: D) -> Report<H>
impl StdError for Report {
fn ext_report<D>(self, msg: D) -> Report
where
D: Display + Send + Sync + 'static,
{
Expand All @@ -44,34 +37,33 @@ mod ext {
}
}

impl<T, E, H> WrapErr<T, E, H> for Result<T, E>
impl<T, E> WrapErr<T, E> for Result<T, E>
where
H: EyreHandler,
E: ext::StdError<H> + Send + Sync + 'static,
E: ext::StdError + Send + Sync + 'static,
{
fn wrap_err<D>(self, msg: D) -> Result<T, Report<H>>
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
{
self.map_err(|error| error.ext_report(msg))
}

fn wrap_err_with<D, F>(self, msg: F) -> Result<T, Report<H>>
fn wrap_err_with<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
self.map_err(|error| error.ext_report(msg()))
}

fn context<D>(self, msg: D) -> Result<T, Report<H>>
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
{
self.wrap_err(msg)
}

fn with_context<D, F>(self, msg: F) -> Result<T, Report<H>>
fn with_context<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
Expand All @@ -80,33 +72,30 @@ where
}
}

impl<T, H> ContextCompat<T, H> for Option<T>
where
H: EyreHandler,
{
fn wrap_err<D>(self, msg: D) -> Result<T, Report<H>>
impl<T> ContextCompat<T> for Option<T> {
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
{
self.context(msg)
}

fn wrap_err_with<D, F>(self, msg: F) -> Result<T, Report<H>>
fn wrap_err_with<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
self.with_context(msg)
}

fn context<D>(self, msg: D) -> Result<T, Report<H>>
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
{
self.ok_or_else(|| Report::from_display(msg))
}

fn with_context<D, F>(self, msg: F) -> Result<T, Report<H>>
fn with_context<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
Expand Down Expand Up @@ -152,9 +141,8 @@ where
}
}

impl<D, H> StdError for ContextError<D, Report<H>>
impl<D> StdError for ContextError<D, Report>
where
H: EyreHandler,
D: Display,
{
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Expand Down Expand Up @@ -185,8 +173,8 @@ impl Write for Quoted<&mut fmt::Formatter<'_>> {
pub(crate) mod private {
use super::*;

pub trait Sealed<H: EyreHandler> {}
pub trait Sealed {}

impl<T, E, H: EyreHandler> Sealed<H> for Result<T, E> where E: ext::StdError<H> {}
impl<T, H: EyreHandler> Sealed<H> for Option<T> {}
impl<T, E> Sealed for Result<T, E> where E: ext::StdError {}
impl<T> Sealed for Option<T> {}
}
Loading