expect-exit 0.2.0

Result.expected(): display an error message and exit without a panic.
Documentation

expect-exit: display an error message and exit without a panic

The expect-exit library defines the Expected trait and implements it for the standard Result enum so that a program can display an error message and exit with a non-zero exit code without invoking a Rust panic, yet optionally unwinding the stack so that various objects may perform some clean-up actions.

Functions

  • exit_unwind(code): unwind the stack and exit with the specified code
  • exit(msg): display a message and exit with code 1
  • exit_perror(msg, err): display a message and the error itself and exit with code 1
  • die(msg): the same as exit(), but do not unwind the stack
  • die_perror(msg, err): the same as exit_perror(), but do not unwind the stack

Traits

  • Expected: declare two methods:

    • expect_or_exit(msg): display the message and exit if the expectation is not met
    • expect_or_die(msg): the same as expect_or_exit(), but do not unwind the stack
  • ExpectedWithError: declare two additional methods:

    • expect_or_exit_perror(msg): display the message and an appropriate error description and exit if the expectation is not met
    • expect_or_die_perror(msg): the same as expect_or_exit_perror(), but do not unwind the stack
  • an implementation of the Expected trait for the standard Option enum

  • an implementation of the Expected and ExpectedWithError traits for the standard Result enum

Example

use expect_exit::Expected;

fn get_env_var(name: &str) -> String {
    env::var(name).expect_or_exit(&format!("{} not specified in the environment", name))
}