#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ExitClass {
Failure,
Usage,
Setup,
Internal,
}
impl ExitClass {
pub fn code(self) -> i32 {
match self {
ExitClass::Failure => 1,
ExitClass::Usage => 2,
ExitClass::Setup => 3,
ExitClass::Internal => 101,
}
}
}
pub trait CunoError: std::error::Error {
fn exit_class(&self) -> ExitClass;
fn help(&self) -> Option<String> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exit_codes_match_the_spec() {
assert_eq!(ExitClass::Failure.code(), 1);
assert_eq!(ExitClass::Usage.code(), 2);
assert_eq!(ExitClass::Setup.code(), 3);
assert_eq!(ExitClass::Internal.code(), 101);
}
#[derive(Debug)]
struct NoHelp;
impl std::fmt::Display for NoHelp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("x")
}
}
impl std::error::Error for NoHelp {}
impl CunoError for NoHelp {
fn exit_class(&self) -> ExitClass {
ExitClass::Failure
}
}
#[test]
fn errors_have_no_help_unless_they_give_one() {
assert_eq!(NoHelp.help(), None);
}
}