diff --git a/src/eyreish/error.rs b/src/eyreish/error.rs index b078e5f..0f45dc9 100644 --- a/src/eyreish/error.rs +++ b/src/eyreish/error.rs @@ -7,7 +7,8 @@ use std::error::Error as StdError; use super::Report; use super::ReportHandler; use crate::chain::Chain; -use crate::Diagnostic; +use crate::eyreish::wrapper::WithSourceCode; +use crate::{Diagnostic, SourceCode}; use core::ops::{Deref, DerefMut}; impl Report { @@ -386,6 +387,15 @@ impl Report { pub fn handler_mut(&mut self) -> &mut dyn ReportHandler { self.inner.handler.as_mut().unwrap().as_mut() } + + /// Provide source code for this error + pub fn with_source_code(self, source_code: impl SourceCode + Send + Sync + 'static) -> Report { + WithSourceCode { + source_code, + error: self, + } + .into() + } } impl From for Report diff --git a/src/eyreish/wrapper.rs b/src/eyreish/wrapper.rs index f3605e6..a5e67db 100644 --- a/src/eyreish/wrapper.rs +++ b/src/eyreish/wrapper.rs @@ -2,7 +2,7 @@ use core::fmt::{self, Debug, Display}; use std::error::Error as StdError; -use crate::{Diagnostic, LabeledSpan}; +use crate::{Diagnostic, LabeledSpan, Report, SourceCode}; use crate as miette; @@ -117,3 +117,92 @@ impl Display for BoxedError { } impl StdError for BoxedError {} + +pub(crate) struct WithSourceCode { + pub(crate) error: E, + pub(crate) source_code: C, +} + +impl Diagnostic for WithSourceCode { + fn code<'a>(&'a self) -> Option> { + self.error.code() + } + + fn severity(&self) -> Option { + self.error.severity() + } + + fn help<'a>(&'a self) -> Option> { + self.error.help() + } + + fn url<'a>(&'a self) -> Option> { + self.error.url() + } + + fn labels<'a>(&'a self) -> Option + 'a>> { + self.error.labels() + } + + fn source_code(&self) -> Option<&dyn miette::SourceCode> { + Some(&self.source_code) + } + + fn related<'a>(&'a self) -> Option + 'a>> { + self.error.related() + } +} + +impl Diagnostic for WithSourceCode { + fn code<'a>(&'a self) -> Option> { + self.error.code() + } + + fn severity(&self) -> Option { + self.error.severity() + } + + fn help<'a>(&'a self) -> Option> { + self.error.help() + } + + fn url<'a>(&'a self) -> Option> { + self.error.url() + } + + fn labels<'a>(&'a self) -> Option + 'a>> { + self.error.labels() + } + + fn source_code(&self) -> Option<&dyn miette::SourceCode> { + Some(&self.source_code) + } + + fn related<'a>(&'a self) -> Option + 'a>> { + self.error.related() + } +} + +impl Debug for WithSourceCode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Debug::fmt(&self.error, f) + } +} + +impl Display for WithSourceCode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Display::fmt(&self.error, f) + } +} + +impl StdError for WithSourceCode { + fn source(&self) -> Option<&(dyn StdError + 'static)> { + self.error.source() + } +} + +impl StdError for WithSourceCode { + fn source(&self) -> Option<&(dyn StdError + 'static)> { + self.error.source() + } +} diff --git a/tests/graphical.rs b/tests/graphical.rs index 4f81ef8..4503f8f 100644 --- a/tests/graphical.rs +++ b/tests/graphical.rs @@ -186,6 +186,41 @@ fn single_line_highlight() -> Result<(), MietteError> { Ok(()) } +#[test] +fn external_source() -> Result<(), MietteError> { + #[derive(Debug, Diagnostic, Error)] + #[error("oops!")] + #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] + struct MyBad { + #[label("this bit here")] + highlight: SourceSpan, + } + + let src = "source\n text\n here".to_string(); + let err = Report::from(MyBad { + highlight: (9, 4).into(), + }) + .with_source_code(NamedSource::new("bad_file.rs", src)); + let out = fmt_report(err); + println!("Error: {}", out); + let expected = r#"oops::my::bad + + × oops! + ╭─[bad_file.rs:1:1] + 1 │ source + 2 │ text + · ──┬─ + · ╰── this bit here + 3 │ here + ╰──── + help: try doing it better next time? +"# + .trim_start() + .to_string(); + assert_eq!(expected, out); + Ok(()) +} + #[test] fn single_line_highlight_offset_zero() -> Result<(), MietteError> { #[derive(Debug, Diagnostic, Error)]