Replace maybe_t::missing_or_empty with a more Rust-friendly helper

There are many places where we want to treat a missing variable the same as
a variable with an empty value.

In C++ we handle this by branching on maybe_t<env_var_t>::missing_or_empty().
If it returns false, we go on to access maybe_t<env_var_t>::value() aka
operator*.

In Rust, Environment::get() will return an Option<EnvVar>.
We could define a MissingOrEmpty trait and implement it for Option<EnvVar>.

However that will still leave us with ugly calls to Option::unwrap()
(by convention Rust does use shorthands like *).

Let's add a variable getter that returns none for empty variables.
This commit is contained in:
Johannes Altmanninger
2023-04-20 12:24:53 +02:00
parent 82a797db9c
commit 1df64a4891
15 changed files with 64 additions and 67 deletions

View File

@@ -353,13 +353,13 @@ static base_directory_t make_base_directory(const wcstring &xdg_var,
// uvars are available.
const auto &vars = env_stack_t::globals();
base_directory_t result{};
const auto xdg_dir = vars.get(xdg_var, ENV_GLOBAL | ENV_EXPORT);
if (!xdg_dir.missing_or_empty()) {
const auto xdg_dir = vars.get_unless_empty(xdg_var, ENV_GLOBAL | ENV_EXPORT);
if (xdg_dir) {
result.path = xdg_dir->as_string() + L"/fish";
result.used_xdg = true;
} else {
const auto home = vars.get(L"HOME", ENV_GLOBAL | ENV_EXPORT);
if (!home.missing_or_empty()) {
const auto home = vars.get_unless_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT);
if (home) {
result.path = home->as_string() + non_xdg_homepath;
}
}