use std::error::Error;
use std::fmt;
#[cfg(feature = "shell-timeout")]
use process_control::ExitStatus;
#[cfg(not(feature = "shell-timeout"))]
use std::process::ExitStatus;
#[cfg(feature = "shell-timeout")]
type ExitCode = i64;
#[cfg(not(feature = "shell-timeout"))]
type ExitCode = i32;
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct ExitStatusError(ExitStatus);
impl ExitStatusError {
pub fn code(&self) -> Option<ExitCode> {
self.0.code()
}
pub fn into_status(self) -> ExitStatus {
self.0
}
}
impl Error for ExitStatusError {}
impl fmt::Display for ExitStatusError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "process exited unsuccessfully: {}", self.into_status())
}
}
pub(crate) trait ExitStatusExt {
fn exit_ok(self) -> Result<(), ExitStatusError>;
}
impl ExitStatusExt for ExitStatus {
fn exit_ok(self) -> Result<(), ExitStatusError> {
if self.success() {
Ok(())
} else {
Err(ExitStatusError(self))
}
}
}