use std::fmt;
use xur_span::Span;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Level {
Error,
Warning,
}
impl fmt::Display for Level {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Error => "error",
Self::Warning => "warning",
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Label {
pub span: Span,
pub message: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use = "a diagnostic does nothing until it is reported"]
pub struct Diag {
pub level: Level,
pub message: String,
pub span: Span,
pub labels: Vec<Label>,
pub notes: Vec<String>,
}
impl Diag {
pub fn new(level: Level, message: impl Into<String>, span: Span) -> Self {
Self {
level,
message: message.into(),
span,
labels: Vec::new(),
notes: Vec::new(),
}
}
pub fn error(message: impl Into<String>, span: Span) -> Self {
Self::new(Level::Error, message, span)
}
pub fn with_label(mut self, span: Span, message: impl Into<String>) -> Self {
self.labels.push(Label {
span,
message: message.into(),
});
self
}
pub fn with_note(mut self, note: impl Into<String>) -> Self {
self.notes.push(note.into());
self
}
}
impl fmt::Display for Diag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.level, self.message)
}
}