Skip to content

Commit

Permalink
most tests looking good
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Sep 5, 2021
1 parent 4718c99 commit 4d2b7d4
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 140 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ owo-colors = "2.0.0"
atty = "0.2.14"
ci_info = "0.14.2"
indenter = "0.3.0"
pyo3 = { version = "0.13", optional = true, default-features = false }

[dev-dependencies]
semver = "1.0.4"
Expand All @@ -28,7 +27,6 @@ semver = "1.0.4"
futures = { version = "0.3", default-features = false }
rustversion = "1.0"
trybuild = { version = "1.0.19", features = ["diff"] }
backtrace = "0.3.46"
anyhow = "1.0.28"
syn = { version = "1.0", features = ["full"] }

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ diagnostic error code: ruget::api::bad_json
- Unique error codes on every [Diagnostic].
- Custom links to get more details on error codes.
- Super handy derive macro for defining diagnostic metadata.
- Lightweight [`anyhow`](https://docs.rs/anyhow)/[`eyre`](https://docs.rs/eyre)-style error wrapper type, [DiagnosticReport],
- [`anyhow`](https://docs.rs/anyhow)/[`eyre`](https://docs.rs/eyre)-compatible error wrapper type, [Report],
which can be returned from `main`.
- Generic support for arbitrary [Source]s for snippet data, with default support for `String`s included.

The `miette` crate also comes bundled with a default [DiagnosticReportPrinter] with the following features:
The `miette` crate also comes bundled with a default [ReportHandler] with the following features:

- Fancy graphical [diagnostic output](#about), using ANSI/Unicode text
- single- and multi-line highlighting support
Expand Down Expand Up @@ -191,7 +191,7 @@ Application code tends to work a little differently than libraries. You don't
always need or care to define dedicated error wrappers for errors coming from
external libraries and tools.

For this situation, `miette` includes two tools: [DiagnosticReport] and
For this situation, `miette` includes two tools: [Report] and
[IntoDiagnostic]. They work in tandem to make it easy to convert regular
`std::error::Error`s into [Diagnostic]s. Additionally, there's a
[DiagnosticResult] type alias that you can use to be more terse:
Expand Down Expand Up @@ -330,7 +330,7 @@ pub struct MyErrorType {
[`color-eyre`](https://crates.io/crates/color-eyre): these two enormously
influential error handling libraries have pushed forward the experience of
application-level error handling and error reporting. `miette`'s
`DiagnosticReport` type is an attempt at a very very rough version of their
`Report` type is an attempt at a very very rough version of their
`Report` types.
- [`thiserror`](https://crates.io/crates/thiserror) for setting the standard
for library-level error definitions, and for being the inspiration behind
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub enum MietteError {
)]
OutOfBounds,

/// Returned when installing a [crate::DiagnosticReportPrinter] failed.
/// Returned when installing a [crate::ReportHandler] failed.
/// Typically, this will be because [crate::set_printer] was called twice.
#[error("Failed to install DiagnosticReportPrinter")]
#[error("Failed to install ReportHandler")]
#[diagnostic(code(miette::set_printer_failed), url(docsrs))]
SetPrinterFailure,
}
22 changes: 0 additions & 22 deletions src/eyreish/backtrace.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/eyreish/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ use core::fmt::{self, Debug, Display, Write};

use std::error::Error as StdError;

#[cfg(backtrace)]
use std::backtrace::Backtrace;

use crate::Diagnostic;

mod ext {
Expand Down Expand Up @@ -147,11 +144,6 @@ where
D: Display,
E: StdError + 'static,
{
#[cfg(backtrace)]
fn backtrace(&self) -> Option<&Backtrace> {
self.error.backtrace()
}

fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.error)
}
Expand Down
17 changes: 7 additions & 10 deletions src/eyreish/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::mem::{self, ManuallyDrop};
use core::ptr::{self, NonNull};
use std::error::Error as StdError;

use super::EyreHandler;
use super::ReportHandler;
use super::Report;
use crate::chain::Chain;
use crate::Diagnostic;
Expand Down Expand Up @@ -193,7 +193,7 @@ impl Report {
unsafe fn construct<E>(
error: E,
vtable: &'static ErrorVTable,
handler: Option<Box<dyn EyreHandler>>,
handler: Option<Box<dyn ReportHandler>>,
) -> Self
where
E: Diagnostic + Send + Sync + 'static,
Expand Down Expand Up @@ -438,24 +438,24 @@ impl Report {
}

/// Get a reference to the Handler for this Report.
pub fn handler(&self) -> &dyn EyreHandler {
pub fn handler(&self) -> &dyn ReportHandler {
self.inner.handler.as_ref().unwrap().as_ref()
}

/// Get a mutable reference to the Handler for this Report.
pub fn handler_mut(&mut self) -> &mut dyn EyreHandler {
pub fn handler_mut(&mut self) -> &mut dyn ReportHandler {
self.inner.handler.as_mut().unwrap().as_mut()
}

/// Get a reference to the Handler for this Report.
#[doc(hidden)]
pub fn context(&self) -> &dyn EyreHandler {
pub fn context(&self) -> &dyn ReportHandler {
self.inner.handler.as_ref().unwrap().as_ref()
}

/// Get a mutable reference to the Handler for this Report.
#[doc(hidden)]
pub fn context_mut(&mut self) -> &mut dyn EyreHandler {
pub fn context_mut(&mut self) -> &mut dyn ReportHandler {
self.inner.handler.as_mut().unwrap().as_mut()
}
}
Expand Down Expand Up @@ -680,7 +680,7 @@ where
#[repr(C)]
pub(crate) struct ErrorImpl<E> {
vtable: &'static ErrorVTable,
pub(crate) handler: Option<Box<dyn EyreHandler>>,
pub(crate) handler: Option<Box<dyn ReportHandler>>,
// NOTE: Don't use directly. Use only through vtable. Erased type may have
// different alignment.
_object: E,
Expand Down Expand Up @@ -789,6 +789,3 @@ impl AsRef<dyn Diagnostic> for Report {
&**self
}
}

#[cfg(feature = "pyo3")]
mod pyo3_compat;
7 changes: 0 additions & 7 deletions src/eyreish/error/pyo3_compat.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/eyreish/into_diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt;

use thiserror::Error;

use crate::{Diagnostic, Report};
Expand Down
68 changes: 28 additions & 40 deletions src/eyreish/mod.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
#![cfg_attr(backtrace, feature(backtrace))]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![allow(
clippy::needless_doctest_main,
clippy::new_ret_no_self,
clippy::wrong_self_convention
)]

#[macro_use]
mod backtrace;
mod context;
mod error;
mod fmt;
mod into_diagnostic;
mod kind;
mod macros;
mod wrapper;

pub use into_diagnostic::*;

use std::error::Error as StdError;

use self::backtrace::Backtrace;
use core::fmt::Display;
use core::mem::ManuallyDrop;
use error::ErrorImpl;

// pub use eyre as format_err;
// /// Compatibility re-export of `eyre` for interopt with `anyhow`
// pub use eyre as anyhow;
use std::error::Error as StdError;

use atty::Stream;
use once_cell::sync::OnceCell;
// #[doc(hidden)]
// pub use DefaultHandler as DefaultContext;

#[doc(hidden)]
pub use EyreHandler as EyreContext;
pub use ReportHandler as EyreContext;
#[doc(hidden)]
pub use Report as ErrReport;
/// Compatibility re-export of `Report` for interop with `anyhow`
pub use Report as Error;
/// Compatibility re-export of `WrapErr` for interop with `anyhow`
pub use WrapErr as Context;
pub use into_diagnostic::*;

use crate::{Diagnostic, GraphicalReportPrinter, NarratableReportPrinter};
use error::ErrorImpl;
use crate::{Diagnostic, GraphicalReportHandler, NarratableReportHandler};

mod context;
mod error;
mod fmt;
mod into_diagnostic;
mod kind;
mod macros;
mod wrapper;

/**
Core Diagnostic wrapper type.
Expand All @@ -51,7 +41,7 @@ pub struct Report {
}

type ErrorHook =
Box<dyn Fn(&(dyn Diagnostic + 'static)) -> Box<dyn EyreHandler> + Sync + Send + 'static>;
Box<dyn Fn(&(dyn Diagnostic + 'static)) -> Box<dyn ReportHandler> + Sync + Send + 'static>;

static HOOK: OnceCell<ErrorHook> = OnceCell::new();

Expand All @@ -77,10 +67,8 @@ pub fn set_hook(hook: ErrorHook) -> Result<(), InstallError> {

#[cfg_attr(track_caller, track_caller)]
#[cfg_attr(not(track_caller), allow(unused_mut))]
fn capture_handler(error: &(dyn Diagnostic + 'static)) -> Box<dyn EyreHandler> {
let hook = HOOK
.get_or_init(|| Box::new(get_default_printer))
.as_ref();
fn capture_handler(error: &(dyn Diagnostic + 'static)) -> Box<dyn ReportHandler> {
let hook = HOOK.get_or_init(|| Box::new(get_default_printer)).as_ref();

let mut handler = hook(error);

Expand All @@ -93,7 +81,7 @@ fn capture_handler(error: &(dyn Diagnostic + 'static)) -> Box<dyn EyreHandler> {
handler
}

fn get_default_printer(_err: &(dyn Diagnostic + 'static)) -> Box<dyn EyreHandler + 'static> {
fn get_default_printer(_err: &(dyn Diagnostic + 'static)) -> Box<dyn ReportHandler + 'static> {
let fancy = if let Ok(string) = std::env::var("NO_COLOR") {
string == "0"
} else if let Ok(string) = std::env::var("CLICOLOR") {
Expand All @@ -102,15 +90,15 @@ fn get_default_printer(_err: &(dyn Diagnostic + 'static)) -> Box<dyn EyreHandler
atty::is(Stream::Stdout) && atty::is(Stream::Stderr) && !ci_info::is_ci()
};
if fancy {
Box::new(GraphicalReportPrinter::new())
Box::new(GraphicalReportHandler::new())
} else {
Box::new(NarratableReportPrinter)
Box::new(NarratableReportHandler)
}
}

impl dyn EyreHandler {
impl dyn ReportHandler {
///
pub fn is<T: EyreHandler>(&self) -> bool {
pub fn is<T: ReportHandler>(&self) -> bool {
// Get `TypeId` of the type this function is instantiated with.
let t = core::any::TypeId::of::<T>();

Expand All @@ -122,26 +110,26 @@ impl dyn EyreHandler {
}

///
pub fn downcast_ref<T: EyreHandler>(&self) -> Option<&T> {
pub fn downcast_ref<T: ReportHandler>(&self) -> Option<&T> {
if self.is::<T>() {
unsafe { Some(&*(self as *const dyn EyreHandler as *const T)) }
unsafe { Some(&*(self as *const dyn ReportHandler as *const T)) }
} else {
None
}
}

///
pub fn downcast_mut<T: EyreHandler>(&mut self) -> Option<&mut T> {
pub fn downcast_mut<T: ReportHandler>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
unsafe { Some(&mut *(self as *mut dyn EyreHandler as *mut T)) }
unsafe { Some(&mut *(self as *mut dyn ReportHandler as *mut T)) }
} else {
None
}
}
}

/// Error Report Handler trait for customizing `eyre::Report`
pub trait EyreHandler: core::any::Any + Send + Sync {
pub trait ReportHandler: core::any::Any + Send + Sync {
/// Define the report format
///
/// Used to override the report format of `eyre::Report`
Expand Down
Loading

0 comments on commit 4d2b7d4

Please sign in to comment.