Skip to content

Commit

Permalink
Use ariadne
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak committed Oct 19, 2024
1 parent 5e3dcff commit 99b0305
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ impl<'src> CompileError<'src> {
}
}

pub(crate) fn render_compile_error(error: &CompileError) {
use ariadne::{Color, Label, Report, ReportKind, Source};

let token = error.token;
let source = Source::from(token.src);

let start = token.offset;
let end = token.offset + token.length;

let path = token.path.display().to_string();
let label = Label::new((&path, start..end));

let message = error.to_string();

let report = Report::build(ReportKind::Custom("error", Color::Red), &path, start)
.with_message(message)
.with_label(label)
.finish();

report.eprint((&path, source)).unwrap();
}

fn capitalize(s: &str) -> String {
let mut chars = s.chars();
match chars.next() {
Expand Down
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ impl<'src> ColorDisplay for Error<'src> {
}
}

pub(crate) fn render_error(error: &Error, color: Color) {
match error {
Error::Compile { compile_error } => compile_error::render_compile_error(compile_error),
_ => eprintln!("{}", error.color_display(color.stderr())),
}
}

fn format_cmd(binary: &OsString, arguments: &Vec<OsString>) -> String {
iter::once(binary)
.chain(arguments)
Expand Down
2 changes: 1 addition & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn run(args: impl Iterator<Item = impl Into<OsString> + Clone>) -> Result<()
})
.map_err(|error| {
if !verbosity.quiet() && error.print_message() {
eprintln!("{}", error.color_display(color.stderr()));
crate::error::render_error(&error, color);
}
error.code().unwrap_or(EXIT_FAILURE)
})
Expand Down

0 comments on commit 99b0305

Please sign in to comment.