gtmpl_value_ng
The value type used by gtmpl-ng, the Golang templating language for Rust. A Value
is what a template parses its literals into and what a context hands it back.
[]
= "1"
Builds on Rust 1.85 and newer, with either feature below enabled or both, which CI checks on that exact toolchain.
use Value;
let v: Value = "something".into;
assert_eq!;
gtmpl_derive_ng derives the From implementation for a struct.
Relation to gtmpl_value
This is a fork of gtmpl_value by Florian Dieminger, taken from its master branch at
a333139 — two years ahead of the last crates.io release and carrying the fix for
negative non-integer numbers that 0.5.1 truncates.
Number::float, Number::signed and Number::unsigned are new: they build a number
without the collapse that From<f64> applies, which turns a whole float into an
integer so {{ 3.0 }} renders 3 the way Go does. That collapse is lossy, and the
original crate had no way around it.
Migrating from gtmpl_value
Enable the gtmpl_value_compat feature and Value gains a TryFrom conversion from
gtmpl_value::Value, so a tree that still uses the original #[derive(Gtmpl)] can be
migrated one call site at a time:
= { = "1", = ["gtmpl_value_compat"] }
use Value;
let legacy: Value = Config .into;
let value = try_from.expect;
assert_eq!;
It is TryFrom rather than From because a gtmpl_value::Func names the other
crate's Value in its own signature and cannot be rewritten — register such a function
with the template instead of passing it through the context. And a negative
non-integer arrives already truncated: gtmpl_value destroyed the fraction when the
value was built, and the conversion carries that over rather than inventing one back.
Features
| feature | effect |
|---|---|
serde |
Serialize/Deserialize for Value and Number |
gtmpl_value_compat |
the TryFrom<gtmpl_value::Value> conversion above |
Two things a document cannot carry, so serde does not pretend to: Value::Object
writes as a map and reads back as Value::Map, because provenance is not part of the
data; and Value::NoValue writes as null and reads back as Value::Nil, because
absence is not a value. A Value::Function is refused outright rather than written as
something it is not.