validate_env_vars

Function validate_env_vars 

Source
pub fn validate_env_vars() -> Result<(), EnvInventoryError>
Expand description

Validates that all registered environment variables are set.

This function checks if the previously registered environment variables (via the register! macro or other means) are present either in the system’s environment or the loaded configuration files.

If any of the registered variables are missing, an EnvInventoryError::MissingEnvVars error is returned, containing a list of the missing variables.

§Parameters

  • config_paths: A slice of file paths (as &str) pointing to the configuration files that might contain the environment variables. These files are expected to be in TOML format with a dedicated [env] section.
  • section_name: The name of the section in the TOML files that contains the environment variables. By default, this is "env".

§Returns

  • Ok(()): If all registered environment variables are found.
  • Err(EnvInventoryError): If there’s an error reading or parsing the config files or if any registered environment variable is missing.

§Examples

let result = validate_env_vars(&["/path/to/settings.conf"], "env");
if result.is_err() {
    eprintln!("Failed to validate environment variables: {:?}", result);
}

§Errors

This function can return the following errors:

  • ReadFileError: If a provided config file cannot be read.
  • ParseFileError: If a provided config file cannot be parsed as TOML or lacks the expected structure.
  • MissingEnvVars: If one or more registered environment variables are missing.