Expand description
Derive macros for miniconf trees.
Most users import these macros through miniconf and write #[derive(Tree)].
Tree is shorthand for TreeSchema, TreeSerialize, TreeDeserialize, and
TreeAny on the same item.
§Tree Shape
Fields and variants are internal nodes when their types implement the relevant
Tree* traits. Serde leaves are accessed directly. Use
#[tree(with = miniconf::leaf)] to force a Tree-capable type to stay one
leaf value.
use miniconf::{leaf, Tree};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Calibration {
offset: i32,
scale: u16,
}
#[derive(Tree)]
struct Settings {
#[tree(rename = "cal", with = leaf)]
calibration: Calibration,
}§Attributes
#[tree(...)] is accepted on containers, fields, and variants:
rename = identexposes a different Rust identifier as the path segment.skipremoves the field or variant from the tree.flattensplices one child tree into the parent when lookup is unambiguous.with = moduledelegates schema, serialization, deserialization, andAnyaccess to functions inmodule.meta(key = "value")attaches reflection metadata.meta(key)inherits supported metadata from Rust syntax:doc,typename, ornullable.
Container meta(doc) stores Rust doc comments as node metadata.
meta(typename) stores the Rust type name. Field and variant metadata is
edge metadata unless the item is flattened into the parent.
use miniconf::Tree;
/// Node documentation copied by `meta(doc)`.
#[derive(Tree)]
#[tree(meta(doc, typename))]
struct Settings {
#[tree(rename = "en")]
enabled: bool,
#[tree(skip)]
cache_only: u32,
}§Custom Access
with = module is the escape hatch for validation, read-only leaves, relaxed
bounds, or nonstandard access. The module exports the operations the derive
calls, such as schema::<T>(), serialize_by_key, deserialize_by_key,
probe_by_key, ref_any_by_key, and mut_any_by_key.
Custom deserialize bounds may refer to the generated deserialize lifetime as
'__de.
Prefer this for real access policy. Keep ordinary Serde leaves on the default
path or use with = miniconf::leaf.
§Limits
- Internal tree enums support unit, newtype, and skipped variants only.
- Enums with named fields or multi-field tuple variants should stay leaves or use a manual/custom implementation.
- Flattening is supported only when generated lookup stays unambiguous.
Derive Macros§
- Tree
- Derive the
TreeSchema,TreeSerialize,TreeDeserialize, andTreeAnytraits for a struct or enum. - TreeAny
- Derive the
TreeAnytrait for a struct or enum. - Tree
Deserialize - Derive the
TreeDeserializetrait for a struct or enum. - Tree
Schema - Derive the
TreeSchematrait for a struct or enum. - Tree
Serialize - Derive the
TreeSerializetrait for a struct or enum.