config-lib 1.4.0

Enterprise-grade multi-format configuration library supporting 8 formats (CONF, INI, Properties, JSON, XML, HCL, TOML, NOML) with sub-50ns caching, hot reloading, and comprehensive validation.
Documentation

Features

Multi-Format Support

  • CONF - Built-in parser for standard .conf files (default)
  • INI - Full INI file parsing with sections, comments, and data type detection
  • JSON - JSON format with edit capabilities and serialization (feature: jsonx on jsonx-lib, strict and dependency-free; or json on serde_json)
  • XML - Zero-copy XML parsing with quick-xml for Java/.NET environments
  • HCL - The static subset of the HashiCorp Configuration Language (attributes, labeled blocks, lists, objects, comments); expressions are refused
  • Properties - Complete Java .properties file parsing with Unicode and escaping
  • NOML - Advanced configuration with dynamic features (feature: noml)
  • TOML - TOML format with format preservation (feature: toml)

Enterprise Performance

  • Sub-50ns Cache Access Target - Multi-tier caching designed for sub-50ns reads on hot paths
  • Lock-Free Notification Dispatch - on_change handlers fire inline from the reloader thread via ArcSwap<Vec<Handler>>; one atomic load + N closure calls, no channel allocation, and no lock held (since v1.4.0), so a handler can read the configuration
  • Zero-Copy Parsing - Minimized allocations and string operations where possible
  • Lock-Free Read Paths - Poison-resistant locking with graceful failure recovery
  • Hot Value Cache - Ultra-fast access for frequently used values
  • Cache Hit Ratio Tracking - Built-in performance statistics and monitoring

Performance numbers, methodology, and tuning guidance live in docs/PERFORMANCE.md. The committed criterion harnesses in benches/ produce the measured baselines.

Production Features

  • Configuration Hot Reloading - File watching with thread-safe Arc swapping
  • Audit Logging System - Structured event logging with multiple sinks and severity filtering
  • Environment Variable Overrides - Smart caching with prefix matching and type conversion
  • Schema Validation - Trait-based validation system with feature gates
  • Format Preservation - An unmodified NOML or TOML configuration saves with its comments and layout; CONF saves read back to the same values, without comments
  • Async Native - Full async/await support throughout the API

Reliability & Safety

  • Zero Unsafe Code - All unwrap() calls eliminated, comprehensive error handling
  • Type Safety - Rich type system with automatic conversions and validation
  • Reads only what you give it - TOML never resolves NOML's include or env(); Config::from_string_strict refuses them in NOML too (since v1.3.0)
  • Bounded loads - files are regular files of at most 16 MiB, NOML includes stay in the config's directory, ${...} expansion is capped at 64 MiB, and errors never quote the input; LoadOptions sets the limits and strict mode for files and hot reload (since v1.4.0)
  • Typed reads that keep the file's value - INI and environment values 1 and 0 are integers (they still read as booleans), so get_or::<u16> sees them
  • Enterprise Error Handling - Production-ready error messages with context preservation
  • Comprehensive Testing - Extensive unit, integration, and doc test coverage

Why Choose config-lib?

Unified Multi-Format Support

Unlike single-format libraries, config-lib provides seamless access to 8 configuration formats through one consistent API. No need to learn different libraries for TOML, JSON, XML, and HCL - one API handles them all with automatic format detection.

Enterprise-Grade Performance

Multi-tier caching with lock-free read paths is designed to deliver sub-50ns cached access on hot paths. Built for high-throughput applications with minimal performance overhead.

Production-Ready Reliability

Zero unsafe code, comprehensive error handling, and poison-resistant locking ensure your configuration system won't crash your application. Extensive testing coverage validates edge cases and error conditions.

Developer Experience First

Rich type system with automatic conversions, format preservation for round-trip editing, and detailed error messages with source location context. No more cryptic parsing errors or manual type casting.

Advanced Enterprise Features

Hot reloading without service interruption, structured audit logging for compliance, environment variable overrides with smart caching, and schema validation with custom rules - features typically requiring multiple libraries.

Installation

Add to your Cargo.toml:

[dependencies]
config-lib = "1.4"

# For enhanced functionality, enable optional features:
config-lib = { version = "1.4", features = [
    "jsonx",          # JSON format support on jsonx-lib (strict, no dependencies; Rust 1.85+)
    "json",           # JSON format support on serde_json (used when `jsonx` is off)
    "xml",            # XML format support with quick-xml backend  
    "hcl",            # HashiCorp Configuration Language support
    "toml",           # TOML format support with preservation
    "noml",           # NOML format support with dynamic features
    "schema",         # Schema validation (`Schema`, `SchemaBuilder`)
    "validation",     # Validation rules (`validation` module)
    "async",          # Async file loading on tokio
    "env-override",   # Environment variable override system
] }

Feature Recommendations:

  • Minimal: Use default features for CONF/INI/Properties support
  • Web Applications: Add "jsonx" (or "json"), "env-override", "validation"
  • DevOps Tools: Add "hcl", "toml", "async"
  • Enterprise Systems: Add "xml", "validation", "env-override"
  • Full Featured: Include all features for maximum flexibility

