Skip to content

Commit

Permalink
feat(report): add with_source_code
Browse files Browse the repository at this point in the history
Fixes #64
  • Loading branch information
tailhook authored and zkat committed Feb 20, 2022
1 parent d1eae3a commit 5051926
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/eyreish/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<E> From<E> for Report
Expand Down
91 changes: 90 additions & 1 deletion src/eyreish/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -117,3 +117,92 @@ impl Display for BoxedError {
}

impl StdError for BoxedError {}

pub(crate) struct WithSourceCode<E, C> {
pub(crate) error: E,
pub(crate) source_code: C,
}

impl<E: Diagnostic, C: SourceCode> Diagnostic for WithSourceCode<E, C> {
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.error.code()
}

fn severity(&self) -> Option<miette::Severity> {
self.error.severity()
}

fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.error.help()
}

fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.error.url()
}

fn labels<'a>(&'a self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + 'a>> {
self.error.labels()
}

fn source_code(&self) -> Option<&dyn miette::SourceCode> {
Some(&self.source_code)
}

fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
self.error.related()
}
}

impl<C: SourceCode> Diagnostic for WithSourceCode<Report, C> {
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.error.code()
}

fn severity(&self) -> Option<miette::Severity> {
self.error.severity()
}

fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.error.help()
}

fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.error.url()
}

fn labels<'a>(&'a self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + 'a>> {
self.error.labels()
}

fn source_code(&self) -> Option<&dyn miette::SourceCode> {
Some(&self.source_code)
}

fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
self.error.related()
}
}

impl<E: Debug, C> Debug for WithSourceCode<E, C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.error, f)
}
}

impl<E: Display, C> Display for WithSourceCode<E, C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.error, f)
}
}

impl<E: StdError, C> StdError for WithSourceCode<E, C> {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.error.source()
}
}

impl<C> StdError for WithSourceCode<Report, C> {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.error.source()
}
}
35 changes: 35 additions & 0 deletions tests/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 5051926

Please sign in to comment.