Skip to main content

Crate tanzim_load

Crate tanzim_load 

Source
Expand description

§tanzim-load

Package | Documentation | Repository

First stage of the tanzim pipeline: reads raw configuration bytes from a declared source.

§The Load trait

Implement Load to add a new source kind (protocol, service, database, …). It only fetches bytes — parsing happens in a later stage. The contract:

  • Return one Payload per config entry found; a single source may expand to many entries. Finding nothing is Ok(vec![]), not an error.
  • Set Payload::source on each entry to the concrete resource loaded (narrow the incoming source with Source::with_resource) so diagnostics stay precise.
  • Payload::maybe_name is the entry name (None merges into the root); Payload::maybe_format is a parser hint (e.g. json).
  • Read options off the source with Source::options() and the typed OptionValue accessors. Only validate options your loader reads; ignore unknown keys. Pick the Error variant that matches the failure (InvalidResource, InvalidOption, NotFound, NoAccess, Timeout, Duplicate, Load).

Register a loader with tanzim::Config::with_loader; it’s dispatched by the source strings its supported_source_list() returns. For a quick, stateless adapter, use closure::Closure instead of a full impl Load. See the Load rustdoc for worked details.

§Built-in loaders

ModuleFeatureSource string
envenvenv
filefilefile
httphttphttp
closureany (user-defined)

§Example

use std::env;
use tanzim_load::{env::Env, Error, Load, Source};

fn main() -> Result<(), Error> {
    // SAFETY: example-only; single-threaded doctest env vars.
    unsafe {
        env::set_var("MY_APP_CFG.DEBUG", "true");
        env::set_var("MY_APP_CFG.NAME", "hello");
        env::set_var("MY_APP_CFG.DATABASE.HOST", "localhost");
    }

    let source = Source::parse(r#"env(prefix=MY_APP_,separator=".")"#).unwrap();

    let payloads = Env::new().load(source)?;
    assert_eq!(payloads.len(), 1);

    let payload = payloads[0].clone();
    assert_eq!(payload.maybe_name, Some("cfg".into()));
    assert_eq!(payload.maybe_format, Some("env".into()));

    let content = String::from_utf8_lossy(&payload.content);
    assert!(content.contains("DEBUG=\"true\""));
    assert!(content.contains("NAME=\"hello\""));
    assert!(content.contains("DATABASE.HOST=\"localhost\""));

    Ok(())
}

§Features

FeatureEnables
envenv loader (reads environment variables)
filefile loader (reads from filesystem)
httphttp loader (user-provided fetch closure)
loggingemit log messages via the log crate
tracingemit trace spans via the tracing crate
fullenv + file + http

Default features: env, file, http. Logging/tracing are opt-in.

§Relations

Modules§

closure
Custom loader backed by a closure.
env
Environment-variable loader (env feature).
file
Filesystem loader (file feature).
http
HTTP loader (http feature).

Structs§

Options
Ordered map of loader options.
Payload
Raw bytes for one configuration entry, with its declaring Source.
Source
One configuration source declaration.

Enums§

Error
Errors a Load implementation can return.
OptionValue
Dynamically typed loader option or nested option map.

Traits§

Load
Loads raw configuration bytes from a declared source.