Crate hexcfg

Crate hexcfg 

Source
Expand description

A hexagonal architecture configuration management crate.

This crate provides a flexible, type-safe configuration management system that can read configuration from multiple sources including environment variables, YAML files, command-line arguments, and remote services like etcd and Redis.

§Architecture

The crate follows hexagonal architecture principles:

  • Domain Layer: Core types and business logic (ConfigKey, ConfigValue, errors)
  • Ports: Trait definitions that define interfaces (ConfigSource, ConfigWatcher)
  • Adapters: Implementations for specific configuration sources (env vars, YAML, etc.)
  • Service: The main configuration service that orchestrates everything

§Features

  • Multiple Sources: Environment variables, YAML files, CLI arguments, etcd, Redis
  • Type Safety: Type-safe conversions from string values to Rust types
  • Precedence: Configurable precedence order (CLI > env > files by default)
  • Dynamic Reloading: Watch for configuration changes and reload automatically
  • Extensible: Easy to add new configuration sources via trait implementation

§Feature Flags

  • yaml: Enable YAML file support (default)
  • env: Enable environment variable support (default)
  • cli: Enable command-line argument support (default)
  • reload: Enable dynamic reloading with file watching
  • etcd: Enable etcd remote configuration support
  • redis: Enable Redis remote configuration support
  • remote: Enable all remote sources (etcd + redis)
  • full: Enable all features

§Quick Start

use hexcfg::prelude::*;

// Create a configuration service with environment variables
let service = DefaultConfigService::builder()
    .with_env_vars()
    .build()?;

// Get a configuration value (using convenient string slice methods)
let value = service.get_or_default_str("app.name", "MyApp");
println!("Application name: {}", value.as_str());

// Type-safe conversions
if let Ok(port_value) = service.get_str("app.port") {
    let port: i32 = port_value.as_i32("app.port").unwrap_or(8080);
    println!("Port: {}", port);
}

// Check if a key exists
if service.has_str("app.debug") {
    println!("Debug mode configured");
}

§Convenience Methods

For ergonomic usage, the crate provides _str variants that accept string slices:

§Examples

See the examples/ directory for comprehensive examples:

  • basic_usage.rs - Getting started with environment variables
  • multi_source.rs - Using multiple configuration sources with precedence
  • dynamic_reload.rs - Dynamic configuration reloading with file watching

Modules§

adapters
Adapters layer containing configuration source implementations.
domain
Domain layer containing core business logic and types.
ports
Ports layer containing trait definitions.
prelude
Commonly used types and traits.
service
Service layer containing the configuration service implementations.