use std::fmt;
use std::process;
pub const EXIT_SUCCESS: ExitStatus = ExitStatus::Code(0);
pub const EXIT_ERROR: ExitStatus = ExitStatus::Code(1);
pub const EXIT_CMD_NOT_EXECUTABLE: ExitStatus = ExitStatus::Code(126);
pub const EXIT_CMD_NOT_FOUND: ExitStatus = ExitStatus::Code(127);
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum ExitStatus {
Code(i32),
Signal(i32),
}
impl ExitStatus {
pub fn success(self) -> bool {
self == EXIT_SUCCESS
}
}
impl fmt::Display for ExitStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ExitStatus::Code(code) => write!(f, "exit code: {}", code),
ExitStatus::Signal(code) => write!(f, "signal: {}", code),
}
}
}
impl From<process::ExitStatus> for ExitStatus {
fn from(exit: process::ExitStatus) -> ExitStatus {
#[cfg(unix)]
fn get_signal(exit: process::ExitStatus) -> Option<i32> {
::std::os::unix::process::ExitStatusExt::signal(&exit)
}
#[cfg(windows)]
fn get_signal(_exit: process::ExitStatus) -> Option<i32> {
None
}
match exit.code() {
Some(code) => ExitStatus::Code(code),
None => get_signal(exit).map_or(EXIT_ERROR, ExitStatus::Signal),
}
}
}