use std::process::ExitCode;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Verdict {
Pass,
Fail,
}
impl Verdict {
pub(crate) fn of(ok: bool) -> Verdict {
if ok { Verdict::Pass } else { Verdict::Fail }
}
}
pub(crate) fn code(outcome: &Result<Verdict>) -> ExitCode {
ExitCode::from(match outcome {
Ok(Verdict::Pass) => 0,
Err(Error::Usage(_)) => 2,
Ok(Verdict::Fail) | Err(_) => 1,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_goal_reached_is_0_a_failure_1_and_a_usage_error_2() {
assert_eq!(code(&Ok(Verdict::of(true))), ExitCode::from(0));
assert_eq!(code(&Ok(Verdict::of(false))), ExitCode::from(1));
assert_eq!(code(&Err(Error::Usage("no manifest".into()))), ExitCode::from(2));
let full = Error::Output(std::io::Error::other("no space left on device"));
assert_eq!(code(&Err(full)), ExitCode::from(1));
}
}