use std::path::Path;
use ariadne::Source;
use bynk_syntax::error::Severity;
use bynk_syntax::{CompileError, span};
pub fn render_errors(errors: &[CompileError], source: &str, filename: &str) -> String {
let mut out = Vec::new();
let mut cache = (filename, Source::from(source));
for err in errors {
err.report(filename)
.write(&mut cache, &mut out)
.expect("write to Vec<u8> cannot fail");
}
String::from_utf8_lossy(&out).into_owned()
}
pub fn render_errors_plain(errors: &[CompileError], source: &str, filename: &str) -> String {
let mut out = Vec::new();
let mut cache = (filename, Source::from(source));
for err in errors {
err.report_plain(filename)
.write(&mut cache, &mut out)
.expect("write to Vec<u8> cannot fail");
}
String::from_utf8_lossy(&out).into_owned()
}
pub fn print_errors(errors: &[CompileError], source: &str, filename: &str) {
let mut cache = (filename, Source::from(source));
for err in errors {
let _ = err.report(filename).eprint(&mut cache);
}
}
pub fn print_project_errors(root: &Path, errors: &[CompileError]) {
let _ = root;
for err in errors {
eprintln!("[{}] {}", err.category, err.message);
for note in &err.notes {
eprintln!(" note: {note}");
}
}
}
pub fn print_errors_short(errors: &[CompileError], source: &str, filename: &str) {
eprint!("{}", render_errors_short(errors, source, filename));
}
pub fn render_errors_short(errors: &[CompileError], source: &str, filename: &str) -> String {
let mut out = String::new();
for err in errors {
out.push_str(&short_line(filename, source, err));
out.push('\n');
}
out
}
pub fn short_line(filename: &str, source: &str, err: &CompileError) -> String {
let (line, col) = span::line_col(source, err.span.start);
format!(
"{filename}:{line}:{col}: {}[{}]: {}",
severity_word(err),
err.category,
err.message
)
}
pub fn severity_word(err: &CompileError) -> &'static str {
match Severity::for_error(err) {
Severity::Error => "error",
Severity::Warning => "warning",
}
}
pub fn render_project_errors(errors: &[CompileError]) -> String {
let mut out = String::new();
for err in errors {
out.push_str(&format!("[{}] {}\n", err.category, err.message));
for note in &err.notes {
out.push_str(&format!(" note: {note}\n"));
}
}
out
}