feat(Report): adds `.context()` method to the `Report` (#109)

Techically there was a hidden undocumented `context` method. But it was
just copied from the `eyre` and there is no evidence that it was used by
any user in miette (the method was an alias for `.handler()`).

Fixes #108
This commit is contained in:
Paul Colomiets 2022-01-18 03:16:08 +02:00 committed by GitHub
parent f158f4e370
commit 2649fd27c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 14 deletions

View File

@ -220,6 +220,14 @@ impl Report {
unsafe { Report::construct(error, vtable, handler) } unsafe { Report::construct(error, vtable, handler) }
} }
/// Compatibility re-export of wrap_err for interop with `anyhow`
pub fn context<D>(self, msg: D) -> Self
where
D: Display + Send + Sync + 'static,
{
self.wrap_err(msg)
}
/// An iterator of the chain of source errors contained by this Report. /// An iterator of the chain of source errors contained by this Report.
/// ///
/// This iterator will visit every error in the cause chain of this error /// This iterator will visit every error in the cause chain of this error
@ -378,18 +386,6 @@ impl Report {
pub fn handler_mut(&mut self) -> &mut dyn ReportHandler { pub fn handler_mut(&mut self) -> &mut dyn ReportHandler {
self.inner.handler.as_mut().unwrap().as_mut() self.inner.handler.as_mut().unwrap().as_mut()
} }
/// Get a reference to the Handler for this Report.
#[doc(hidden)]
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 ReportHandler {
self.inner.handler.as_mut().unwrap().as_mut()
}
} }
impl<E> From<E> for Report impl<E> From<E> for Report

View File

@ -1,7 +1,7 @@
#[test] #[test]
fn test_context() { fn test_handler() {
use miette::{miette, Report}; use miette::{miette, Report};
let error: Report = miette!("oh no!"); let error: Report = miette!("oh no!");
let _ = error.context(); let _ = error.handler();
} }