gtmpl_value_ng/lib.rs
1//! `gtmpl_value_ng` holds the value type used by [`gtmpl-ng`][gtmpl-ng], the Golang
2//! templating language for Rust. A [`Value`] is what a template parses its literals
3//! into and what a context hands it back.
4//!
5//! The [`From`] trait is implemented for:
6//!
7//! * `String`, `&str`, `Cow<str>`
8//! * most numeric types `u64, u32, …, i64, i32, …, f64, f32`
9//! * `bool`
10//! * `Vec<Value>`, `&[Value]`
11//! * `HashMap<String, Value>`
12//! * `Option<T>`, where `None` becomes [`Value::NoValue`]
13//!
14//! [`gtmpl_derive_ng`][gtmpl_derive_ng] derives it for a struct.
15//!
16//! # Examples
17//!
18//! ```rust
19//! use gtmpl_value_ng::Value;
20//!
21//! let v: Value = "something".into();
22//! assert_eq!(v.to_string(), "something");
23//! ```
24//!
25//! # Relation to `gtmpl_value`
26//!
27//! This crate is a fork of [`gtmpl_value`][gtmpl_value] by Florian Dieminger, taken
28//! from its `master` branch at `a333139` — which is two years ahead of the last
29//! release on crates.io and carries the fix for negative non-integer numbers that
30//! `0.5.1` truncates. It lives in the same workspace as the engine so that the value
31//! type and the engine are released together.
32//!
33//! With the `gtmpl_value_compat` feature, [`Value`] gains a [`TryFrom`] conversion from
34//! `gtmpl_value::Value`, so a tree that still derives against the published crate can
35//! be migrated one call site at a time.
36//!
37//! [gtmpl-ng]: https://github.com/firstdorsal/gtmpl-rust
38//! [gtmpl_derive_ng]: https://docs.rs/gtmpl_derive_ng
39//! [gtmpl_value]: https://github.com/fiji-flo/gtmpl_value
40
41#[cfg(feature = "gtmpl_value_compat")]
42mod compat;
43mod from;
44mod go_fmt;
45mod number;
46mod value;
47
48#[cfg(feature = "gtmpl_value_compat")]
49pub use crate::compat::FromLegacyValueError;
50pub use crate::from::*;
51pub use crate::go_fmt::{format_float, write_float, FloatFormat};
52pub use crate::value::*;
53
54#[cfg(test)]
55mod test {
56 use super::*;
57
58 #[test]
59 fn test_function_cmp() {
60 fn f(a: &[Value]) -> Result<Value, FuncError> {
61 Ok(a[0].clone())
62 }
63 let f1 = Function { f };
64 let f2 = Function { f };
65 assert_eq!(f1, f2);
66 }
67}
68
69/// Compiles the README's examples as doc tests, so a documented example cannot stop
70/// building without the suite noticing. Nothing is rendered into the real docs.
71/// Gated on `gtmpl_value_compat` because some of the examples use it; CI runs with every
72/// feature on, so they are all checked there.
73#[cfg(all(doctest, feature = "gtmpl_value_compat"))]
74#[doc = include_str!("../README.md")]
75pub struct ReadmeExamples;