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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use codespan_reporting::diagnostic::{Diagnostic as CodespanDiag, Label, LabelStyle, Severity};
use calypso_base::span::Span;
use super::{Diagnostic, EnsembleDiagnostic};
#[derive(Debug, Default)]
#[allow(clippy::module_name_repetitions)]
pub struct EnsembleBuilder {
diags: Vec<Builder>,
}
impl EnsembleBuilder {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn add(mut self, severity: Severity, f: impl FnOnce(Builder) -> Builder) -> Self {
self.diags.push(f(Builder::new(severity)));
self
}
pub fn bug(self, f: impl FnOnce(Builder) -> Builder) -> Self {
self.add(Severity::Bug, f)
}
pub fn error(self, f: impl FnOnce(Builder) -> Builder) -> Self {
self.add(Severity::Error, f)
}
pub fn warning(self, f: impl FnOnce(Builder) -> Builder) -> Self {
self.add(Severity::Warning, f)
}
pub fn note(self, f: impl FnOnce(Builder) -> Builder) -> Self {
self.add(Severity::Note, f)
}
pub fn help(self, f: impl FnOnce(Builder) -> Builder) -> Self {
self.add(Severity::Help, f)
}
#[allow(clippy::missing_panics_doc)]
#[must_use]
pub fn build(self) -> EnsembleDiagnostic {
let diags = self
.diags
.into_iter()
.map(Builder::build)
.collect::<Vec<_>>();
if diags.len() == 1 {
diags.into_iter().next().unwrap().into()
} else {
diags.into()
}
}
}
#[derive(Debug)]
pub struct Builder {
diag: CodespanDiag<usize>,
labels: Vec<Label<usize>>,
notes: Vec<String>,
}
impl Builder {
#[must_use]
pub fn new(severity: Severity) -> Self {
Self {
diag: CodespanDiag::new(severity),
labels: Vec::new(),
notes: Vec::new(),
}
}
#[must_use]
pub fn code(mut self, code: &'static str) -> Self {
self.diag = self.diag.with_code(code);
self
}
#[must_use]
pub fn short(mut self, short: impl Into<String>) -> Self {
self.diag = self.diag.with_message(short.into());
self
}
#[must_use]
pub fn note(mut self, note: impl Into<String>) -> Self {
self.notes.push(note.into());
self
}
#[must_use]
pub fn label(
mut self,
style: LabelStyle,
message: Option<&str>,
file_id: usize,
span: Span,
) -> Self {
let mut label = Label::new(style, file_id, span);
if let Some(message) = message {
label = label.with_message(message);
}
self.labels.push(label);
self
}
#[must_use]
pub fn build(self) -> Diagnostic {
Diagnostic(self.diag.with_labels(self.labels).with_notes(self.notes))
}
}