Add labels field

This commit is contained in:
Gavrilikhin Daniil 2023-05-06 10:11:40 +08:00
parent a1f602c0fe
commit c6e977bbb8
1 changed files with 48 additions and 1 deletions

View File

@ -3,7 +3,7 @@ use std::{
fmt::{Debug, Display}, fmt::{Debug, Display},
}; };
use crate::{Diagnostic, Severity}; use crate::{Diagnostic, LabeledSpan, Severity};
/// Diagnostic that can be created at runtime. /// Diagnostic that can be created at runtime.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -25,6 +25,8 @@ pub struct MietteDiagnostic {
/// URL to visit for a more detailed explanation/help about this /// URL to visit for a more detailed explanation/help about this
/// [`Diagnostic`]. /// [`Diagnostic`].
pub url: Option<String>, pub url: Option<String>,
/// Labels to apply to this `Diagnostic`'s [`Diagnostic::source_code`]
pub labels: Vec<LabeledSpan>,
} }
impl Display for MietteDiagnostic { impl Display for MietteDiagnostic {
@ -60,6 +62,10 @@ impl Diagnostic for MietteDiagnostic {
.map(Box::new) .map(Box::new)
.map(|c| c as Box<dyn Display>) .map(|c| c as Box<dyn Display>)
} }
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
Some(Box::new(self.labels.iter().cloned()))
}
} }
impl MietteDiagnostic { impl MietteDiagnostic {
@ -78,6 +84,7 @@ impl MietteDiagnostic {
Self { Self {
description: description.into(), description: description.into(),
severity: Severity::Error, severity: Severity::Error,
labels: Vec::new(),
code: None, code: None,
help: None, help: None,
url: None, url: None,
@ -152,4 +159,44 @@ impl MietteDiagnostic {
..self ..self
} }
} }
/// Return new diagnostic with the given label
///
/// # Examples
/// ```
/// use miette::{Diagnostic, LabeledSpan, MietteDiagnostic};
///
/// let source = "cpp is the best language";
///
/// let label = LabeledSpan::new(Some("This should be Rust".to_string()), 0, 3);
/// let diag = MietteDiagnostic::new("Wrong best language").with_label(label.clone());
/// assert_eq!(diag.description, "Wrong best language");
/// assert_eq!(diag.labels, vec![label]);
/// ```
pub fn with_label(self, label: impl Into<LabeledSpan>) -> Self {
Self {
labels: vec![label.into()],
..self
}
}
/// Return new diagnostic with the given labels
///
/// # Examples
/// ```
/// use miette::{Diagnostic, LabeledSpan, MietteDiagnostic};
///
/// let source = "helo word";
///
/// let labels = vec![
/// LabeledSpan::new(Some("add 'l'".to_string()), 3, 0),
/// LabeledSpan::new(Some("add 'l'".to_string()), 8, 0),
/// ];
/// let diag = MietteDiagnostic::new("Typos in 'hello world'").with_labels(labels.clone());
/// assert_eq!(diag.description, "Typos in 'hello world'");
/// assert_eq!(diag.labels, labels);
/// ```
pub fn with_labels(self, labels: Vec<LabeledSpan>) -> Self {
Self { labels, ..self }
}
} }