cfgmatic-source 3.0.1

Configuration sources (file, env, remote) for cfgmatic framework
Documentation

cfgmatic-source

Crates.io Documentation License

Unified configuration source abstraction for the cfgmatic framework.

Overview

cfgmatic-source provides a trait-based abstraction for loading configuration from various sources. It follows hexagonal architecture principles with clear separation between domain, application, and infrastructure layers.

Features

  • FileSource - Load configuration from files (TOML, JSON, YAML)
  • EnvSource - Load from environment variables with prefix support
  • MemorySource - In-memory source for testing
  • CompositeSource - Combine multiple sources with priorities
  • Async Support - Optional async API via async feature flag
  • Format Detection - Automatic format detection from file extensions

Installation

Add to your Cargo.toml:

[dependencies]
cfgmatic-source = "2.1"

Feature Flags

Feature Description Default
file File source support Yes
env Environment source Yes
toml TOML format support Yes
json JSON format support Yes
yaml YAML format support No
async Async API support No

Quick Start

File Source

use cfgmatic_source::prelude::*;

// Single file
let source = FileSource::new("config.toml");
let content = source.load_raw()?;

// Multiple files (merged)
let source = FileSource::builder()
    .path("config.default.toml")
    .path("config.local.toml")
    .required(false)
    .build()?;

let parsed = source.load()?;

Environment Source

use cfgmatic_source::prelude::*;

// With prefix
let source = EnvSource::with_prefix("MYAPP");
// Reads MYAPP_DATABASE_URL, MYAPP_SERVER_PORT, etc.

let content = source.load_raw()?;

Composite Source

use cfgmatic_source::prelude::*;

let composite = CompositeSource::builder()
    .add(FileSource::new("config.toml"), SourcePriority::default())
    .add(EnvSource::with_prefix("MYAPP"), SourcePriority::override_priority())
    .build()?;

let config = composite.load()?;

Using with Loader

use cfgmatic_source::prelude::*;
use serde::Deserialize;

#[derive(Deserialize)]
struct Config {
    server: ServerConfig,
    database: DatabaseConfig,
}

#[derive(Deserialize)]
struct ServerConfig {
    host: String,
    port: u16,
}

#[derive(Deserialize)]
struct DatabaseConfig {
    url: String,
}

let source = FileSource::new("config.toml");
let loader = Loader::new();

let config: Config = loader.load_as(&source)?;

Architecture

+-------------------------------------------------+
|                    lib.rs                        |
|              (Facade + Re-exports)               |
+-------------------------------------------------+
                       |
+-------------------------------------------------+
|                 Config Layer                     |
|         LoadOptions, SourceConfig               |
+-------------------------------------------------+
                       |
+-------------------------------------------------+
|              Application Layer                   |
|           Loader, SourceCoordinator             |
+-------------------------------------------------+
                       |
+-------------------------------------------------+
|             Infrastructure Layer                 |
|   FileSource, EnvSource, MemorySource, etc.     |
+-------------------------------------------------+
                       |
+-------------------------------------------------+
|                 Domain Layer                     |
|    Source trait, Format, RawContent, Errors     |
+-------------------------------------------------+

API Reference

See the API documentation for full details.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.