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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//! A generic "parse error" with very fancy formatting.
use codespan_reporting::{
diagnostic::{Diagnostic, Label},
files::SimpleFiles,
term,
};
use std::{error::Error as StdError, fmt, io::Cursor, ops::Range, sync::Arc};
use termcolor::NoColor;
/// An error occurred processing the schema.
#[derive(Debug)]
pub(crate) struct ParseError {
/// The source file in which the error occurred.
file_info: Arc<FileInfo>,
/// The location of the error.
pub(crate) annotations: Vec<Annotation>,
/// The error message to display.
pub(crate) message: String,
}
impl ParseError {
/// Construct a parse error from an input file.
pub(crate) fn new<M: Into<String>>(
file_info: Arc<FileInfo>,
annotations: Vec<Annotation>,
message: M,
) -> ParseError {
ParseError {
file_info,
annotations,
message: message.into(),
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Build a set of source files.
let mut files = SimpleFiles::new();
let file_id = files.add(&self.file_info.name, &self.file_info.contents);
// Build our diagnostic.
let diagnostic = Diagnostic::error().with_message(&self.message).with_labels(
self.annotations
.iter()
.map(|a| match a.ty {
AnnotationType::Primary => {
Label::primary(file_id, &a.location).with_message(&a.message)
}
AnnotationType::Secondary => {
Label::secondary(file_id, &a.location).with_message(&a.message)
}
})
.collect(),
);
// Normally, we would write this directly to standard error with some
// pretty colors, but we can't do that inside `Display`, because we
// don't know if we're displaying to the terminal or not. So write
// everything to a local buffer.
let mut buf = Vec::with_capacity(1024);
let mut wtr = NoColor::new(Cursor::new(&mut buf));
let config = codespan_reporting::term::Config::default();
term::emit(&mut wtr, &config, &files, &diagnostic).map_err(|_| fmt::Error)?;
write!(f, "{}", String::from_utf8_lossy(&buf))
}
}
impl StdError for ParseError {}
/// Information about a file we attempted to parse.
#[derive(Debug)]
pub(crate) struct FileInfo {
/// The name of the file.
pub(crate) name: String,
/// The data of the file.
pub(crate) contents: String,
}
impl FileInfo {
/// Create a new `FileInfo`.
pub(crate) fn new(name: String, contents: String) -> Self {
Self { name, contents }
}
}
/// An annotation pointing at a particular part of our input.
#[derive(Debug)]
pub(crate) struct Annotation {
/// What type of annotation is this?
pub(crate) ty: AnnotationType,
/// What location are we annotating?
pub(crate) location: Location,
/// The message to display for this annotation.
pub(crate) message: String,
}
impl Annotation {
/// Create a primary annotation which shows the main location of the error.
pub(crate) fn primary<L, M>(location: L, message: M) -> Self
where
L: Into<Location>,
M: Into<String>,
{
Annotation {
ty: AnnotationType::Primary,
location: location.into(),
message: message.into(),
}
}
/// Create a secondary annotation that shows another location related to the error.
pub(crate) fn secondary<L, M>(location: L, message: M) -> Self
where
L: Into<Location>,
M: Into<String>,
{
Annotation {
ty: AnnotationType::Secondary,
location: location.into(),
message: message.into(),
}
}
}
/// What type of annotation are we displaying?
#[derive(Debug)]
pub(crate) enum AnnotationType {
/// This the main source location associated with the error.
Primary,
/// This is a secondary source location associated with the error.
Secondary,
}
/// The location where an error occurred.
#[derive(Debug)]
pub(crate) enum Location {
/// This error occurred as a specific place in the source code.
Position(usize),
/// This error occurred at a span in the source code.
Range(Range<usize>),
}
impl From<usize> for Location {
fn from(pos: usize) -> Self {
Location::Position(pos)
}
}
impl From<Range<usize>> for Location {
fn from(range: Range<usize>) -> Self {
Location::Range(range)
}
}
impl<'a> From<&'a Location> for Range<usize> {
fn from(input: &'a Location) -> Self {
match input {
Location::Position(p) => *p..(*p + 1),
Location::Range(r) => r.to_owned(),
}
}
}