extract_env

Function extract_env 

Source
pub fn extract_env(
    file_path: &Path,
) -> TreeResult<(BTreeMap<String, String>, Vec<PathBuf>)>
Expand description

Extracts environment variables and the parent path from a specified file.

This function reads the given file_path to:

  1. Identify and extract environment variables specified using the export keyword.
  2. Identify any parent environment file via the special # rsenv: comment. parent’s path can be relative to the child’s path.

The current working directory is temporarily changed to the directory of the file_path during the extraction process to construct correct parent paths. It is restored afterward.

§Arguments

  • file_path - A string slice representing the path to the .env file. The function will attempt to canonicalize this path.

§Returns

A Result containing:

  • A tuple with:
    • A BTreeMap with the key as the variable name and the value as its corresponding value.
    • An Option containing a Utf8PathBuf pointing to the parent env file, if specified.
  • An error if there’s any problem reading the file, extracting the variables, or if the path is invalid.

§Errors

This function will return an error in the following situations:

  • The provided file_path is invalid.
  • There’s an issue reading or processing the env file.
  • The parent path specified in # rsenv: is invalid or not specified properly.