diff options
author | sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> | 2021-02-12 16:24:50 +0100 |
---|---|---|
committer | sternenseemann <sternenseemann@systemli.org> | 2021-02-25 19:52:33 +0100 |
commit | c66f141e26c11c641f2959f88e7f00a951cb567f (patch) | |
tree | 443c636f747152895f1c1827e02865ef8b970ef8 /pkgs/profpatsch/nman | |
parent | 3e62680966ff052638af7abc3ee05ef70bbe9fe0 (diff) |
pkgs/profpatsch/nman: refactor exit code handling, print error messages
* make exit code determination nicer syntactically by using an impl * add .msg() function which returns an appropriate error message for every error type.
Diffstat (limited to 'pkgs/profpatsch/nman')
-rw-r--r-- | pkgs/profpatsch/nman/nman.rs | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/pkgs/profpatsch/nman/nman.rs b/pkgs/profpatsch/nman/nman.rs index 475b42dd..53fd4b77 100644 --- a/pkgs/profpatsch/nman/nman.rs +++ b/pkgs/profpatsch/nman/nman.rs @@ -40,6 +40,8 @@ fn mktemp(suffix: &str) -> std::io::Result<TempDir> { } } +// TODO(sterni): distinguish between cmd not found and cmd failure +// TODO(sterni): track helpful error context in this enum enum NmanError { NoTempDir, Instantiate, @@ -50,16 +52,29 @@ enum NmanError { Usage, } -// TODO(sterni): return 127 for cmd not found -fn nman_error_exit_code(err: NmanError) -> i32 { - match err { - NmanError::NoTempDir => 9, - NmanError::Instantiate => 10, - NmanError::Build => 11, - NmanError::Man => 12, - NmanError::NotFound => 1, - NmanError::NixParseError => 69, // EX_SOFTWARE - NmanError::Usage => 64, // EX_USAGE +impl NmanError { + fn code(&self) -> i32 { + match self { + NmanError::NoTempDir => 9, + NmanError::Instantiate => 10, + NmanError::Build => 11, + NmanError::Man => 12, + NmanError::NotFound => 1, + NmanError::NixParseError => 69, // EX_SOFTWARE + NmanError::Usage => 64, // EX_USAGE + } + } + + fn msg(&self) -> &str { + match self { + NmanError::NoTempDir => "failed to create temporary directory", + NmanError::Instantiate => "failure evaluating the attribute", + NmanError::Build => "failed to realise derivation", + NmanError::Man => "couldn't open man page using man(1)", + NmanError::NotFound => "desired man page could not be found", + NmanError::NixParseError => "nix executable produced unexpected output", + NmanError::Usage => "usage error", + } } } @@ -248,7 +263,10 @@ fn main() -> std::io::Result<()> { match open_man_page(attr, section, page) { Ok(_) => Ok(()), // TODO(sterni): print error message - Err(t) => std::process::exit(nman_error_exit_code(t)), + Err(t) => { + println!("error: {}", t.msg()); + std::process::exit(t.code()) + }, }, } } @@ -281,7 +299,7 @@ fn main() -> std::io::Result<()> { // TODO(sterni): stderr println!("usage error: {}", msg); dispatch_action(&args[0], CliAction::Usage); - std::process::exit(nman_error_exit_code(NmanError::Usage)); + std::process::exit(NmanError::Usage.code()); }, } } |