use std::collections::HashMap;
use std::path::Path;
use bynk_emit::project::{AttributedError, ProjectFailure};
pub fn render_project_failure(failure: &ProjectFailure) {
let texts = snapshot_map(failure);
for ae in &failure.errors {
match attributed_text(ae, &texts) {
Some((path, text)) => {
let label = path.to_string_lossy().replace('\\', "/");
bynk_render::print_errors(std::slice::from_ref(&ae.error), text, &label);
}
None => {
eprintln!("[{}] {}", ae.error.category, ae.error.message);
for note in &ae.error.notes {
eprintln!(" note: {note}");
}
}
}
}
}
pub fn render_project_failure_short(failure: &ProjectFailure) {
let texts = snapshot_map(failure);
for ae in &failure.errors {
let line = match attributed_text(ae, &texts) {
Some((path, text)) => {
let label = path.to_string_lossy().replace('\\', "/");
bynk_render::short_line(&label, text, &ae.error)
}
None => format!(
"{}[{}]: {}",
bynk_render::severity_word(&ae.error),
ae.error.category,
ae.error.message
),
};
eprintln!("{line}");
}
}
pub fn print_project_warnings(warnings: &[AttributedError]) {
for w in warnings {
let where_ = w
.source_path
.as_deref()
.map(|p| format!("{}: ", p.to_string_lossy().replace('\\', "/")))
.unwrap_or_default();
eprintln!("{where_}warning[{}]: {}", w.error.category, w.error.message);
for note in &w.error.notes {
eprintln!(" note: {note}");
}
}
}
fn snapshot_map(failure: &ProjectFailure) -> HashMap<&Path, &str> {
failure
.snapshots
.iter()
.map(|(p, t)| (p.as_path(), t.as_str()))
.collect()
}
fn attributed_text<'a>(
ae: &'a AttributedError,
texts: &HashMap<&'a Path, &'a str>,
) -> Option<(&'a Path, &'a str)> {
ae.source_path
.as_deref()
.and_then(|p| texts.get(p).map(|t| (p, *t)))
}