midenc_driver/
lib.rs

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