pub const SUCCESS: i32 = 0;
pub const FAILURE: i32 = 1;
pub const USAGE: i32 = 2;
pub const HANGUP: i32 = 129;
pub const INTERRUPTED: i32 = 130;
pub const TERMINATED: i32 = 143;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Status {
pub code: i32,
pub means: &'static str,
pub session_only: bool,
}
const fn status(code: i32, means: &'static str, session_only: bool) -> Status {
Status {
code,
means,
session_only,
}
}
pub const STATUSES: &[Status] = &[
status(
SUCCESS,
"Success: the session was quit (q, Ctrl+Q or Ctrl+C), or the command did what it was asked.",
false,
),
status(
FAILURE,
"An error, printed to standard error: a PATH that is not there, a configuration file that cannot be used, nothing piped in for -, or a command that failed (datui formats check on a spec with errors).",
false,
),
status(
USAGE,
"A usage error: an unknown option or command, or a value an option does not take.",
false,
),
status(
HANGUP,
"The terminal hung up (SIGHUP). The screen is restored and temporary files removed first.",
true,
),
status(
INTERRUPTED,
"Interrupted by SIGINT, as from kill -INT. The screen is restored and temporary files removed first.",
true,
),
status(
TERMINATED,
"Ended by SIGTERM. The screen is restored and temporary files removed first.",
true,
),
];
#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;
#[test]
fn a_usage_error_is_clap_s_status() {
let error = crate::Args::command()
.try_get_matches_from(["datui", "--no-such-option"])
.unwrap_err();
assert_eq!(error.exit_code(), USAGE);
let help = crate::Args::command()
.try_get_matches_from(["datui", "--help"])
.unwrap_err();
assert_eq!(help.exit_code(), SUCCESS);
}
#[test]
fn statuses_are_distinct_and_in_order() {
for pair in STATUSES.windows(2) {
assert!(pair[0].code < pair[1].code, "{pair:?}");
}
}
}