Add url field

This commit is contained in:
Gavrilikhin Daniil 2023-05-06 09:49:36 +08:00
parent 53b21ead39
commit a1f602c0fe
1 changed files with 32 additions and 0 deletions

View File

@ -22,6 +22,9 @@ pub struct MietteDiagnostic {
pub severity: Severity,
/// Additional help text related to this Diagnostic
pub help: Option<String>,
/// URL to visit for a more detailed explanation/help about this
/// [`Diagnostic`].
pub url: Option<String>,
}
impl Display for MietteDiagnostic {
@ -50,6 +53,13 @@ impl Diagnostic for MietteDiagnostic {
.map(Box::new)
.map(|c| c as Box<dyn Display>)
}
fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.url
.as_ref()
.map(Box::new)
.map(|c| c as Box<dyn Display>)
}
}
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<String>) -> Self {
Self {
url: Some(url.into()),
..self
}
}
}