diff --git a/src/miette_diagnostic.rs b/src/miette_diagnostic.rs index bd8072d..a21f1ce 100644 --- a/src/miette_diagnostic.rs +++ b/src/miette_diagnostic.rs @@ -22,6 +22,9 @@ pub struct MietteDiagnostic { pub severity: Severity, /// Additional help text related to this Diagnostic pub help: Option, + /// URL to visit for a more detailed explanation/help about this + /// [`Diagnostic`]. + pub url: Option, } impl Display for MietteDiagnostic { @@ -50,6 +53,13 @@ impl Diagnostic for MietteDiagnostic { .map(Box::new) .map(|c| c as Box) } + + fn url<'a>(&'a self) -> Option> { + self.url + .as_ref() + .map(Box::new) + .map(|c| c as Box) + } } impl MietteDiagnostic { @@ -70,6 +80,7 @@ impl MietteDiagnostic { severity: Severity::Error, code: None, help: None, + url: None, } } @@ -120,4 +131,25 @@ impl MietteDiagnostic { ..self } } + + /// Return new diagnostic with the given URL. + /// + /// # Examples + /// ``` + /// use miette::{Diagnostic, MietteDiagnostic}; + /// + /// let diag = MietteDiagnostic::new("PC is not working") + /// .with_url("https://letmegooglethat.com/?q=Why+my+pc+doesn%27t+work"); + /// assert_eq!(diag.description, "PC is not working"); + /// assert_eq!( + /// diag.url, + /// Some("https://letmegooglethat.com/?q=Why+my+pc+doesn%27t+work".to_string()) + /// ); + /// ``` + pub fn with_url(self, url: impl Into) -> Self { + Self { + url: Some(url.into()), + ..self + } + } }