feat(derive): allow `Report` in `related`

Fixes #119
This commit is contained in:
Paul Colomiets 2022-02-22 02:47:15 +02:00
parent ea5fdaf562
commit 1d111e87ba
3 changed files with 22 additions and 2 deletions

View File

@ -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() })
))
}
})
}

View File

@ -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()
}
}

View File

@ -1,4 +1,4 @@
use miette::{Diagnostic, Severity, SourceSpan};
use miette::{Diagnostic, Report, Severity, SourceSpan};
use thiserror::Error;
#[test]
@ -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)]