Skip to main content

Crate tanzim_parse

Crate tanzim_parse 

Source
Expand description

§tanzim-parse

Package | Documentation | Repository

Second stage of the tanzim pipeline: parses raw bytes into typed, source-located value trees.

§The Parse trait

Implement Parse to add a new configuration format. It turns the raw bytes a loader produced into a typed, source-located value tree. The contract:

  • parse returns one LocatedValue tree per payload, given the source kind and resource identifier the bytes came from.
  • Every node — including the root — should carry a Location (source, resource, line/column) so downstream error messages can point at the exact value. Build them with Location::at.
  • A parser is selected by the payload’s format hint against supported_format_list (which may list several extensions, e.g. yml/yaml); with no hint, the stage probes each parser via is_format_supported (Some(true)/Some(false)/None to abstain).
  • Report failures with the matching Error variant (InvalidUtf8, Parse, UnsupportedNull, UnsupportedType).

Register a parser with tanzim::Config::with_parser. For a quick, stateless adapter, use closure::Closure instead of a full impl Parse. See the Parse rustdoc and the example below for worked details.

§Built-in parsers

TypeFeatureFormats
Envenvenv
Jsonjsonjson
Yamlyamlyml, yaml
Tomltomltoml
closure::Closurecustom

§Example

use tanzim_parse::{Parse, json::Json};
use tanzim_source::SourceBuilder;

fn main() -> Result<(), tanzim_value::Error> {
    let source = SourceBuilder::new()
        .with_source("file")
        .with_resource("config.json")
        .build()
        .unwrap();
    let value = Json::new().parse(&source, br#"{"port": 8080}"#)?;
    let map = value.value.as_map().unwrap();
    let port = map.get("port").unwrap();
    println!("port={port}  location={}", port.location);
    Ok(())
}

§Features

FeatureEnables
envenv format parser (KEY=VALUE lines)
jsonJSON parser with source spans
yamlYAML parser with line numbers
tomlTOML parser with source spans
loggingemit log messages via the log crate
tracingemit trace spans via the tracing crate
fullenv + json + yaml + toml

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

§Relations

Modules§

closure
Custom parser backed by a closure.
env
Dotenv / env-file parser (env feature).
json
JSON parser (json feature).
span
Shared span-to-Location conversion helpers used by format modules.
toml
TOML parser (toml feature).
yaml
YAML parser (yaml feature).

Structs§

LocatedValue
A Value with its Location and an optional Comment.
Source
One configuration source declaration.

Enums§

Error
Error while deserializing configuration input.
Value
Dynamically typed configuration value.

Traits§

Parse
Parses raw bytes into a LocatedValue tree for one format.