fix(reporter): fix reporter and tests... again

This commit is contained in:
Kat Marchán 2021-08-10 22:42:26 -04:00
parent 0d2e3312a4
commit d201dde4b5
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
3 changed files with 16 additions and 10 deletions

View File

@ -66,12 +66,12 @@ Next, we have to implement the `Diagnostic` trait for it:
use miette::{Diagnostic, Severity, DiagnosticSnippet}; use miette::{Diagnostic, Severity, DiagnosticSnippet};
impl Diagnostic for MyBad { impl Diagnostic for MyBad {
fn code(&self) -> &(dyn std::fmt::Display + 'static) { fn code(&self) -> Box<dyn std::fmt::Display> {
&"oops::my::bad" Box::new("oops::my::bad")
} }
fn help(&self) -> Option<&(dyn std::fmt::Display)> { fn help(&self) -> Option<Box<dyn std::fmt::Display>> {
Some(&"try doing it better next time?") Some(Box::new("try doing it better next time?"))
} }
fn snippets(&self) -> Option<Box<dyn Iterator<Item = DiagnosticSnippet>>> { fn snippets(&self) -> Option<Box<dyn Iterator<Item = DiagnosticSnippet>>> {

View File

@ -23,11 +23,12 @@ impl MietteReporter {
snippet: &DiagnosticSnippet, snippet: &DiagnosticSnippet,
) -> fmt::Result { ) -> fmt::Result {
use fmt::Write as _; use fmt::Write as _;
write!(f, "\n[{}]", snippet.source_name)?; write!(f, "[{}]", snippet.source_name)?;
if let Some(msg) = &snippet.message { if let Some(msg) = &snippet.message {
write!(f, " {}:", msg)?; write!(f, " {}:", msg)?;
} }
writeln!(f)?; writeln!(f)?;
writeln!(f)?;
let context_data = snippet let context_data = snippet
.source .source
.read_span(&snippet.context) .read_span(&snippet.context)
@ -113,7 +114,8 @@ impl DiagnosticReporter for MietteReporter {
writeln!(f, "{}[{}]: {}", sev, diagnostic.code(), diagnostic)?; writeln!(f, "{}[{}]: {}", sev, diagnostic.code(), diagnostic)?;
if let Some(cause) = diagnostic.source() { if let Some(cause) = diagnostic.source() {
write!(f, "\n\nCaused by:")?; writeln!(f)?;
write!(f, "Caused by:")?;
let multiple = cause.source().is_some(); let multiple = cause.source().is_some();
for (n, error) in Chain::new(cause).enumerate() { for (n, error) in Chain::new(cause).enumerate() {
@ -127,15 +129,19 @@ impl DiagnosticReporter for MietteReporter {
} }
if let Some(snippets) = diagnostic.snippets() { if let Some(snippets) = diagnostic.snippets() {
writeln!(f)?; let mut pre = false;
for snippet in snippets { for snippet in snippets {
if !pre {
writeln!(f)?;
pre = true;
}
self.render_snippet(f, &snippet)?; self.render_snippet(f, &snippet)?;
} }
} }
if let Some(help) = diagnostic.help() { if let Some(help) = diagnostic.help() {
writeln!(f)?; writeln!(f)?;
write!(f, "﹦{}", help)?; writeln!(f, "﹦{}", help)?;
} }
Ok(()) Ok(())

View File

@ -38,7 +38,7 @@ fn basic() -> Result<(), MietteError> {
}; };
let out = format!("{:?}", err); let out = format!("{:?}", err);
assert_eq!( 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 out
); );
Ok(()) Ok(())
@ -68,6 +68,6 @@ fn fancy() -> Result<(), MietteError> {
}; };
let out = format!("{:?}", err); let out = format!("{:?}", err);
// println!("{}", out); // 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(()) Ok(())
} }