Use `Option<Severity>`

This commit is contained in:
Gavrilikhin Daniil 2023-05-06 12:59:10 +08:00
parent 021eb01feb
commit b9a892f39b
1 changed files with 8 additions and 6 deletions

View File

@ -19,7 +19,7 @@ pub struct MietteDiagnostic {
/// [`Diagnostic`] severity. Intended to be used by /// [`Diagnostic`] severity. Intended to be used by
/// [`ReportHandler`](crate::ReportHandler)s to change the way different /// [`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, pub severity: Option<Severity>,
/// Additional help text related to this Diagnostic /// Additional help text related to this Diagnostic
pub help: Option<String>, pub help: Option<String>,
/// URL to visit for a more detailed explanation/help about this /// URL to visit for a more detailed explanation/help about this
@ -46,7 +46,7 @@ impl Diagnostic for MietteDiagnostic {
} }
fn severity(&self) -> Option<Severity> { fn severity(&self) -> Option<Severity> {
Some(self.severity) self.severity
} }
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> { fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
@ -78,13 +78,12 @@ impl MietteDiagnostic {
/// let diag = MietteDiagnostic::new("Oops, something went wrong!"); /// let diag = MietteDiagnostic::new("Oops, something went wrong!");
/// assert_eq!(diag.to_string(), "Oops, something went wrong!"); /// assert_eq!(diag.to_string(), "Oops, something went wrong!");
/// assert_eq!(diag.description, "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 { pub fn new(description: impl Into<String>) -> Self {
Self { Self {
description: description.into(), description: description.into(),
severity: Severity::Error,
labels: Vec::new(), labels: Vec::new(),
severity: None,
code: None, code: None,
help: None, help: None,
url: None, url: None,
@ -116,10 +115,13 @@ impl MietteDiagnostic {
/// ///
/// let diag = MietteDiagnostic::new("I warn you to stop!").with_severity(Severity::Warning); /// 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.description, "I warn you to stop!");
/// assert_eq!(diag.severity, Severity::Warning); /// assert_eq!(diag.severity, Some(Severity::Warning));
/// ``` /// ```
pub fn with_severity(self, severity: Severity) -> Self { pub fn with_severity(self, severity: Severity) -> Self {
Self { severity, ..self } Self {
severity: Some(severity),
..self
}
} }
/// Return new diagnostic with the given help message. /// Return new diagnostic with the given help message.