Skip to main content

dekopon_process/
exit.rs

1#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
2pub struct ExitCode(u8);
3
4impl ExitCode {
5    pub const SUCCESS: Self = Self(0);
6    pub const FAILURE: Self = Self(1);
7    pub const SYNTAX: Self = Self(2);
8    pub const TIMEOUT: Self = Self(124);
9    pub const CANCELLED: Self = Self(130);
10    pub const DENIED: Self = Self(126);
11    pub const NOT_FOUND: Self = Self(127);
12
13    #[must_use]
14    pub fn from_script_exit(status: i64) -> Self {
15        Self(u8::try_from(status.rem_euclid(256)).unwrap_or(0))
16    }
17
18    #[must_use]
19    pub const fn get(self) -> u8 {
20        self.0
21    }
22}
23
24impl From<ExitCode> for u8 {
25    fn from(code: ExitCode) -> Self {
26        code.0
27    }
28}
29
30impl From<u8> for ExitCode {
31    fn from(code: u8) -> Self {
32        Self(code)
33    }
34}
35
36impl std::fmt::Display for ExitCode {
37    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        write!(formatter, "{}", self.0)
39    }
40}