Skip to content

pf_config

Crate: pf_config · Path: crates/config/pf_config
Description (Cargo.toml): Layered configuration loader (JSON, env, dotenv, Cargo.toml section) with optional WASM-friendly path later

Layered configuration loader that merges multiple sources in precedence order (low → high): Cargo.toml section (optional) → JSON files → .env (optional) → OS environment variables. Target-agnostic by design; initial implementation is native-only.

From crates/config/pf_config/Cargo.toml:

FeaturePurpose
default(empty — no features enabled by default)
dotenv.env file loading via dotenvy
cargo-tomlCargo.toml [package.metadata.*] section parsing via toml
  • load_config::<T>(opts) — load and deserialize config into a typed struct T: DeserializeOwned
  • load_config_untyped(opts) — load config as a raw serde_json::Value
  • LoadOptions — configuration for the loader:
    • json_paths: Vec<PathBuf> — JSON files to load (default: ./config.json)
    • enable_dotenv: bool — whether to load .env (default: true)
    • env_prefix: Option<&'static str> — filter env vars by prefix (default: "AGENT_")
    • env_key_transform: EnvKeyTransform — how to map env keys to nested JSON
    • required: bool — error if no sources found (default: false)
    • cargo: Option<CargoTomlOptions> — Cargo.toml table path (feature: cargo-toml)
  • EnvKeyTransform — enum: DoubleUnderscoreToNested (converts PREFIX_A__B__C to nested {"a":{"b":{"c": value}}})
  • CargoTomlOptionspath: PathBuf, table_path: Vec<&'static str> (feature: cargo-toml)
  • ConfigError — error enum: Io, JsonParse, TomlParse (feature: cargo-toml), TypeMismatch, NotFound
use pf_config::{load_config, LoadOptions};
use serde::Deserialize;
#[derive(Deserialize)]
struct AgentCfg {
name: String,
port: u16,
}
let opts = LoadOptions {
json_paths: vec!["./agent.json".into()],
env_prefix: Some("AGENT_"),
..Default::default()
};
let cfg: AgentCfg = load_config(opts)?;

Full API reference: cargo doc -p pf_config --no-deps --all-features