1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// SPDX-License-Identifier: AGPL-3.0-or-later
//! This module contains functions which are useful for configuring applications.

use std::env::VarError;

use crate::{bterr, Result};

/// Returns the value of the given environment variable, if it is defined and contains all unicode
/// characters. `Ok(None)` is returned if the environment variable is not defined, and [Err] is
/// returned if it contains non-unicode characters.
pub fn from_envvar(envvar_name: &str) -> Result<Option<String>> {
    match std::env::var(envvar_name) {
        Ok(uid_map) => Ok(Some(uid_map)),
        Err(err) => match err {
            VarError::NotPresent => Ok(None),
            VarError::NotUnicode(_) => Err(bterr!(
                "environment variable {envvar_name} contained non-unicode characters"
            )),
        },
    }
}