Have Diagnostic implement std::error::Error
*/
use crate::Location;
-use std::fmt::{Debug, Display, Write};
+use std::error::Error;
+use std::fmt;
+use std::fmt::Write;
pub type DiagnosticsResult<T> = Result<T, Vec<Diagnostic>>;
@@ -26,7 +28,7 @@ pub fn diagnostics_result<T>(result: T, diagnostics: Vec<Diagnostic>) -> Diagnos
/// - `location` is different from LSP in that it's a file + span instead of
/// just a span.
/// - Unused fields are omitted.
-#[derive(Debug)]
+#[derive(fmt::Debug)]
pub struct Diagnostic(Box<DiagnosticData>);
impl Diagnostic {
@@ -92,6 +94,14 @@ impl Diagnostic {
}
}
+impl fmt::Display for Diagnostic {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}", self.print_without_source())
+ }
+}
+
+impl Error for Diagnostic {}
+
// statically verify that the Diagnostic type is thread safe
fn _assert_diagnostic_constraints()
where
@@ -99,7 +109,7 @@ where
{
}
-#[derive(Debug)]
+#[derive(fmt::Debug)]
struct DiagnosticData {
/// Human readable error message.
message: Box<dyn DiagnosticDisplay>,
@@ -113,7 +123,7 @@ struct DiagnosticData {
}
/// Secondary locations attached to a diagnostic.
-#[derive(Debug)]
+#[derive(fmt::Debug)]
pub struct DiagnosticRelatedInformation {
/// The message of this related diagnostic information.
pub message: Box<dyn DiagnosticDisplay>,
@@ -124,11 +134,11 @@ pub struct DiagnosticRelatedInformation {
/// Trait for diagnostic messages to allow structs that capture
/// some data and can lazily convert it to a message.
-pub trait DiagnosticDisplay: Debug + Display + Send + Sync {}
+pub trait DiagnosticDisplay: fmt::Debug + fmt::Display + Send + Sync {}
/// Automatically implement the trait if constraints are met, so that
/// implementors don't need to.
-impl<T> DiagnosticDisplay for T where T: Debug + Display + Send + Sync {}
+impl<T> DiagnosticDisplay for T where T: fmt::Debug + fmt::Display + Send + Sync {}
impl From<Diagnostic> for Vec<Diagnostic> {
fn from(diagnostic: Diagnostic) -> Self {
