1pub const SUCCESS: i32 = 0;
6pub const FAILURE: i32 = 1;
9pub const USAGE: i32 = 2;
12pub const HANGUP: i32 = 129;
14pub const INTERRUPTED: i32 = 130;
16pub const TERMINATED: i32 = 143;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct Status {
22 pub code: i32,
23 pub means: &'static str,
24 pub session_only: bool,
26}
27
28const fn status(code: i32, means: &'static str, session_only: bool) -> Status {
29 Status {
30 code,
31 means,
32 session_only,
33 }
34}
35
36pub const STATUSES: &[Status] = &[
38 status(
39 SUCCESS,
40 "Success: the session was quit (q, Ctrl+Q or Ctrl+C), or the command did what it was asked.",
41 false,
42 ),
43 status(
44 FAILURE,
45 "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).",
46 false,
47 ),
48 status(
49 USAGE,
50 "A usage error: an unknown option or command, or a value an option does not take.",
51 false,
52 ),
53 status(
54 HANGUP,
55 "The terminal hung up (SIGHUP). The screen is restored and temporary files removed first.",
56 true,
57 ),
58 status(
59 INTERRUPTED,
60 "Interrupted by SIGINT, as from kill -INT. The screen is restored and temporary files removed first.",
61 true,
62 ),
63 status(
64 TERMINATED,
65 "Ended by SIGTERM. The screen is restored and temporary files removed first.",
66 true,
67 ),
68];
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73 use clap::CommandFactory;
74
75 #[test]
77 fn a_usage_error_is_clap_s_status() {
78 let error = crate::Args::command()
79 .try_get_matches_from(["datui", "--no-such-option"])
80 .unwrap_err();
81 assert_eq!(error.exit_code(), USAGE);
82 let help = crate::Args::command()
83 .try_get_matches_from(["datui", "--help"])
84 .unwrap_err();
85 assert_eq!(help.exit_code(), SUCCESS);
86 }
87
88 #[test]
89 fn statuses_are_distinct_and_in_order() {
90 for pair in STATUSES.windows(2) {
91 assert!(pair[0].code < pair[1].code, "{pair:?}");
92 }
93 }
94}