Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't override provided source code #300

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 87 additions & 2 deletions src/eyreish/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down Expand Up @@ -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>> {
Expand Down Expand Up @@ -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)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't use derive macro here. Looks like it always expands to miette::, instead of crate::. This may be fixed with $crate:: in macro implementation, I think

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a good patch to have

#[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");
Comment on lines +282 to +292
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is a better way to check that sources are equal?

}

#[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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug printing without fancy feature is broken

assert!(message.contains(inner_source));
assert!(message.contains(outer_source));
}
}