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
use flavored::RpName;
use std::fmt;
use std::slice;
use {Source, Span};
#[derive(Debug, Clone, Copy, Serialize)]
pub enum SymbolKind {
#[serde(rename = "type")]
Type,
#[serde(rename = "interface")]
Interface,
#[serde(rename = "tuple")]
Tuple,
#[serde(rename = "enum")]
Enum,
#[serde(rename = "service")]
Service,
}
#[derive(Debug, Clone)]
pub enum Diagnostic {
Error(Span, String),
Info(Span, String),
Symbol {
kind: SymbolKind,
span: Span,
name: RpName,
},
}
#[derive(Debug, Clone)]
pub struct Diagnostics {
pub source: Source,
pub items: Vec<Diagnostic>,
}
impl Diagnostics {
pub fn new(source: Source) -> Self {
Self {
source,
items: Vec::new(),
}
}
pub fn has_errors(&self) -> bool {
self.items.iter().any(|item| match *item {
Diagnostic::Error(_, _) => true,
_ => false,
})
}
pub fn err<S: Into<Span>, E: fmt::Display>(&mut self, span: S, error: E) {
self.items
.push(Diagnostic::Error(span.into(), error.to_string()));
}
pub fn info<S: Into<Span>, I: fmt::Display>(&mut self, span: S, info: I) {
self.items
.push(Diagnostic::Info(span.into(), info.to_string()));
}
pub fn symbol<P: Into<Span>>(&mut self, kind: SymbolKind, span: P, name: &RpName) {
self.items.push(Diagnostic::Symbol {
kind,
span: span.into(),
name: name.clone(),
});
}
pub fn items(&self) -> Items {
Items {
iter: self.items.iter(),
}
}
pub fn clear(&mut self) {
self.items.clear();
}
}
pub struct Items<'a> {
iter: slice::Iter<'a, Diagnostic>,
}
impl<'a> Iterator for Items<'a> {
type Item = <slice::Iter<'a, Diagnostic> as Iterator>::Item;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}