use crate::{catalog::Catalog, diagnostic::{EngineDiagnostic, Severity}};
use tracing::{error, warn, info};
pub struct Engine {
catalog: Catalog,
}
impl Engine {
pub fn new(catalog: Catalog) -> Self {
Self { catalog }
}
pub fn message(&self, diag: &dyn EngineDiagnostic) -> String {
self.catalog.render(diag.code(), &diag.context())
}
pub fn log(&self, diag: &dyn EngineDiagnostic) {
let msg = self.message(diag);
match diag.severity() {
Severity::Error => error!(code = diag.code(), "{msg}"),
Severity::Warning => warn!(code = diag.code(), "{msg}"),
Severity::Info => info!(code = diag.code(), "{msg}"),
}
}
#[cfg(feature = "cli")]
pub fn print(&self, diag: &dyn EngineDiagnostic) {
use owo_colors::OwoColorize;
let msg = self.message(diag);
let hint = self.catalog.hint(diag.code());
match diag.severity() {
Severity::Error => eprintln!("{} {msg}", "✖".red().bold()),
Severity::Warning => eprintln!("{} {msg}", "⚠".yellow().bold()),
Severity::Info => println!("{} {msg}", "ℹ".blue().bold()),
}
if let Some(hint) = hint {
eprintln!(" {} {hint}", "→".dimmed());
}
}
}