mirror of https://github.com/zkat/miette.git
parent
d1eae3a6f7
commit
50519264d4
|
|
@ -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<E> From<E> for Report
|
||||
|
|
|
|||
|
|
@ -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<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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
|
|
|
|||
Loading…
Reference in New Issue