use std::process::ExitCode;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Exit {
Success,
Internal,
Usage,
ConfigInvalid,
PlanRefused,
Connect,
RunFailed,
VerifyFailed,
ResumeRefused,
Interrupted,
}
impl Exit {
pub const ALL: [Exit; 10] = [
Exit::Success,
Exit::Internal,
Exit::Usage,
Exit::ConfigInvalid,
Exit::PlanRefused,
Exit::Connect,
Exit::RunFailed,
Exit::VerifyFailed,
Exit::ResumeRefused,
Exit::Interrupted,
];
pub const fn code(self) -> u8 {
match self {
Exit::Success => 0,
Exit::Internal => 1,
Exit::Usage => 2,
Exit::ConfigInvalid => 3,
Exit::PlanRefused => 4,
Exit::Connect => 5,
Exit::RunFailed => 6,
Exit::VerifyFailed => 7,
Exit::ResumeRefused => 8,
Exit::Interrupted => 130,
}
}
}
impl From<Exit> for ExitCode {
fn from(exit: Exit) -> Self {
ExitCode::from(exit.code())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn documented() -> Vec<u8> {
include_str!("../README.md")
.lines()
.skip_while(|l| !l.starts_with("| Code |"))
.skip(2)
.take_while(|l| l.starts_with('|'))
.map(|l| {
let cell = l.split('|').nth(1).unwrap().trim();
cell.parse()
.unwrap_or_else(|_| panic!("not an exit code: {cell}"))
})
.collect()
}
#[test]
fn the_documented_table_is_the_implemented_one() {
let implemented: Vec<u8> = Exit::ALL.iter().map(|e| e.code()).collect();
assert_eq!(documented(), implemented);
assert_eq!(implemented, [0, 1, 2, 3, 4, 5, 6, 7, 8, 130]);
}
}