feat(source-code): Don't override provided source code (#300)

BREAKING CHANGE: Source code is no longer overridden if it was provided by the diagnostic's own `source_code()` impl.
This commit is contained in:
Gavrilikhin Daniil 2024-02-04 06:33:59 +03:00 committed by GitHub
parent c0a298e5a8
commit 0d5c2ce753
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 87 additions and 2 deletions

View File

@ -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));
}
}