tanzim-source
Package | Documentation | Repository
Declarative configuration source: where to load from, loader options, and resource (address).
Format
SOURCE [(OPTIONS)] [?] [:RESOURCE]
| Part | Meaning |
|---|---|
SOURCE |
Loader kind (opaque string, e.g. env, file, http, custom) |
(OPTIONS) |
Optional loader options as key=value pairs |
? |
Ignore errors when loading this source |
:RESOURCE |
Optional address (path, URL, …); may be empty |
Source examples
env
env(prefix=APP_)
file:/etc/app/config.json
file?:.env
http(headers=(Authorization="TOKEN"),timeout=3s)?:https://example.com/config.yml
custom(k=v,list=[1,2,3.14,""],inner-kv=(foo=bar,baz=qux)):oops
Parsing
use Source;
let env = parse?;
let file: Source = "file:/etc/app/config.json".parse?;
let http = try_from?;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
assert_eq!;
# Ok::
Format Rules
SOURCEand option keys — ASCII letters, digits,-,_,.(non-empty).- No whitespace anywhere except inside
"quoted"strings. - Option values — try, in order: boolean, integer, float, list, map; otherwise unquoted string.
- Booleans:
true/false(case-insensitive). - Integers: base-10, optional leading
-(no+). - Floats: digits,
., optional leading-(e.g.3.14; not.5). - Lists:
[value,value,…] - Maps:
(key=value,…)(same value grammar). - Unquoted strings: letters, digits,
-,_,.only (e.g.APP_,3s). - Quoted strings:
"…"with escapes\",\\,\n,\r,\t.
- Booleans:
- Empty value — error; use
"". - Trailing commas — error (
(a=1,)/[1,]). - Duplicate keys in
(OPTIONS)— last wins. ?(ignore errors) — must come afterSOURCEor after the closing)of(OPTIONS); never before(OPTIONS).
Build programmatically with SourceBuilder:
use SourceBuilder;
let source = new
.with_source
.with_option
.with_ignore_errors
.build?;
# Ok::
Try sources from the command line:
cargo run -p tanzim-source --example parse_sources -- \
'env(prefix=APP_)' \
'file:/etc/app/config.json'
Ergonomic Errors
Use {error:#} for multi-line errors with caret:
use Source;
let error = parse.unwrap_err;
println!
invalid configuration source at column 4: configuration source: skip marker `?` must come after options `(...)`; use `source(...)?` not `source?(...)`
bad?(k=v)
^
Use {error} for one-line errors.
Cargo features
| Feature | Enables |
|---|---|
serde |
Serialize / Deserialize for Source as its canonical string |
Relations
- Used by all other tanzim crates to represent where and how to load from.
tanzim-loadconsumesSourcefields (source,options,resource) in its loaders.- Full pipeline wired in
tanzim.