dotenv_conf
The dotenv_conf
is made to support both configurations via environment
variables (via envoy
) and .env files.
.env files are read using read_file
, then passed to the specific read_
functions, to avoid reading the file multiple times.
Types
EnvFile
represents the environment file result.
When the file is read correctly, it contains Ok(Dict(String, String))
with all the parsed values. If an error occurs, it contains
Error(EnvFileError)
.
pub type EnvFile =
Result(Dict(String, String), EnvFileError)
EnvFileError
is an enumeration of the possible errors when dealing with
the .env file.
MissingFile
: the file is not found at the provided path.InvalidFile
: the file is not correct (likely is a directory or special).FileError
: an error occured when reading the file.BadConfig
: the format of the file is not correct.MissingVar
: the config variable does not exist.
pub type EnvFileError {
MissingFile
InvalidFile
FileError
BadConfig
MissingVar
}
Constructors
-
MissingFile
-
InvalidFile
-
FileError
-
BadConfig
-
MissingVar
Functions
pub fn read_file(
path: String,
then: fn(Result(Dict(String, String), EnvFileError)) -> a,
) -> a
Reads the .env file at the given path
. It is meant to be used as a use
expression.
Example
use file <- read_file(".env")
let username = read_string_or("USER", file, "admin")
pub fn read_float_or(
name: String,
from_file: Result(Dict(String, String), EnvFileError),
default: Float,
) -> Float
Reads the config float with the given name
, first from the environment
variables and, if not found, from the EnvFile read using read_file
. If
neither works, the given default
value is used.
pub fn read_int_or(
name: String,
from_file: Result(Dict(String, String), EnvFileError),
default: Int,
) -> Int
Reads the config integer with the given name
, first from the environment
variables and, if not found, from the EnvFile read using read_file
. If
neither works, the given default
value is used.
pub fn read_string(
name: String,
from_file: Result(Dict(String, String), EnvFileError),
) -> Result(String, EnvFileError)
Reads the config string with the given name
. If it is not found in the
environment variables, the EnvFile read using read_file
is used.
If there was an error reading the file or the key is not found in the file
either, an Error(EnvFileError)
is returned.
This function can be used to implement custom config types, otherwise
read_<type>_or
is preferrable.
pub fn read_string_or(
name: String,
from_file: Result(Dict(String, String), EnvFileError),
default: String,
) -> String
Reads the config string with the given name
, first from the environment
variables and, if not found, from the EnvFile read using read_file
. If
neither works, the given default
value is used.