proc_result/
raw.rs

1//! Raw exit code and status representations.
2//!
3//! The `raw` module provides a platform-agnostic representation of exit codes [`RawExitCode`], as
4//! well as a concrete implementation for Unix and Windows platforms. While useful, the `raw` module
5//! is not necessary for most applications.
6
7use core::fmt::Display;
8use std::fmt::Debug;
9
10use num_traits::{PrimInt, Zero};
11
12/// A trait that represents a raw platform-specific exit code.
13pub trait RawExitCode: Clone + Copy + Debug + PartialEq + Eq {
14    /// Underlying code type.
15    type Code: PrimInt + Display;
16
17    /// Returns whether the exit status indicates success.
18    fn is_success(&self) -> bool {
19        self.to_raw().is_zero()
20    }
21
22    /// Returns whether the exit status indicates failure.
23    fn is_failure(&self) -> bool {
24        !self.is_success()
25    }
26
27    /// Create a [`RawExitCode`] from the underlying code.
28    fn from_raw(code: Self::Code) -> Self;
29
30    /// Returns the underlying code.
31    fn to_raw(&self) -> Self::Code;
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::unix::ExitCode;
38
39    #[test]
40    fn test_into_raw_exit_code() {
41        fn arg_raw_exit_code<T: RawExitCode>(code: impl Into<T>) -> T {
42            code.into()
43        }
44
45        let code: ExitCode = arg_raw_exit_code(0);
46        assert!(code.is_success());
47        assert_eq!(code.to_raw(), 0);
48
49        let code: ExitCode = arg_raw_exit_code(1);
50        assert!(code.is_failure());
51        assert_eq!(code.to_raw(), 1);
52    }
53}