Crate salak[−][src]
A layered configuration loader with zero-boilerplate configuration management.
About
salak
is a rust version of layered configuration loader inspired by
spring-boot.
salak
also has a haskell version.
salak
defines following default PropertySource
s:
- Command line arguments using
clap
to parsing-P, --propery KEY=VALUE
. - System Environment.
- app.toml(*) in current dir and $HOME dir. Or if you specify
APP_CONF_DIR
dir, then only load toml in this dir.
* APP_CONF_NAME
can be specified to replace app
.
Placeholder format
${key:default}
means get value ofkey
, if not exists then returndefault
.${key}
means get value ofkey
, if not exists then returnPropertyError::NotFound(_)
.\$\{key\}
means escape to${key}
or u can usedisable_placeholder
attribute.
Key format
a.b.c
is a normal key separated by dot(.
).a.b[0]
,a.b[1]
,a.b[2]
... is a group of keys with arrays.- System environment key will be changed from
HELLO_WORLD
<=>hello.world
,HELLO__WORLD_HOW
<=>hello_world.how
,hello[1].world
=>HELLO_1_WORLD
<=>hello.1.world
.
Auto derived parameters.
attribute default
to set default value.
#[salak(default="string")]
#[salak(default=1)]
attribute disable_placeholder
to disable placeholder parsing.
#[salak(disable_placeholder)]
#[salak(disable_placeholder = true)]
Features
Default features
enable_log
, enable log record if enabled.enable_toml
, enable toml support.enable_derive
, enable auto deriveFromEnvironment
for struts.
Optional features
enable_clap
, enable default command line arguments parsing byclap
.enable_yaml
, enable yaml support.
Quick Example
use salak::*; #[derive(FromEnvironment, Debug)] pub struct DatabaseConfig { url: String, #[salak(default = "salak")] name: String, #[salak(default = "{database.name}")] username: String, password: Option<String>, #[salak(default = "${Hello}", disable_placeholder)] description: String, } fn main() { std::env::set_var("database.url", "localhost:5432"); let env = SalakBuilder::new() .with_default_args(auto_read_sys_args_param!()) // This line need enable feature `enable_clap`. .build(); match env.require::<DatabaseConfig>("database") { Ok(val) => println!("{:?}", val), Err(e) => println!("{}", e), } } // Output: DatabaseConfig { // url: "localhost:5432", // name: "salak", // username: "salak", // password: None, // description: "${Hello}" // }
Modules
args | Provide command line arguments |
env | Provide system environment |
map | Provide hashmap |
property |
|
toml | enable_toml Provide toml |
yaml | enable_yaml Provide yaml |
Macros
auto_read_sys_args_param | enable_clap Auto generate |
Structs
EnvironmentOption | An option be used to add default values for some keys. |
PlaceholderResolver | An implementation of |
Salak | A wrapper for |
SalakBuilder |
|
SourceRegistry | An implementation of |
Enums
Property | Unified property structure. |
PropertyError | Property Error |
Traits
Environment | An environment for getting properties in multiple |
FromEnvironment | Generate object from |
PropertySource | An abstract source loader from various sources, such as commandline arguments, system environment, files, etc. |
Derive Macros
FromEnvironment | enable_derive Auto derive |