mise 2026.7.18

Dev tools, env vars, and tasks in one CLI
use crate::cmd::CmdLineRunner;
use eyre::Report;
#[cfg(unix)]
use nix::sys::signal::SIGTERM;
use std::error::Error;
use std::fmt::{self, Display, Formatter};

#[derive(Debug)]
struct ExitRequest {
    code: i32,
}

impl Display for ExitRequest {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "exit with status {}", self.code)
    }
}

impl Error for ExitRequest {}

/// Request a process status without terminating before callers can unwind.
pub fn request(code: i32) -> Report {
    Report::new(ExitRequest { code })
}

pub fn requested_exit_code(err: &Report) -> Option<i32> {
    err.downcast_ref::<ExitRequest>().map(|exit| exit.code)
}

pub fn kill_all() {
    #[cfg(unix)]
    CmdLineRunner::kill_all(SIGTERM);

    #[cfg(windows)]
    CmdLineRunner::kill_all();
}

/// Terminate only after command scopes and their destructors have unwound.
#[allow(clippy::disallowed_methods)]
pub fn terminate(code: i32) -> ! {
    debug!("exiting with code: {code}");
    std::process::exit(code)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn finds_requested_exit_code_through_context() {
        let err = request(42).wrap_err("context");
        assert_eq!(requested_exit_code(&err), Some(42));
    }
}