|
| 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. |
0 commit comments