1use std::fmt;
2
3use xur_span::Span;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Level {
7 Error,
8 Warning,
9}
10
11impl fmt::Display for Level {
12 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13 f.write_str(match self {
14 Self::Error => "error",
15 Self::Warning => "warning",
16 })
17 }
18}
19
20#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct Label {
23 pub span: Span,
24 pub message: String,
25}
26
27#[derive(Debug, Clone, PartialEq, Eq)]
30#[must_use = "a diagnostic does nothing until it is reported"]
31pub struct Diag {
32 pub level: Level,
33 pub message: String,
34 pub span: Span,
35 pub labels: Vec<Label>,
36 pub notes: Vec<String>,
37}
38
39impl Diag {
40 pub fn new(level: Level, message: impl Into<String>, span: Span) -> Self {
41 Self {
42 level,
43 message: message.into(),
44 span,
45 labels: Vec::new(),
46 notes: Vec::new(),
47 }
48 }
49
50 pub fn error(message: impl Into<String>, span: Span) -> Self {
51 Self::new(Level::Error, message, span)
52 }
53
54 pub fn with_label(mut self, span: Span, message: impl Into<String>) -> Self {
57 self.labels.push(Label {
58 span,
59 message: message.into(),
60 });
61 self
62 }
63
64 pub fn with_note(mut self, note: impl Into<String>) -> Self {
65 self.notes.push(note.into());
66 self
67 }
68}
69
70impl fmt::Display for Diag {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 write!(f, "{}: {}", self.level, self.message)
73 }
74}