From 7920a2315c0d15f6c199ffd127d0c34756a9b034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sat, 7 Aug 2021 22:54:46 -0400 Subject: [PATCH] feat(protocol: Severity is now optional --- src/protocol.rs | 6 +++++- src/reporter.rs | 12 ++++++------ tests/reporter.rs | 7 +++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/protocol.rs b/src/protocol.rs index 90be836..a4e5f40 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -22,7 +22,11 @@ pub trait Diagnostic: std::error::Error { /// Diagnostic severity. This may be used by [DiagnosticReporter]s to change the /// display format of this diagnostic. - fn severity(&self) -> Severity; + /// + /// If `None`, reporters should treat this as [Severity::Error] + fn severity(&self) -> Option { + None + } /// Additional help text related to this Diagnostic. Do you have any /// advice for the poor soul who's just run into this issue? diff --git a/src/reporter.rs b/src/reporter.rs index 5dd1c6b..1224bc5 100644 --- a/src/reporter.rs +++ b/src/reporter.rs @@ -103,9 +103,9 @@ impl DiagnosticReporter for MietteReporter { } let sev = match diagnostic.severity() { - Severity::Error => "Error", - Severity::Warning => "Warning", - Severity::Advice => "Advice", + Some(Severity::Error) | None => "Error", + Some(Severity::Warning) => "Warning", + Some(Severity::Advice) => "Advice", }; write!(f, "{}[{}]: {}", sev, diagnostic.code(), diagnostic)?; @@ -151,9 +151,9 @@ impl DiagnosticReporter for JokeReporter { } let sev = match diagnostic.severity() { - Severity::Error => "error", - Severity::Warning => "warning", - Severity::Advice => "advice", + Some(Severity::Error) | None => "error", + Some(Severity::Warning) => "warning", + Some(Severity::Advice) => "advice", }; writeln!( f, diff --git a/tests/reporter.rs b/tests/reporter.rs index ccacd28..2b36dab 100644 --- a/tests/reporter.rs +++ b/tests/reporter.rs @@ -1,7 +1,8 @@ -use std::fmt; +use std::{fmt, sync::Arc}; use miette::{ - Diagnostic, DiagnosticSnippet, DiagnosticReporter, MietteError, MietteReporter, Severity, SourceSpan, + Diagnostic, DiagnosticReporter, DiagnosticSnippet, MietteError, MietteReporter, + SourceSpan, }; use thiserror::Error; @@ -22,8 +23,6 @@ impl Diagnostic for MyBad { &"oops::my::bad" } - fn severity(&self) -> Severity { - Severity::Error } fn help(&self) -> Option>> {