-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Deriving `Default` trait on a number of tests, so that `cargo test --all-features` test will pass * Adding CustomFn layer, where any Fn type, (functions or immutable closures) can provide a values. This is useful to support additional data sources, for example a database or an unsupported file format, e.g. hjson * Converting from Fn to FnOnce, because the 'static lifetime bound requirement on the closure makes an Fn version no better than a static fn. Fixing a wrong doc comment * Feature-gating tests, depending on cfg, so `cargo test` should pass, regardless of which combination of features are enabled * Adding mention of custom_fn feature in README Synchronizing 3 copies of README.md in the repo
- Loading branch information
1 parent
7ad38f3
commit 75b974a
Showing
21 changed files
with
160 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,10 @@ For now it supports : | |
|
||
- Default settings (inside your codebase with `#[serde(default = ...)]` coming from [serde](https://serde.rs)) | ||
- Reading from `TOML`, `YAML`, `JSON`, `DHALL`, `INI` files | ||
- Executing a custom function or closure to supply values via a [serde_json::Value] | ||
- Reading from environment variables: it supports `HashMap` structure with `MY_VARIABLE="mykey=myvalue,mykey2=myvalue2"` and also array like `MY_VARIABLE=first,second` thanks to [envy](https://github.com/softprops/envy). | ||
- All [serde](https://serde.rs) attributes can be used in your struct to customize your configuration as you wish | ||
- Reading your configuration from your command line built with [clap](https://github.com/clap-rs/clap) (ATTENTION: if you're using version < v3 use the `twelf@0.1.8` version, if you're using `[email protected]` please use `[email protected]`) | ||
- Reading your configuration from your command line built with [clap](https://github.com/clap-rs/clap) (ATTENTION: if you're using version < v3 use the `[email protected]` version) | ||
|
||
# Usage | ||
|
||
|
@@ -23,6 +24,7 @@ For now it supports : | |
use twelf::{config, Layer}; | ||
#[config] | ||
#[derive(Default)] | ||
struct Conf { | ||
test: String, | ||
another: usize, | ||
|
@@ -49,7 +51,6 @@ struct Conf { | |
// Will generate global arguments for each of your fields inside your configuration struct | ||
let app = clap::Command::new("test").args(&Conf::clap_args()); | ||
// If you're looking for how to use with clap derive feature, check in the examples directory (twelf/examples/clap_derive.rs) | ||
// Init configuration with layers, each layers override only existing fields | ||
let config = Conf::with_layers(&[ | ||
|
@@ -68,9 +69,13 @@ Check [here](./twelf/examples) for more examples. | |
Twelf supports crate features, if you only want support for `json`, `env` and `toml` then you just have to add this to your `Cargo.toml` | ||
|
||
```toml | ||
twelf = { version = "0.4", default-features = false, features = ["json", "toml", "env"] } | ||
twelf = { version = "0.11", default-features = false, features = ["json", "toml", "env"] } | ||
``` | ||
|
||
`default_trait` enables code for a layer that integrate fields derived with the [std::default::Default] trait. | ||
|
||
`custom_fn` enables code for a layer that allows a custom closure to be executed. | ||
|
||
Default features are `["env", "clap"]` | ||
|
||
# Contributing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,4 @@ | |
toml = [] | ||
yaml = [] | ||
default_trait = [] | ||
custom_fn = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ For now it supports : | |
|
||
- Default settings (inside your codebase with `#[serde(default = ...)]` coming from [serde](https://serde.rs)) | ||
- Reading from `TOML`, `YAML`, `JSON`, `DHALL`, `INI` files | ||
- Executing a custom function or closure to supply values via a [serde_json::Value] | ||
- Reading from environment variables: it supports `HashMap` structure with `MY_VARIABLE="mykey=myvalue,mykey2=myvalue2"` and also array like `MY_VARIABLE=first,second` thanks to [envy](https://github.com/softprops/envy). | ||
- All [serde](https://serde.rs) attributes can be used in your struct to customize your configuration as you wish | ||
- Reading your configuration from your command line built with [clap](https://github.com/clap-rs/clap) (ATTENTION: if you're using version < v3 use the `[email protected]` version) | ||
|
@@ -23,6 +24,7 @@ For now it supports : | |
use twelf::{config, Layer}; | ||
#[config] | ||
#[derive(Default)] | ||
struct Conf { | ||
test: String, | ||
another: usize, | ||
|
@@ -67,9 +69,13 @@ Check [here](./twelf/examples) for more examples. | |
Twelf supports crate features, if you only want support for `json`, `env` and `toml` then you just have to add this to your `Cargo.toml` | ||
|
||
```toml | ||
twelf = { version = "0.3", default-features = false, features = ["json", "toml", "env"] } | ||
twelf = { version = "0.11", default-features = false, features = ["json", "toml", "env"] } | ||
``` | ||
|
||
`default_trait` enables code for a layer that integrate fields derived with the [std::default::Default] trait. | ||
|
||
`custom_fn` enables code for a layer that allows a custom closure to be executed. | ||
|
||
Default features are `["env", "clap"]` | ||
|
||
# Contributing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#![cfg(feature = "custom_fn")] | ||
|
||
use config_derive::config; | ||
use twelf::Layer; | ||
|
||
use serde_json::value::to_value; | ||
|
||
#[test] | ||
fn simple_custom_fn() { | ||
#[config] | ||
#[derive(Debug, Default)] | ||
struct TestCfg { | ||
test: String, | ||
another: usize, | ||
} | ||
|
||
std::env::set_var("ANOTHER", "5"); | ||
|
||
let func = || { | ||
to_value(TestCfg{test: "from func".to_string(), another: 25}).unwrap() | ||
}; | ||
|
||
let prio = vec![Layer::CustomFn(func.into()), Layer::Env(None)]; | ||
let config = TestCfg::with_layers(&prio).unwrap(); | ||
assert_eq!(config.test, String::from("from func")); | ||
assert_eq!(config.another, 5usize); | ||
|
||
let prio = vec![Layer::Env(None), Layer::CustomFn(func.into())]; | ||
let config = TestCfg::with_layers(&prio).unwrap(); | ||
assert_eq!(config.test, String::from("from func")); | ||
assert_eq!(config.another, 25usize); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.