Skip to main content

mkit_cli/
exit.rs

1//! sysexits(3)-style exit codes for the mkit CLI.
2//!
3//! Shell scripts that pipe `mkit ... || handle` use `$?` to distinguish
4//! usage errors from transient transport failures. See `docs/CLI.md`
5//! §"Exit codes" for the documented contract.
6
7/// Successful termination.
8pub const OK: u8 = 0;
9
10/// Catch-all for errors that do not fit a more specific category.
11pub const GENERAL_ERROR: u8 = 1;
12
13/// Wrong args or unknown subcommand.
14pub const USAGE: u8 = 64;
15
16/// Malformed input — corrupt object, bad hash literal on the CLI, etc.
17pub const DATAERR: u8 = 65;
18
19/// Input file does not exist or is unreadable.
20pub const NOINPUT: u8 = 66;
21
22/// Transport could not connect.
23pub const UNAVAILABLE: u8 = 69;
24
25/// Cannot create an output file.
26pub const CANTCREAT: u8 = 73;
27
28/// Temporary failure; retry is safe.
29pub const TEMPFAIL: u8 = 75;
30
31/// Bad URL scheme or malformed server response.
32pub const PROTOCOL_ERROR: u8 = 76;
33
34/// Permission denied.
35pub const NOPERM: u8 = 77;
36
37/// Unknown config key or invalid config value.
38pub const CONFIG_ERROR: u8 = 78;
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn constants_match_sysexits() {
46        assert_eq!(OK, 0);
47        assert_eq!(GENERAL_ERROR, 1);
48        assert_eq!(USAGE, 64);
49        assert_eq!(DATAERR, 65);
50        assert_eq!(NOINPUT, 66);
51        assert_eq!(UNAVAILABLE, 69);
52        assert_eq!(CANTCREAT, 73);
53        assert_eq!(TEMPFAIL, 75);
54        assert_eq!(PROTOCOL_ERROR, 76);
55        assert_eq!(NOPERM, 77);
56        assert_eq!(CONFIG_ERROR, 78);
57    }
58
59    #[test]
60    fn error_codes_are_nonzero() {
61        for code in [
62            GENERAL_ERROR,
63            USAGE,
64            DATAERR,
65            NOINPUT,
66            UNAVAILABLE,
67            CANTCREAT,
68            TEMPFAIL,
69            PROTOCOL_ERROR,
70            NOPERM,
71            CONFIG_ERROR,
72        ] {
73            assert!(code != 0);
74        }
75    }
76}