Skip to main content

nifty_syntax/
diagnostic.rs

1use crate::span::Span;
2
3/// An error in the source, pointing at the text that caused it.
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct Diagnostic {
6    pub message: String,
7    pub span: Span,
8    /// Other places the error involves, such as the first of two declarations.
9    pub notes: Vec<Note>,
10}
11
12/// A secondary place a [`Diagnostic`] points at, with what it is.
13#[derive(Clone, Debug, PartialEq, Eq)]
14pub struct Note {
15    pub message: String,
16    pub span: Span,
17}
18
19impl Diagnostic {
20    pub fn new(message: impl Into<String>, span: Span) -> Self {
21        Self {
22            message: message.into(),
23            span,
24            notes: Vec::new(),
25        }
26    }
27
28    #[must_use]
29    pub fn with_note(mut self, message: impl Into<String>, span: Span) -> Self {
30        self.notes.push(Note {
31            message: message.into(),
32            span,
33        });
34        self
35    }
36}