Skip to content

Commit

Permalink
feat(derive): allow Report in related (#121)
Browse files Browse the repository at this point in the history
Fixes: #119
  • Loading branch information
tailhook authored Feb 22, 2022
1 parent ea5fdaf commit 75d4505
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion miette-derive/src/related.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ impl Related {
let rel = &self.0;
Some(quote! {
fn related<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::iter::Iterator<Item = &'a dyn miette::Diagnostic> + 'a>> {
std::option::Option::Some(std::boxed::Box::new(self.#rel.iter().map(|x| -> &(dyn miette::Diagnostic) { &*x })))
use ::core::borrow::Borrow;
std::option::Option::Some(std::boxed::Box::new(
self.#rel.iter().map(|x| -> &(dyn miette::Diagnostic) { &*x.borrow() })
))
}
})
}
Expand Down
6 changes: 6 additions & 0 deletions src/eyreish/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,3 +727,9 @@ impl AsRef<dyn Diagnostic> for Report {
&**self
}
}

impl std::borrow::Borrow<dyn Diagnostic> for Report {
fn borrow(&self) -> &(dyn Diagnostic + 'static) {
self.as_ref()
}
}
13 changes: 12 additions & 1 deletion tests/derive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use miette::{Diagnostic, Severity, SourceSpan};
use miette::{Diagnostic, Report, Severity, SourceSpan};
use thiserror::Error;

#[test]
Expand Down Expand Up @@ -32,6 +32,17 @@ fn related() {
struct Baz;
}

#[test]
fn related_report() {
#[derive(Error, Debug, Diagnostic)]
#[error("welp")]
#[diagnostic(code(foo::bar::baz))]
struct Foo {
#[related]
related: Vec<Report>,
}
}

#[test]
fn basic_struct() {
#[derive(Debug, Diagnostic, Error)]
Expand Down

0 comments on commit 75d4505

Please sign in to comment.