Skip to content

Commit 99b0305

Browse files
committed
Use ariadne
#1323
1 parent 5e3dcff commit 99b0305

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/compile_error.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ impl<'src> CompileError<'src> {
1919
}
2020
}
2121

22+
pub(crate) fn render_compile_error(error: &CompileError) {
23+
use ariadne::{Color, Label, Report, ReportKind, Source};
24+
25+
let token = error.token;
26+
let source = Source::from(token.src);
27+
28+
let start = token.offset;
29+
let end = token.offset + token.length;
30+
31+
let path = token.path.display().to_string();
32+
let label = Label::new((&path, start..end));
33+
34+
let message = error.to_string();
35+
36+
let report = Report::build(ReportKind::Custom("error", Color::Red), &path, start)
37+
.with_message(message)
38+
.with_label(label)
39+
.finish();
40+
41+
report.eprint((&path, source)).unwrap();
42+
}
43+
2244
fn capitalize(s: &str) -> String {
2345
let mut chars = s.chars();
2446
match chars.next() {

src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,13 @@ impl<'src> ColorDisplay for Error<'src> {
504504
}
505505
}
506506

507+
pub(crate) fn render_error(error: &Error, color: Color) {
508+
match error {
509+
Error::Compile { compile_error } => compile_error::render_compile_error(compile_error),
510+
_ => eprintln!("{}", error.color_display(color.stderr())),
511+
}
512+
}
513+
507514
fn format_cmd(binary: &OsString, arguments: &Vec<OsString>) -> String {
508515
iter::once(binary)
509516
.chain(arguments)

src/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn run(args: impl Iterator<Item = impl Into<OsString> + Clone>) -> Result<()
2929
})
3030
.map_err(|error| {
3131
if !verbosity.quiet() && error.print_message() {
32-
eprintln!("{}", error.color_display(color.stderr()));
32+
crate::error::render_error(&error, color);
3333
}
3434
error.code().unwrap_or(EXIT_FAILURE)
3535
})

0 commit comments

Comments
 (0)