Function dt_core::utils::default_config_path
source · [−]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.
- 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()),
);- 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 withinsearch_listwill be checked in order and the first existing file’s path will be returned. If none of thesearch_listexists, a fallback filenameconfig.tomlwill 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()),
);- When neither of
env_for_file’s andenv_for_dir’s corresponding environment variable exists, the parent directory of returned path is inferred as$XDG_CONFIG_HOME/dt, or$HOME/.config/dtif 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()),
);