Skip to content

Commit

Permalink
fix(reporter): fix reporter and tests... again
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 11, 2021
1 parent 0d2e331 commit d201dde
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ Next, we have to implement the `Diagnostic` trait for it:
use miette::{Diagnostic, Severity, DiagnosticSnippet};

impl Diagnostic for MyBad {
fn code(&self) -> &(dyn std::fmt::Display + 'static) {
&"oops::my::bad"
fn code(&self) -> Box<dyn std::fmt::Display> {
Box::new("oops::my::bad")
}

fn help(&self) -> Option<&(dyn std::fmt::Display)> {
Some(&"try doing it better next time?")
fn help(&self) -> Option<Box<dyn std::fmt::Display>> {
Some(Box::new("try doing it better next time?"))
}

fn snippets(&self) -> Option<Box<dyn Iterator<Item = DiagnosticSnippet>>> {
Expand Down
14 changes: 10 additions & 4 deletions src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ impl MietteReporter {
snippet: &DiagnosticSnippet,
) -> fmt::Result {
use fmt::Write as _;
write!(f, "\n[{}]", snippet.source_name)?;
write!(f, "[{}]", snippet.source_name)?;
if let Some(msg) = &snippet.message {
write!(f, " {}:", msg)?;
}
writeln!(f)?;
writeln!(f)?;
let context_data = snippet
.source
.read_span(&snippet.context)
Expand Down Expand Up @@ -113,7 +114,8 @@ impl DiagnosticReporter for MietteReporter {
writeln!(f, "{}[{}]: {}", sev, diagnostic.code(), diagnostic)?;

if let Some(cause) = diagnostic.source() {
write!(f, "\n\nCaused by:")?;
writeln!(f)?;
write!(f, "Caused by:")?;
let multiple = cause.source().is_some();

for (n, error) in Chain::new(cause).enumerate() {
Expand All @@ -127,15 +129,19 @@ impl DiagnosticReporter for MietteReporter {
}

if let Some(snippets) = diagnostic.snippets() {
writeln!(f)?;
let mut pre = false;
for snippet in snippets {
if !pre {
writeln!(f)?;
pre = true;
}
self.render_snippet(f, &snippet)?;
}
}

if let Some(help) = diagnostic.help() {
writeln!(f)?;
write!(f, "﹦{}", help)?;
writeln!(f, "﹦{}", help)?;
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions tests/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn basic() -> Result<(), MietteError> {
};
let out = format!("{:?}", err);
assert_eq!(
"Error[oops::my::bad]: oops!\n\n﹦try doing it better next time?".to_string(),
"Error[oops::my::bad]: oops!\n\n﹦try doing it better next time?\n".to_string(),
out
);
Ok(())
Expand Down Expand Up @@ -68,6 +68,6 @@ fn fancy() -> Result<(), MietteError> {
};
let out = format!("{:?}", err);
// println!("{}", out);
assert_eq!("Error[oops::my::bad]: oops!\n\n[bad_file.rs] This is the part that broke:\n\n 1 | source\n 2 | text\n ⫶ | ^^^^ this bit here\n 3 | here\n﹦try doing it better next time?".to_string(), out);
assert_eq!("Error[oops::my::bad]: oops!\n\n[bad_file.rs] This is the part that broke:\n\n 1 | source\n 2 | text\n ⫶ | ^^^^ this bit here\n 3 | here\n\n﹦try doing it better next time?\n".to_string(), out);
Ok(())
}

0 comments on commit d201dde

Please sign in to comment.