Crates.io | grafton-config |
lib.rs | grafton-config |
version | 0.2.1 |
source | src |
created_at | 2024-07-03 04:23:26.282437 |
updated_at | 2024-07-04 02:53:13.14259 |
description | Load configuration from toml files with token variable expansion and environment overrides |
homepage | https://www.grafton.ai |
repository | https://github.com/GrantSparks/grafton-config |
max_upload_size | |
id | 1290179 |
size | 58,560 |
grafton-config
is a Rust-based configuration library for Rust applications, featuring token expansion and layered configuration loading for TOML format configuration files.
Robust configuration management is crucial for:
grafton-config
addresses these challenges, providing a comprehensive solution for Rust developers.
Add grafton-config
to your Cargo.toml
:
[dependencies]
grafton-config = "*"
Let's create a basic configuration structure:
use serde::{Deserialize, Serialize};
use grafton_config::TokenExpandingConfig;
#[derive(Debug, Serialize, Deserialize)]
struct Server {
host: String,
port: u16,
database_url: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct AppConfig {
server: Server,
}
impl TokenExpandingConfig for AppConfig {}
This structure provides a clear, type-safe representation of your app's configuration.
use grafton_config::{load_config_from_dir, TokenExpandingConfig, Error};
fn main() -> Result<(), Error> {
let config = grafton_config::load_config_from_dir::<AppConfig>("examples/config")?;
println!("Database URL: {}", config.server.database_url);
Ok(())
}
To run the example from the repository, use the following command:
cargo run --example config_example
grafton-config
supports layered configurations, allowing different settings for various environments.
Configuration Files:
The configuration is loaded from the following files in a specified directory:
default.toml
: Your base configuration (required)local.toml
: Local overrides (optional){run_mode}.toml
: Environment-specific config (optional)The run_mode
is determined by the RUN_MODE
environment variable, defaulting to dev
if not set. Files are loaded in the order listed above, with later files overriding any values from earlier ones.
Example Setup:
default.toml
:
[server]
host = "localhost"
port = 5432
database_url = "postgresql://user:password@${server.host}:${server.port}/mydb"
local.toml
(optional, for local development):
[server]
host = "127.0.0.1"
prod.toml
(optional, for production. See run_mode above):
[server]
host = "db.production.com"
database_url = "postgresql://user:password@${server.host}:${server.port}/mydb"
Token expansion is a key feature of grafton-config
. It allows you to reference other values within your configuration, making it more dynamic and reducing redundancy.
Tokens use the ${key_path}
syntax, where key_path
is a dot-separated path to the desired value.
[database]
host = "db.example.com"
port = 5432
url = "postgresql://user:password@${database.host}:${database.port}/mydb"
Nested Paths:
[person]
first_name = "John"
last_name = "Doe"
full_name = "${person.first_name} ${person.last_name}"
Array Access:
fruits = ["apple", "banana", "cherry"]
favorite = "My favorite fruit is ${fruits.1}"
Escaping Tokens:
literal = "This is a \${literal} dollar sign"
grafton-config
handles various scenarios gracefully:
load_config_from_dir(path: &str) -> Result<T, Error>
: Load and parse configuration from a directoryGraftonConfig
: Trait for grafton-configuration structsTokenExpandingConfig
: Trait for configuration structs that support token expansionContributions are welcome! Please feel free to submit a Pull Request.
grafton-config
is dual-licensed under the Apache License 2.0 (for open-source use) and a commercial license.
Unless explicitly stated otherwise, all files in this repository are licensed under the Apache License 2.0. The full text of the license can be found in the LICENSE file.
For those wishing to integrate grafton-config
into closed-source applications or who need more flexibility than the Apache License 2.0 allows, a commercial license is available. For commercial licensing inquiries, please contact grant@grafton.ai