1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use codespan::ByteSpan;
use Severity;
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub enum LabelStyle {
Primary,
Secondary,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct Label {
pub span: ByteSpan,
pub message: Option<String>,
pub style: LabelStyle,
}
impl Label {
pub fn new(span: ByteSpan, style: LabelStyle) -> Label {
Label {
span,
message: None,
style,
}
}
pub fn new_primary(span: ByteSpan) -> Label {
Label::new(span, LabelStyle::Primary)
}
pub fn new_secondary(span: ByteSpan) -> Label {
Label::new(span, LabelStyle::Secondary)
}
pub fn with_message<S: Into<String>>(mut self, message: S) -> Label {
self.message = Some(message.into());
self
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct Diagnostic {
pub severity: Severity,
pub code: Option<String>,
pub message: String,
pub labels: Vec<Label>,
}
impl Diagnostic {
pub fn new<S: Into<String>>(severity: Severity, message: S) -> Diagnostic {
Diagnostic {
severity,
code: None,
message: message.into(),
labels: Vec::new(),
}
}
pub fn new_bug<S: Into<String>>(message: S) -> Diagnostic {
Diagnostic::new(Severity::Bug, message)
}
pub fn new_error<S: Into<String>>(message: S) -> Diagnostic {
Diagnostic::new(Severity::Error, message)
}
pub fn new_warning<S: Into<String>>(message: S) -> Diagnostic {
Diagnostic::new(Severity::Warning, message)
}
pub fn new_note<S: Into<String>>(message: S) -> Diagnostic {
Diagnostic::new(Severity::Note, message)
}
pub fn new_help<S: Into<String>>(message: S) -> Diagnostic {
Diagnostic::new(Severity::Help, message)
}
pub fn with_code<S: Into<String>>(mut self, code: S) -> Diagnostic {
self.code = Some(code.into());
self
}
pub fn with_label(mut self, label: Label) -> Diagnostic {
self.labels.push(label);
self
}
pub fn with_labels<Labels: IntoIterator<Item = Label>>(mut self, labels: Labels) -> Diagnostic {
self.labels.extend(labels);
self
}
}