From 53b21ead3976704d5138f9f8a3adc553204b9b90 Mon Sep 17 00:00:00 2001 From: Gavrilikhin Daniil Date: Sat, 6 May 2023 09:43:46 +0800 Subject: [PATCH] Add help field --- src/miette_diagnostic.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/miette_diagnostic.rs b/src/miette_diagnostic.rs index 6addfb0..bd8072d 100644 --- a/src/miette_diagnostic.rs +++ b/src/miette_diagnostic.rs @@ -14,12 +14,14 @@ pub struct MietteDiagnostic { /// about this Diagnostic. Ideally also globally unique, and documented /// in the toplevel crate's documentation for easy searching. /// Rust path format (`foo::bar::baz`) is recommended, but more classic - /// codes like `E0123` will work just fine. + /// codes like `E0123` will work just fine pub code: Option, /// [`Diagnostic`] severity. Intended to be used by /// [`ReportHandler`](crate::ReportHandler)s to change the way different - /// [`Diagnostic`]s are displayed. Defaults to [`Severity::Error`]. + /// [`Diagnostic`]s are displayed. Defaults to [`Severity::Error`] pub severity: Severity, + /// Additional help text related to this Diagnostic + pub help: Option, } impl Display for MietteDiagnostic { @@ -41,6 +43,13 @@ impl Diagnostic for MietteDiagnostic { fn severity(&self) -> Option { Some(self.severity) } + + fn help<'a>(&'a self) -> Option> { + self.help + .as_ref() + .map(Box::new) + .map(|c| c as Box) + } } impl MietteDiagnostic { @@ -58,8 +67,9 @@ impl MietteDiagnostic { pub fn new(description: impl Into) -> Self { Self { description: description.into(), - code: None, severity: Severity::Error, + code: None, + help: None, } } @@ -93,4 +103,21 @@ impl MietteDiagnostic { pub fn with_severity(self, severity: Severity) -> Self { Self { severity, ..self } } + + /// Return new diagnostic with the given help message. + /// + /// # Examples + /// ``` + /// use miette::{Diagnostic, MietteDiagnostic}; + /// + /// let diag = MietteDiagnostic::new("PC is not working").with_help("Try to reboot it again"); + /// assert_eq!(diag.description, "PC is not working"); + /// assert_eq!(diag.help, Some("Try to reboot it again".to_string())); + /// ``` + pub fn with_help(self, help: impl Into) -> Self { + Self { + help: Some(help.into()), + ..self + } + } }