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, Option, and
bool 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
Result,
Option, and
bool types as appropriate.
For the crate’s change history, see the NEWS.md file in the source distribution.
Structs
ExpectedResult methods.