mirror of https://github.com/zkat/miette.git
Don't override provided source code
This commit is contained in:
parent
ba313282a8
commit
75c05cdd5f
|
|
@ -163,7 +163,7 @@ impl<E: Diagnostic, C: SourceCode> Diagnostic for WithSourceCode<E, C> {
|
|||
}
|
||||
|
||||
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
|
||||
Some(&self.source_code)
|
||||
self.error.source_code().or(Some(&self.source_code))
|
||||
}
|
||||
|
||||
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
|
||||
|
|
@ -197,7 +197,7 @@ impl<C: SourceCode> Diagnostic for WithSourceCode<Report, C> {
|
|||
}
|
||||
|
||||
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
|
||||
Some(&self.source_code)
|
||||
self.error.source_code().or(Some(&self.source_code))
|
||||
}
|
||||
|
||||
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
|
||||
|
|
@ -232,3 +232,88 @@ impl<C> StdError for WithSourceCode<Report, C> {
|
|||
self.error.source()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{Diagnostic, LabeledSpan, Report, SourceCode, SourceSpan};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[error("inner")]
|
||||
struct Inner {
|
||||
pub(crate) at: SourceSpan,
|
||||
pub(crate) source_code: Option<String>,
|
||||
}
|
||||
|
||||
impl Diagnostic for Inner {
|
||||
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
|
||||
Some(Box::new(std::iter::once(LabeledSpan::underline(self.at))))
|
||||
}
|
||||
|
||||
fn source_code(&self) -> Option<&dyn SourceCode> {
|
||||
self.source_code.as_ref().map(|s| s as _)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[error("outer")]
|
||||
struct Outer {
|
||||
pub(crate) errors: Vec<Inner>,
|
||||
}
|
||||
|
||||
impl Diagnostic for Outer {
|
||||
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
|
||||
Some(Box::new(self.errors.iter().map(|e| e as _)))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_override() {
|
||||
let inner_source = "hello world";
|
||||
let outer_source = "abc";
|
||||
|
||||
let report = Report::from(Inner {
|
||||
at: (0..5).into(),
|
||||
source_code: Some(inner_source.to_string()),
|
||||
})
|
||||
.with_source_code(outer_source.to_string());
|
||||
|
||||
let underlined = String::from_utf8(
|
||||
report
|
||||
.source_code()
|
||||
.unwrap()
|
||||
.read_span(&(0..5).into(), 0, 0)
|
||||
.unwrap()
|
||||
.data()
|
||||
.to_vec(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(underlined, "hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "fancy")]
|
||||
fn two_source_codes() {
|
||||
let inner_source = "hello world";
|
||||
let outer_source = "abc";
|
||||
|
||||
let report = Report::from(Outer {
|
||||
errors: vec![
|
||||
Inner {
|
||||
at: (0..5).into(),
|
||||
source_code: Some(inner_source.to_string()),
|
||||
},
|
||||
Inner {
|
||||
at: (1..2).into(),
|
||||
source_code: None,
|
||||
},
|
||||
],
|
||||
})
|
||||
.with_source_code(outer_source.to_string());
|
||||
|
||||
let message = format!("{:?}", report);
|
||||
assert!(message.contains(inner_source));
|
||||
assert!(message.contains(outer_source));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue