mirror of https://github.com/zkat/miette.git
Add severity field
This commit is contained in:
parent
01b34a4f75
commit
61d43f8b17
|
|
@ -3,7 +3,7 @@ use std::{
|
|||
fmt::{Debug, Display},
|
||||
};
|
||||
|
||||
use crate::Diagnostic;
|
||||
use crate::{Diagnostic, Severity};
|
||||
|
||||
/// Diagnostic that can be created at runtime.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -16,6 +16,10 @@ pub struct MietteDiagnostic {
|
|||
/// Rust path format (`foo::bar::baz`) is recommended, but more classic
|
||||
/// codes like `E0123` will work just fine.
|
||||
pub code: Option<String>,
|
||||
/// [`Diagnostic`] severity. Intended to be used by
|
||||
/// [`ReportHandler`](crate::ReportHandler)s to change the way different
|
||||
/// [`Diagnostic`]s are displayed. Defaults to [`Severity::Error`].
|
||||
pub severity: Severity,
|
||||
}
|
||||
|
||||
impl Display for MietteDiagnostic {
|
||||
|
|
@ -33,6 +37,10 @@ impl Diagnostic for MietteDiagnostic {
|
|||
.map(Box::new)
|
||||
.map(|c| c as Box<dyn Display>)
|
||||
}
|
||||
|
||||
fn severity(&self) -> Option<Severity> {
|
||||
Some(self.severity)
|
||||
}
|
||||
}
|
||||
|
||||
impl MietteDiagnostic {
|
||||
|
|
@ -40,16 +48,18 @@ impl MietteDiagnostic {
|
|||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use miette::{Diagnostic, MietteDiagnostic};
|
||||
/// use miette::{Diagnostic, MietteDiagnostic, Severity};
|
||||
///
|
||||
/// let diag = MietteDiagnostic::new("Oops, something went wrong!");
|
||||
/// assert_eq!(diag.to_string(), "Oops, something went wrong!");
|
||||
/// assert_eq!(diag.description, "Oops, something went wrong!");
|
||||
/// assert_eq!(diag.severity, Severity::Error);
|
||||
/// ```
|
||||
pub fn new(description: impl Into<String>) -> Self {
|
||||
Self {
|
||||
description: description.into(),
|
||||
code: None,
|
||||
severity: Severity::Error,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -69,4 +79,18 @@ impl MietteDiagnostic {
|
|||
..self
|
||||
}
|
||||
}
|
||||
|
||||
/// Return new diagnostic with the given severity.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use miette::{Diagnostic, MietteDiagnostic, Severity};
|
||||
///
|
||||
/// let diag = MietteDiagnostic::new("I warn you to stop!").with_severity(Severity::Warning);
|
||||
/// assert_eq!(diag.description, "I warn you to stop!");
|
||||
/// assert_eq!(diag.severity, Severity::Warning);
|
||||
/// ```
|
||||
pub fn with_severity(self, severity: Severity) -> Self {
|
||||
Self { severity, ..self }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue