midenc_driver/
lib.rs

1mod midenc;
2
3pub use clap::Error as ClapError;
4use log::Log;
5pub use midenc_session::diagnostics;
6use midenc_session::diagnostics::{miette, Diagnostic, Report};
7
8pub use self::midenc::Midenc;
9
10/// A convenience alias for `Result<T, Report>`
11pub type DriverResult<T> = Result<T, Report>;
12
13#[derive(Debug, thiserror::Error, Diagnostic)]
14#[error(transparent)]
15#[diagnostic()]
16pub struct ClapDiagnostic {
17    #[from]
18    err: ClapError,
19}
20impl ClapDiagnostic {
21    pub fn exit(self) -> ! {
22        self.err.exit()
23    }
24}
25
26/// Run the driver as if it was invoked from the command-line
27pub fn run<P, A>(
28    cwd: P,
29    args: A,
30    logger: Box<dyn Log>,
31    filter: log::LevelFilter,
32) -> Result<(), Report>
33where
34    P: Into<std::path::PathBuf>,
35    A: IntoIterator<Item = std::ffi::OsString>,
36{
37    setup_diagnostics();
38
39    match Midenc::run(cwd, args, logger, filter) {
40        Err(report) => match report.downcast::<midenc_compile::CompilerStopped>() {
41            Ok(_) => Ok(()),
42            Err(report) => Err(report),
43        },
44        result => result,
45    }
46}
47
48fn setup_diagnostics() {
49    use diagnostics::ReportHandlerOpts;
50
51    let result =
52        diagnostics::reporting::set_hook(Box::new(|_| Box::new(ReportHandlerOpts::new().build())));
53    if result.is_ok() {
54        diagnostics::reporting::set_panic_hook();
55    }
56}