xur-errors 0.1.2

Diagnostics and their rendering for the Xur language
Documentation
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",
        })
    }
}

/// A message attached to a region of source code.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Label {
    pub span: Span,
    pub message: String,
}

/// A single diagnostic: a headline message, the primary span it is about,
/// optional labelled spans and free-form notes.
#[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)
    }

    /// Attaches `message` to `span`. A label on the primary span is
    /// highlighted as the main point of the diagnostic.
    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)
    }
}