use typst::diag::{SourceDiagnostic, SourceResult, Warned};
use crate::{
diag::print_diagnostics,
world::{CompilerFeat, CompilerWorld},
CompileReport, DiagnosticFormat,
};
#[derive(Default, Clone)]
pub struct DiagnosticHandler {
pub diagnostic_format: DiagnosticFormat,
pub print_compile_status: bool,
}
impl DiagnosticHandler {
pub fn status(&self, rep: &CompileReport) {
if self.print_compile_status {
log::info!("{}", rep.message());
}
}
pub fn report_compiled<T, F: CompilerFeat>(
&self,
world: &CompilerWorld<F>,
res: Warned<SourceResult<T>>,
) -> Option<T> {
let (result, diag) = match res.output {
Ok(doc) => (Some(doc), res.warnings),
Err(diag) => (None, diag),
};
if !diag.is_empty() {
self.report(world, diag.iter());
}
result
}
pub fn report<'d, F: CompilerFeat>(
&self,
world: &CompilerWorld<F>,
diagnostics: impl Iterator<Item = &'d SourceDiagnostic>,
) {
let _err = print_diagnostics(world, diagnostics, self.diagnostic_format);
#[cfg(feature = "system-compile")]
if _err.is_err() {
log::error!("failed to print diagnostics: {_err:?}");
}
}
}