use std::fmt;
use crate::Signal;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExitStatus {
Exited(i8),
Signaled(Signal, bool),
}
impl ExitStatus {
pub fn success(&self) -> bool {
*self == ExitStatus::Exited(0)
}
pub fn code(&self) -> Option<i32> {
match *self {
ExitStatus::Exited(e) => Some(i32::from(e)),
ExitStatus::Signaled(_, _) => None,
}
}
pub fn signal(&self) -> Option<i32> {
match *self {
ExitStatus::Exited(_) => None,
ExitStatus::Signaled(sig, _) => Some(sig as i32),
}
}
}
impl fmt::Display for ExitStatus {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
use self::ExitStatus::*;
match *self {
Exited(c) => write!(fmt, "exited with code {c}"),
Signaled(sig, false) => {
write!(fmt, "killed by signal {:?}[{}]", sig, sig as i32)
}
Signaled(sig, true) => {
write!(
fmt,
"killed by signal {:?}[{}] (core dumped)",
sig, sig as i32
)
}
}
}
}
#[cfg(test)]
mod tests {
use nix::sys::signal::Signal;
use super::*;
#[test]
fn test_exit_status_1() {
assert!(ExitStatus::Exited(0).success());
}
#[test]
fn test_exit_status_2() {
assert!(!ExitStatus::Exited(1).success());
}
#[test]
fn test_exit_status_3() {
assert!(!ExitStatus::Signaled(Signal::SIGKILL, false).success());
}
#[test]
fn test_exit_status_4() {
assert_eq!(ExitStatus::Exited(0).code(), Some(0));
}
#[test]
fn test_exit_status_5() {
assert_eq!(ExitStatus::Exited(42).code(), Some(42));
}
#[test]
fn test_exit_status_6() {
assert_eq!(ExitStatus::Signaled(Signal::SIGKILL, false).code(), None);
}
#[test]
fn test_exit_status_7() {
assert_eq!(ExitStatus::Exited(0).signal(), None);
}
#[test]
fn test_exit_status_8() {
let status = ExitStatus::Signaled(Signal::SIGTERM, false);
assert_eq!(status.signal(), Some(Signal::SIGTERM as i32));
}
#[test]
fn test_exit_status_9() {
let s = ExitStatus::Exited(0).to_string();
assert!(s.contains("exited"));
}
#[test]
fn test_exit_status_10() {
let s = ExitStatus::Signaled(Signal::SIGKILL, false).to_string();
assert!(s.contains("killed"));
assert!(!s.contains("core"));
}
#[test]
fn test_exit_status_11() {
let s = ExitStatus::Signaled(Signal::SIGSEGV, true).to_string();
assert!(s.contains("core"));
}
}