Crate salak[−][src]
Expand description
Salak is a multi layered configuration loader and zero-boilerplate configuration parser, with many predefined sources.
About
salak is a multi layered configuration loader with many predefined sources. Also it
is a zero-boilerplate configuration parser which provides an auto-derive procedure macro
to derive FromEnvironment so that we can parse configuration structs without any additional codes.
Quick Start
A simple example of salak:
use salak::*; #[derive(Debug, FromEnvironment)] #[salak(prefix = "config")] struct Config { #[salak(default = false)] verbose: bool, optional: Option<String>, #[salak(name = "val")] value: i64, } let env = Salak::builder() .set("config.val", "2021") .build() .unwrap(); let config = env.get::<Config>().unwrap(); assert_eq!(2021, config.value); assert_eq!(None, config.optional); assert_eq!(false, config.verbose);
Features
Predefined Sources
Predefined sources has the following order, Salak will find by sequence of these orders,
if the property with specified key is found at the current source, than return immediately. Otherwise,
it will search the next source.
- Random source provides a group of keys can return random values.
random.u8random.u16random.u32random.u64random.u128random.usizerandom.i8random.i16random.i32random.i64random.i128random.isize
- Custom arguments source.
SalakBuilder::set()can set a single kv, andSalakBuilder::set_args()can set a group of kvs. - System environment source. Implemented by
source::system_environment. - Profile specified file source, eg.
app-dev.toml, supports reloading. - No profile file source, eg.
app.toml, supports reloading. - Custom sources, which can register by
Salak::register().
Key Convention
Key is used for search configuration from Environment, normally it is represented by string.
Key is a group of SubKey separated by dot(.), and SubKey is a name or a name followed by index.
- SubKey Format (
[a-z][_a-z0-9]+(\[[0-9]+\])*)aa0a_ba[0]a[0][0]
- Key Format (
SubKey(\.SubKey)*)aa.ba.val[0]a_b[0]
Value Placeholder Parsing
- Placeholder Format
${key}=> Get value ofkey.${key:default}=> Get value ofkey, if not exists returndefault.
- Escape Format
\$\{key\}=> Return${key}.$,\,{,}must use escape format.
Attributes For Derive
salak supports some attributes for automatically derive FromEnvironment.
All attributes have format #[salak(..)], eg. #[salak(default = "default value")].
- Struct Header Attribute.
#[salak(prefix = "salak.application")], has this attr will auto implementPrefixedFromEnvironment.
- Struct Field Attribute.
#[salak(default = "value")], this attr can specify default value.#[salak(name = "key")], this attr can specify property key, default convension is use field name.#[salak(desc = "Field Description")], this attr can be describe this property.
Reload Configuration
salak supports reload configurations. Since in rust mutable
and alias can’t be used together, here we introduce a wrapper
wrapper::IORef for updating values when reloading.
Resource Factory
Resource defines a standard way to create instance. Factory provides functions to initialize resource
and cache resource. Please refer to salak_factory for resource usage.
Feature ‘app’ should be open for this feature.
Modules
| source | Salak sources. |
| wrapper | Salak wrapper for configuration parsing. |
Macros
| app_info | argsGenerate |
| impl_enum_property | Implement enum as |
| inline_toml | tomlInline toml file as |
Structs
| AppInfo | argsApplication info. |
| ResourceBuilder | appResource builder. |
| Salak | Salak is a wrapper for salak env, all functions that this crate provides will be implemented on it. |
| SalakBuilder | A builder which can configure for how to build a salak env. |
| SalakContext | Context for implementing |
| SalakDescContext | deriveContext for implementing description. |
Enums
| Property | Raw property, it is a temprory representation of property, which can be either |
| PropertyError | Property error for the whole crate. |
Traits
| AutoDeriveFromEnvironment | deriveThis trait is automatically derived. |
| DescFromEnvironment | deriveGenerate description for this object. |
| EnumProperty | Any enum implements this trait is automatically implementing |
| Environment | Environment defines interface for getting values, and reloading configurations. |
| Factory | appFactory is a resource manager for initializing resource or getting resource from cache. |
| FromEnvironment | Parsing value from environment by |
| IsProperty | Any object implements this trait is automatically implmenting |
| PrefixedFromEnvironment | derive
|
| PropertySource | A property source defines how to load properties.
|
| Resource | appResource can be initialized in a standard way by |
Derive Macros
| FromEnvironment | deriveAuto derive |