lazy_template/lib.rs
1//! # Description
2//!
3//! This is a string template crate.
4//! Instead of requiring a complete set of inputs (such as via a `struct`, a `HashMap`, or a JSON object) to be available,
5//! the templates from this crate would send queries (which would usually be the names of the variables) to a function
6//! (called "responder") to get the value of each query.
7//!
8//! # Usage Examples
9//!
10//! **Example 1:** Lazily parse template
11//!
12//! ```
13//! # #[cfg(not(feature = "std"))] fn main() {}
14//! # #[cfg(feature = "std")] fn main() {
15//! # use pretty_assertions::assert_eq;
16//! let system = lazy_template::simple_curly_braces();
17//! let template = system.lazy_parse("{name} is a {age} years old {gender}");
18//! let alice_info = template
19//! .to_string(|query| match query {
20//! "name" => Ok("Alice"),
21//! "age" => Ok("20"),
22//! "gender" => Ok("girl"),
23//! _ => Err(format!("Can't answer {query}")),
24//! })
25//! .unwrap();
26//! let bob_info = template
27//! .to_string(|query| match query {
28//! "name" => Ok("Bob"),
29//! "age" => Ok("32"),
30//! "gender" => Ok("man"),
31//! _ => Err(format!("Can't answer {query}")),
32//! })
33//! .unwrap();
34//! assert_eq!(alice_info, "Alice is a 20 years old girl");
35//! assert_eq!(bob_info, "Bob is a 32 years old man");
36//! # }
37//! ```
38//!
39//! _see more:_ [`lazy_parse`](crate::TemplateSystem::lazy_parse)
40//!
41//! **Example 2:** Eagerly parse template:
42//!
43//! ```
44//! # #[cfg(not(feature = "std"))] fn main() {}
45//! # #[cfg(feature = "std")] fn main() {
46//! # use pretty_assertions::assert_eq;
47//! let system = lazy_template::simple_curly_braces();
48//! let parsed_template = system
49//! .eager_parse::<Vec<_>>("{name} is a {age} years old {gender}")
50//! .unwrap();
51//! let alice_info = parsed_template
52//! .to_template()
53//! .to_string(|query| match query {
54//! "name" => Ok("Alice"),
55//! "age" => Ok("20"),
56//! "gender" => Ok("girl"),
57//! _ => Err(format!("Can't answer {query}")),
58//! })
59//! .unwrap();
60//! let bob_info = parsed_template
61//! .to_template()
62//! .to_string(|query| match query {
63//! "name" => Ok("Bob"),
64//! "age" => Ok("32"),
65//! "gender" => Ok("man"),
66//! _ => Err(format!("Can't answer {query}")),
67//! })
68//! .unwrap();
69//! assert_eq!(alice_info, "Alice is a 20 years old girl");
70//! assert_eq!(bob_info, "Bob is a 32 years old man");
71//! # }
72//! ```
73//!
74//! _see more:_ [`eager_parse`](crate::TemplateSystem::eager_parse)
75//!
76#![cfg_attr(not(feature = "std"), no_std)]
77
78pub mod iter;
79
80pub mod enclosed;
81pub use enclosed::EnclosedTemplateParser;
82
83mod errors;
84mod parse;
85mod render;
86mod shorthands;
87mod system;
88mod template;
89
90pub use errors::*;
91pub use parse::*;
92pub use render::*;
93pub use shorthands::*;
94pub use system::*;
95pub use template::*;