From d201dde4b559a2baa4259a0845582a5d14453c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Tue, 10 Aug 2021 22:42:26 -0400 Subject: [PATCH] fix(reporter): fix reporter and tests... again --- README.md | 8 ++++---- src/reporter.rs | 14 ++++++++++---- tests/reporter.rs | 4 ++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8f3d766..21d5e34 100644 --- a/README.md +++ b/README.md @@ -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 { + Box::new("oops::my::bad") } - fn help(&self) -> Option<&(dyn std::fmt::Display)> { - Some(&"try doing it better next time?") + fn help(&self) -> Option> { + Some(Box::new("try doing it better next time?")) } fn snippets(&self) -> Option>> { diff --git a/src/reporter.rs b/src/reporter.rs index 9ab70d3..e531103 100644 --- a/src/reporter.rs +++ b/src/reporter.rs @@ -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) @@ -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() { @@ -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(()) diff --git a/tests/reporter.rs b/tests/reporter.rs index 7a52aee..fa2c01c 100644 --- a/tests/reporter.rs +++ b/tests/reporter.rs @@ -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(()) @@ -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(()) }