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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::{Backtraced, INDENT};
use leo_span::{source_map::SpanLocation, symbol::with_session_globals, Span};
use backtrace::Backtrace;
use color_backtrace::{BacktracePrinter, Verbosity};
use colored::Colorize;
use std::fmt;
#[derive(Clone, Debug, Default, Hash, PartialEq)]
pub struct Formatted {
pub span: Span,
pub backtrace: Backtraced,
}
impl Formatted {
#[allow(clippy::too_many_arguments)]
pub fn new_from_span<S>(
message: S,
help: Option<String>,
code: i32,
code_identifier: i8,
type_: String,
error: bool,
span: Span,
backtrace: Backtrace,
) -> Self
where
S: ToString,
{
Self {
span,
backtrace: Backtraced::new_from_backtrace(
message.to_string(),
help,
code,
code_identifier,
type_,
error,
backtrace,
),
}
}
pub fn exit_code(&self) -> i32 {
self.backtrace.exit_code()
}
pub fn error_code(&self) -> String {
self.backtrace.error_code()
}
pub fn warning_code(&self) -> String {
self.backtrace.warning_code()
}
}
impl fmt::Display for Formatted {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let underline = |mut start: usize, mut end: usize| -> String {
if start > end {
std::mem::swap(&mut start, &mut end)
}
let mut underline = String::new();
for _ in 0..start {
underline.push(' ');
end -= 1;
}
for _ in 0..end {
underline.push('^');
}
underline
};
let (loc, contents) = with_session_globals(|s| {
(
s.source_map
.span_to_location(self.span)
.unwrap_or_else(SpanLocation::dummy),
s.source_map
.line_contents_of_span(self.span)
.unwrap_or_else(|| "<contents unavailable>".to_owned()),
)
});
let underlined = underline(loc.col_start, loc.col_stop);
let (kind, code) = if self.backtrace.error {
("Error", self.error_code())
} else {
("Warning", self.warning_code())
};
let message = format!("{kind} [{code}]: {message}", message = self.backtrace.message,);
if std::env::var("LEO_TESTFRAMEWORK")
.unwrap_or_default()
.trim()
.to_owned()
.is_empty()
{
if self.backtrace.error {
write!(f, "{}", message.bold().red())?;
} else {
write!(f, "{}", message.bold().yellow())?;
}
} else {
write!(f, "{message}")?;
};
write!(
f,
"\n{indent }--> {path}:{line_start}:{start}\n\
{indent } |\n",
indent = INDENT,
path = &loc.source_file.name,
line_start = loc.line_start,
start = loc.col_start,
)?;
for (line_no, line) in contents.lines().enumerate() {
writeln!(
f,
"{line_no:width$} | {text}",
width = INDENT.len(),
line_no = loc.line_start + line_no,
text = line,
)?;
}
write!(f, "{INDENT } |{underlined}",)?;
if let Some(help) = &self.backtrace.help {
write!(
f,
"\n{INDENT } |\n\
{INDENT } = {help}",
)?;
}
let leo_backtrace = std::env::var("LEO_BACKTRACE").unwrap_or_default().trim().to_owned();
match leo_backtrace.as_ref() {
"1" => {
let mut printer = BacktracePrinter::default();
printer = printer.verbosity(Verbosity::Medium);
printer = printer.lib_verbosity(Verbosity::Medium);
let trace = printer
.format_trace_to_string(&self.backtrace.backtrace)
.map_err(|_| fmt::Error)?;
write!(f, "\n{trace}")?;
}
"full" => {
let mut printer = BacktracePrinter::default();
printer = printer.verbosity(Verbosity::Full);
printer = printer.lib_verbosity(Verbosity::Full);
let trace = printer
.format_trace_to_string(&self.backtrace.backtrace)
.map_err(|_| fmt::Error)?;
write!(f, "\n{trace}")?;
}
_ => {}
}
Ok(())
}
}
impl std::error::Error for Formatted {
fn description(&self) -> &str {
&self.backtrace.message
}
}