toml-env
A simple configuration library using toml.
This library is designed to load a configuration for an application at startup using the initialize() function. The configuration can be loaded (in order of preference):
- From a dotenv style file
.env.toml(a file name of your choosing) - From an environment variable
CONFIG(or a variable name of your choosing). - From mapped environments (e.g.
MY_VARIABLE => my_variable.child). - From a configuration file.
Why yet another config library?
Here are some possible alternatives to this library:
configYou want maximum flexibility.figmentYou want maximum flexibility.just-configYou want maximum flexibility.dotenvyYou just want.envsupport.env_inventoryYou just want environment variable configuration file support.
Why would you use this one?
- Small feature set.
- Minimal dependencies.
- Opinionated defaults.
.envusingTOMLwhich is a more established file format standard.- Loading config from TOML stored in a multiline environment variable.
- For large configurations with nested maps, this could be seen as a bit more legible than
MY_VARIABLE__SOMETHING_ELSE__SOMETHING_SOMETHING_ELSE. - You can also just copy text from a TOML file to use in the environment variable instead of translating it into complicated names of variables.
- For large configurations with nested maps, this could be seen as a bit more legible than
Config Struct
Firstly you need to define your struct which implements serde::de::DeserializeOwned + serde::Serialize + Default:
.env.toml
Initally configuration will attempted to be loaded from a file named .env.toml by default. You can elect to customize the name of this file. The format of this file is as follows:
="some value"
="some other value"
[]
="some value"
="some other value"
[]
="some other other value"
Environment variables for the application can be set using the top level keys in the file (e.g. SECRET_ENV_VAR_1).
The configuration can be loaded from a subset of this file in CONFIG. The CONFIG key will be the name from the Args::config_variable_name which is CONFIG by default.
Environment Variable CONFIG
You can specify the configuration by storing it in the variable name as specified using Args::config_variable_name (CONFIG by default).
# Store a multiline string into an environment variable in bash shell.
Example
use ;
use tempdir;
use ;
let dir = tempdir.unwrap;
let dotenv_path = dir.path.join;
let config_path = dir.path.join;
// Normally you would read this from .env.toml file
write
.unwrap;
// Normally you may choose set this from a shell script or some
// other source in your environment (docker file or server config file).
set_var;
set_var;
// Normally you would read this from config.toml
// (or whatever name you want) file.
write
.unwrap;
let config: Config = initialize
.unwrap
.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let secret = var.unwrap;
assert_eq!;
Changelog
See CHANGELOG.md for an account of changes to this library.