Crate expect_exit
source · [−]Expand description
Display an error message and exit without a panic.
The expect-exit
library defines the Expected
, ExpectedWithError
,
and ExpectedResult
traits and implements them for the standard
Result
and Option
types as appropriate. This allows a program to 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.
The methods with an _e
suffix append an appropriate error message to
the supplied one. The methods with a _
suffix allow the caller to
specify an already-constructed message instead of a function that
returns it.
use std::collections::HashMap;
use std::env;
use expect_exit::{Expected, ExpectedWithError};
let mut vars: HashMap<String, String> = ["HOME", "PATH", "LOGNAME"]
.iter()
.map(|name| {
(
name.to_string(),
env::var(name).or_exit_e(|| format!("No '{} in the environment", name)),
)
})
.collect();
vars.insert(
"PWD".to_string(),
env::current_dir()
.or_exit_e_("Could not determine the current directory")
.to_str()
.or_exit_("Could not represent the path to the current directory")
.to_string(),
);
println!("{:?}", vars);
if !vars["PWD"].starts_with("/") {
expect_exit::exit("Expected an absolute path to the current directory");
}
The traits are currently implemented for the standard
Option
and
Result
types as appropriate.
For the crate’s change history, see the NEWS.md file in the source distribution.
Structs
The error object returned by the ExpectedResult
methods.
Traits
Unwrap or exit with the specified message.
Test the value and return a result object containing either the inner value or an error object that, when displayed, will provide the specified error message.
Unwrap or exit with an appropriate error message.
Functions
Display the specified message, then exit without unwinding the stack.
Display the specified message and append an appropriate description of the error, then exit without unwinding the stack.
Display the specified message, then unwind the stack and exit.
Display the specified message and append an appropriate description of the error, then unwind the stack and exit.
Unwind the stack and end the process with the specified exit code.