Quick Start

use config_lib::Config;

// Parse a configuration; name the format. With `None`, text is read as
// CONF unless it clearly starts as XML or JSON or has `;` comments (INI).
let mut config = Config::from_string(r#"
[database]
host = "localhost"
port = 5432
username = "admin"

[app]
name = "MyApp"
debug = true
"#, Some("conf"))?;

// Typed reads with a default: the default's type picks the conversion.
let host: String = config.get_or("database.host", "127.0.0.1".to_string());
let port: u16 = config.get_or("database.port", 5432);
let debug = config.get_or("app.debug", false);
let workers: usize = config.get_or("app.workers", 4); // missing, so 4

// Reads that must succeed: `get` returns `Option<&Value>`.
let name = config
    .get("app.name")
    .ok_or_else(|| config_lib::Error::key_not_found("app.name"))?
    .as_string()?
    .to_owned();

// Modify configuration
config.set("database.port", 5433)?;
config.set("app.version", "1.0.0")?;

println!("{name}: connecting to {host}:{port} (debug {debug}, {workers} workers)");
Ok::<(), config_lib::Error>(())

get_or works for bool, String, f64, and every integer type from i8/u8 to i64/u64, isize, and usize. Integer reads are range-checked: a value that does not fit the requested type (say 70000 read as u16, or 8080.5 read as any integer) returns the default. The same conversions are available directly as TryFrom<Value> and TryFrom<&Value>.

Multi-Format Support

use config_lib::Config;

// The same calls work for every format; name it.
let config = Config::from_string(r#"
[server]
port = 8080
host = "localhost"
"#, Some("conf"))?;

let port: u16 = config.get_or("server.port", 80);
let timeout: u64 = config.get_or("server.timeout", 30);
let name: String = config.get_or("app.name", "DefaultApp".to_string());
assert_eq!((port, timeout, name.as_str()), (8080, 30, "DefaultApp"));

// Check existence
if config.contains_key("server.ssl") {
    println!("SSL configuration found");
}
Ok::<(), config_lib::Error>(())

Format names are "conf", "ini", "properties", "json", "xml", "hcl", "toml", and "noml"; each format other than CONF, INI, and properties needs its Cargo feature.

Read-only mode and forward-compatible options

ConfigOptions is the structured place to express the small set of behaviors that should not be enabled by default — making a Config read-only, sizing the cache, etc. The struct is #[non_exhaustive] so v1.x MINOR releases can add new knobs without breaking SemVer; callers go through the consuming builder methods.

use config_lib::{Config, ConfigOptions};

// Default options — caching on, writes allowed.
let _cfg = Config::with_options(ConfigOptions::default());

// Read-only configuration for a hot path that must never be mutated.
let opts = ConfigOptions::new().read_only(true);
let mut locked = Config::with_options(opts);
assert!(locked.set("foo", "bar").is_err());   // rejected

Default Configuration Settings

config-lib offers three ways to supply defaults for keys that are missing from a configuration file.

Method 1: Inline defaults with get_or (recommended)

The default sits at the read, and its type picks the conversion.

use config_lib::Config;

struct AppConfig {
    name: String,
    port: u16,
    debug: bool,
    timeout: u64,
    db_host: String,
    db_port: u16,
    db_pool_size: u32,
    analytics_enabled: bool,
    cache_enabled: bool,
}

let config = Config::from_file("app.conf")?;

let app_config = AppConfig {
    name: config.get_or("app.name", "DefaultApp".to_string()),
    port: config.get_or("server.port", 8080),
    debug: config.get_or("app.debug", false),
    timeout: config.get_or("server.timeout", 30),

    // Database configuration with sensible defaults
    db_host: config.get_or("database.host", "localhost".to_string()),
    db_port: config.get_or("database.port", 5432),
    db_pool_size: config.get_or("database.pool_size", 10),

    // Feature flags with conservative defaults
    analytics_enabled: config.get_or("features.analytics", false),
    cache_enabled: config.get_or("features.cache", true),
};
Ok::<(), config_lib::Error>(())

Method 2: A defaults table

set_default fills a table that get_or_default falls back to when the main value tree has no entry for the path.

use config_lib::{Config, Value};

let config = Config::from_string("[server]\nport = 9000", Some("conf"))?;

config.set_default("server.port", 8080)?;
config.set_default("server.workers", 4)?;
config.set_default("logging.level", "info")?;

// File value wins over the default.
assert_eq!(config.get_or_default("server.port"), Some(Value::Integer(9000)));

// Missing from the file, so the default.
assert_eq!(config.get_or_default("logging.level"), Some(Value::from("info")));
Ok::<(), config_lib::Error>(())

Method 3: The key accessor

use config_lib::Config;

let config = Config::from_string("[cache]\nttl = 600", Some("conf"))?;

let cache_ttl = config.key("cache.ttl").as_integer_or(3600);        // i64
let environment = config.key("app.environment").as_string_or("development");
let verbose = config.key("app.verbose").as_bool_or(false);
assert_eq!((cache_ttl, environment.as_str(), verbose), (600, "development", false));
Ok::<(), config_lib::Error>(())

Best Practices for Default Configuration

  1. Use Sensible Defaults: Choose defaults that work for most use cases
  2. Document Defaults: Keep defaults in sync with documentation
  3. Environment-Specific: Use different defaults for dev/staging/production
  4. Type Safety: Read into the narrowest type that fits (u16 for a port), so an out-of-range value is not accepted
  5. Validation: Validate both defaults and overrides

Environment Variable Integration

Needs the env-override feature.

use config_lib::env_override::EnvOverrideSystem;
use config_lib::Config;

let config = Config::from_file("app.conf")?;

// MYAPP_DATABASE_HOST overrides database.host, MYAPP_SERVER_PORT
// overrides server.port, and so on.
let overrides = EnvOverrideSystem::with_prefix("MYAPP_");
let config = Config::from(overrides.apply_overrides(config.as_value().clone())?);

let host: String = config.get_or("database.host", "localhost".to_string());
let port: u16 = config.get_or("server.port", 8080);
Ok::<(), config_lib::Error>(())

Configuration Validation

Needs the validation feature. Rules run against each top-level value.

use config_lib::validation::{RangeValidator, TypeValidator, ValidationRuleSet, ValueType};
use config_lib::Config;

let mut config = Config::from_string("server_port = 8080\nmax_connections = 100", Some("conf"))?;

config.set_validation_rules(
    ValidationRuleSet::new()
        .add_rule(TypeValidator::new(ValueType::Integer))
        .add_rule(RangeValidator::new(Some(1.0), Some(65535.0))),
);

let errors = config.validate()?;
assert!(errors.is_empty(), "{errors:?}");
Ok::<(), config_lib::Error>(())

Hot Reloading & Audit Logging

use config_lib::audit::{self, AuditLogger, AuditSeverity, ConsoleSink};
use config_lib::hot_reload::{ConfigChangeEvent, HotReloadConfig};

// Process-wide audit logger
audit::init_audit_logger(
    AuditLogger::new().add_sink(Box::new(ConsoleSink::new(AuditSeverity::Info))),
);

// Hot reloading: take the shared handle before `start_watching` consumes the watcher.
// A reload that fails to parse, holds no values, or fails the validator keeps
// the current configuration. Handlers run after the lock is released, so they
// may read the configuration.
let hot = HotReloadConfig::from_file("app.conf")?.with_validator(|config| {
    if config.contains_key("server.port") {
        Ok(())
    } else {
        Err(config_lib::Error::validation("server.port is required"))
    }
});
let shared = hot.config();
let reader = hot.config();
let _sub = hot.on_change(move |event| {
    if let ConfigChangeEvent::Reloaded { path, .. } = event {
        let port: u16 = reader.read().map(|c| c.get_or("server.port", 8080)).unwrap_or(8080);
        println!("reloaded {}: port {port}", path.display());
    }
});
let _handle = hot.start_watching();

let port: u16 = shared
    .read()
    .map(|config| config.get_or("server.port", 8080))
    .unwrap_or(8080);
Ok::<(), config_lib::Error>(())

Multi-File Configuration

use config_lib::Config;

// Later files override earlier ones, key by key.
let mut config = Config::from_file("default.conf")?;
config.merge(&Config::from_file("environment.conf")?)?;
config.merge(&Config::from_file("local.conf")?)?;

let database_url: String = config.get_or("database.url", String::new());
let debug_mode = config.get_or("debug", false);
Ok::<(), config_lib::Error>(())

Documentation & Resources

Documentation

External Resources

Getting Started Guides

Common Use Cases

  • Web Applications: Environment overrides, JSON/TOML configs
  • DevOps Tools: HCL integration, audit logging, hot reloading
  • Enterprise Systems: XML support, validation, caching
  • Microservices: Multi-format support, environment-based configuration

Version Compatibility

  • Rust: 1.75+ for the default feature set; 1.79+ with the xml feature (quick-xml 0.41); 1.82+ with the noml or toml features (noml = "=0.9.2"); 1.85+ with the jsonx feature (jsonx-lib = "=0.5.1")
  • Edition: 2021
  • MSRV Policy: MINOR releases may bump MSRV within the last-12-stable-Rust-versions window; PATCH releases never bump MSRV
  • API Stability: v1.x public API is frozen per docs/STABILITY-1.0.md. Growth-likely enums (Error, ConfigChangeEvent, etc.) are #[non_exhaustive]; new variants land in MINOR releases without breaking SemVer
  • Feature Flags: All optional features maintain independent compatibility

Development Setup

git clone https://github.com/jamesgober/config-lib.git
cd config-lib
cargo test --all-features  # Run comprehensive test suite
cargo bench               # Performance benchmarks
cargo clippy              # Lint checks (should show zero warnings)

Contributing

We welcome contributions! See our Contributing Guide for details.