exitcode/lib.rs
1//! Preferred system exit codes as defined by sysexits.h
2//!
3//! Exit code constants intended to be passed to
4//! `std::process::exit()`
5//!
6//! # Example:
7//! ```
8//! extern crate exitcode;
9//!
10//! ::std::process::exit(exitcode::OK);
11//! ```
12
13/// Alias for the numeric type that holds system exit codes.
14pub type ExitCode = i32;
15
16/// Successful exit
17pub const OK: ExitCode = 0;
18
19/// The command was used incorrectly, e.g., with the
20/// wrong number of arguments, a bad flag, a bad syntax
21/// in a parameter, etc.
22pub const USAGE: ExitCode = 64;
23
24/// The input data was incorrect in some way. This
25/// should only be used for user's data and not system
26/// files.
27pub const DATAERR: ExitCode = 65;
28
29/// An input file (not a system file) did not exist or
30/// was not readable. This could also include errors
31/// like "No message" to a mailer (if it cared to
32/// catch it).
33pub const NOINPUT: ExitCode = 66;
34
35/// The user specified did not exist. This might be
36/// used for mail addresses or remote logins.
37pub const NOUSER: ExitCode = 67;
38
39/// The host specified did not exist. This is used in
40/// mail addresses or network requests.
41pub const NOHOST: ExitCode = 68;
42
43/// A service is unavailable. This can occur if a
44/// support program or file does not exist. This can also
45/// be used as a catchall message when something you
46/// wanted to do doesn't work, but you don't know why.
47pub const UNAVAILABLE: ExitCode = 69;
48
49/// An internal software error has been detected. This
50/// should be limited to non-operating system related
51/// errors as possible.
52pub const SOFTWARE: ExitCode = 70;
53
54/// An operating system error has been detected. This
55/// is intended to be used for such things as "cannot
56/// fork", "cannot create pipe", or the like. It
57/// includes things like getuid returning a user that
58/// does not exist in the passwd file.
59pub const OSERR: ExitCode = 71;
60
61/// Some system file (e.g., /etc/passwd, /var/run/utmp,
62/// etc.) does not exist, cannot be opened, or has some
63/// sort of error (e.g., syntax error).
64pub const OSFILE: ExitCode = 72;
65
66/// A (user specified) output file cannot be created.
67pub const CANTCREAT: ExitCode = 73;
68
69/// An error occurred while doing I/O on some file.
70pub const IOERR: ExitCode = 74;
71
72/// Temporary failure, indicating something that is not
73/// really an error. In sendmail, this means that a
74/// mailer (e.g.) could not create a connection, and
75/// the request should be reattempted later.
76pub const TEMPFAIL: ExitCode = 75;
77
78/// The remote system returned something that was
79/// "not possible" during a protocol exchange.
80pub const PROTOCOL: ExitCode = 76;
81
82/// You did not have sufficient permission to perform
83/// the operation. This is not intended for file system
84/// problems, which should use `NOINPUT` or `CANTCREAT`,
85/// but rather for higher level permissions.
86pub const NOPERM: ExitCode = 77;
87
88/// Something was found in an unconfigured or misconfigured state.
89pub const CONFIG: ExitCode = 78;
90
91/// Check if exit code given by `code` is successful
92///
93/// # Example:
94/// ```
95/// extern crate exitcode;
96///
97/// assert!(exitcode::is_success(exitcode::OK));
98/// assert!(!exitcode::is_success(exitcode::USAGE));
99/// assert!(exitcode::is_success(0));
100/// ```
101pub fn is_success(code: ExitCode) -> bool {
102 code == OK
103}
104
105/// Check if exit code given by `code` is an error
106///
107/// # Example:
108/// ```
109/// extern crate exitcode;
110///
111/// assert!(exitcode::is_error(exitcode::USAGE));
112/// assert!(!exitcode::is_error(exitcode::OK));
113/// assert!(exitcode::is_error(1));
114/// ```
115pub fn is_error(code: ExitCode) -> bool {
116 !is_success(code)
117}