From 75d4505e7d55e816cac071eb126213b72bf48982 Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 22 Feb 2022 05:12:37 +0200 Subject: [PATCH] feat(derive): allow `Report` in `related` (#121) Fixes: #119 --- miette-derive/src/related.rs | 5 ++++- src/eyreish/error.rs | 6 ++++++ tests/derive.rs | 13 ++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/miette-derive/src/related.rs b/miette-derive/src/related.rs index b81df5fd..5533f2d0 100644 --- a/miette-derive/src/related.rs +++ b/miette-derive/src/related.rs @@ -69,7 +69,10 @@ impl Related { let rel = &self.0; Some(quote! { fn related<'a>(&'a self) -> std::option::Option + '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() }) + )) } }) } diff --git a/src/eyreish/error.rs b/src/eyreish/error.rs index 0f45dc9d..bb31d592 100644 --- a/src/eyreish/error.rs +++ b/src/eyreish/error.rs @@ -727,3 +727,9 @@ impl AsRef for Report { &**self } } + +impl std::borrow::Borrow for Report { + fn borrow(&self) -> &(dyn Diagnostic + 'static) { + self.as_ref() + } +} diff --git a/tests/derive.rs b/tests/derive.rs index f1767c48..60a77630 100644 --- a/tests/derive.rs +++ b/tests/derive.rs @@ -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, + } +} + #[test] fn basic_struct() { #[derive(Debug, Diagnostic, Error)]