Skip to content

Commit 97dffdc

Browse files
committed
functora-cli readme
1 parent 543118e commit 97dffdc

File tree

3 files changed

+127
-6
lines changed

3 files changed

+127
-6
lines changed

rust/functora-cfg/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
[package]
22
name = "functora-cfg"
3+
license = "MIT"
34
version = "0.1.0"
45
edition = "2024"
6+
keywords = ["config", "configuration", "settings", "env", "environment"]
7+
repository = "https://github.com/functora/functora.github.io/tree/master/rust/functora-cfg"
8+
description = "A Rust library that merges configuration values from multiple sources into a single typed value."
59

610
[dependencies]
711
config = "0.15.18"

rust/functora-cfg/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# functora-cfg
2+
3+
A Rust library that merges configuration values from multiple sources into a single typed value. Configuration values are applied in the following order:
4+
5+
- Defaults
6+
- Config file
7+
- Environment variables
8+
- Command-line arguments
9+
10+
All sources are optional. Only the ones you provide will be applied.
11+
12+
## Example
13+
14+
```rust
15+
use clap::{Parser, Subcommand};
16+
use functora_cfg;
17+
use serde::{Deserialize, Serialize};
18+
19+
#[derive(
20+
Debug, Clone, Serialize, Deserialize, PartialEq,
21+
)]
22+
pub struct Cfg {
23+
pub conn: String,
24+
pub logs: String,
25+
pub many: Vec<String>,
26+
pub nest: CfgNest,
27+
}
28+
29+
#[derive(
30+
Debug,
31+
Clone,
32+
Serialize,
33+
Deserialize,
34+
PartialEq,
35+
Subcommand,
36+
)]
37+
pub enum CfgNest {
38+
Nest { name: String, value: i32 },
39+
}
40+
41+
#[derive(Debug, Clone, Serialize, Parser)]
42+
#[command(version, about)]
43+
pub struct Cli {
44+
#[arg(long)]
45+
pub toml: Option<String>,
46+
#[arg(long)]
47+
pub conn: Option<String>,
48+
#[arg(long)]
49+
pub logs: Option<String>,
50+
#[arg(long)]
51+
pub many: Option<Vec<String>>,
52+
#[command(subcommand)]
53+
pub nest: Option<CfgNest>,
54+
}
55+
56+
fn new_cfg(cli: &Cli) -> Cfg {
57+
let defaults = Cli {
58+
toml: None,
59+
conn: Some("postgres://localhost".into()),
60+
logs: Some("/var/log/app.log".into()),
61+
many: Some(vec!["a".into(), "b".into()]),
62+
nest: Some(CfgNest::Nest {
63+
name: "foo".into(),
64+
value: 42,
65+
}),
66+
};
67+
68+
functora_cfg::Cfg {
69+
default: &defaults,
70+
file_path: |cli: &Cli| cli.toml.as_deref(),
71+
env_prefix: "FUNCTORA",
72+
command_line: cli,
73+
}
74+
.eval()
75+
.unwrap()
76+
}
77+
78+
fn main() {
79+
let cfg = new_cfg(&Cli::parse());
80+
println!("{:#?}", cfg);
81+
}
82+
```
83+
84+
Run with defaults:
85+
86+
```shell
87+
cargo run
88+
```
89+
90+
Use a config file to override defaults:
91+
92+
```shell
93+
cargo run -- --toml ./functora.toml
94+
```
95+
96+
Example `functora.toml`:
97+
98+
```toml
99+
conn = "postgres://remote"
100+
logs = "/tmp/app.log"
101+
many = ["x", "y", "z"]
102+
103+
[nest.Nest]
104+
name = "file_nested"
105+
value = 99
106+
```
107+
108+
Environment variables override defaults and file values:
109+
110+
```shell
111+
FUNCTORA_CONN="./sqlite.db" cargo run -- --toml ./functora.toml
112+
```
113+
114+
Command-line arguments override all other sources:
115+
116+
```shell
117+
cargo run -- --toml ./functora.toml --conn "./functora.db"
118+
```
119+
120+
<hr>
121+
122+
© 2025 [Functora](https://functora.github.io/). All rights reserved.

rust/functora-cfg/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![doc = include_str!("../README.md")]
12
pub use config::ConfigError;
23
use config::{Config, Environment, File, Value};
34
use serde::{Serialize, de::DeserializeOwned};
@@ -26,12 +27,6 @@ where
2627
Src: Serialize,
2728
Dst: Serialize + DeserializeOwned,
2829
{
29-
//
30-
// TODO : use functional style with tierator over
31-
// trait object array and try_fold into builder,
32-
// maybe somthing else, this looks too bad:
33-
//
34-
3530
let mut builder = Config::builder();
3631

3732
builder =

0 commit comments

Comments
 (0)