pub fn default_config_path<P: AsRef<Path>>(
    env_for_file: &str,
    env_for_dir: &str,
    search_list: &[P]
) -> Result<PathBuf>
Expand description

Gets config path from environment variables, or infer one.

  1. If the environment variable indexed by env_for_file’s value is present, that environment variable’s value is returned as the config file path.

Example

std::env::set_var("DT_CLI_CONFIG_PATH", "/tmp/dt/configuration.toml");
assert_eq!(
    default_config_path::<&str>("DT_CLI_CONFIG_PATH", "", &[]),
    Ok(PathBuf::from_str("/tmp/dt/configuration.toml").unwrap()),
);
  1. Otherwise, if the environment variable indexed by env_for_dir’s value is present, that environment variable’s value is considered the parent directory of the returned config path, filenames within search_list will be checked in order and the first existing file’s path will be returned. If none of the search_list exists, a fallback filename config.toml will be used.

Example

std::env::set_var("DT_CONFIG_DIR", "/tmp/d/t");
assert_eq!(
    default_config_path::<&str>(
        "some_non_existing_var",
        "DT_CONFIG_DIR",
        &[],
    ),
    Ok(PathBuf::from_str("/tmp/d/t/config.toml").unwrap()),
);
  1. When neither of env_for_file’s and env_for_dir’s corresponding environment variable exists, the parent directory of returned path is inferred as $XDG_CONFIG_HOME/dt, or $HOME/.config/dt if XDG_CONFIG_HOME is not set in the runtime environment.

Example

std::env::set_var("XDG_CONFIG_HOME", "/tmp/confighome");
assert_eq!(
    default_config_path::<&str>(
        "some_non_existing_var",
        "some_other_non_existing_var",
        &[],
    ),
    Ok(PathBuf::from_str("/tmp/confighome/dt/config.toml").unwrap()),
);

std::env::remove_var("XDG_CONFIG_HOME");
std::env::set_var("HOME", "/tmp/home");
assert_eq!(
    default_config_path::<&str>(
        "some_non_existing_var",
        "some_other_non_existing_var",
        &[],
    ),
    Ok(PathBuf::from_str("/tmp/home/.config/dt/config.toml").unwrap()),
);