Expand description
§Description
This is a string template crate.
Instead of requiring a complete set of inputs (such as via a struct
, a HashMap
, or a JSON object) to be available,
the templates from this crate would send queries (which would usually be the names of the variables) to a function
(called “responder”) to get the value of each query.
§Usage Examples
Example 1: Lazily parse template
let system = lazy_template::simple_curly_braces();
let template = system.lazy_parse("{name} is a {age} years old {gender}");
let alice_info = template
.to_string(|query| match query {
"name" => Ok("Alice"),
"age" => Ok("20"),
"gender" => Ok("girl"),
_ => Err(format!("Can't answer {query}")),
})
.unwrap();
let bob_info = template
.to_string(|query| match query {
"name" => Ok("Bob"),
"age" => Ok("32"),
"gender" => Ok("man"),
_ => Err(format!("Can't answer {query}")),
})
.unwrap();
assert_eq!(alice_info, "Alice is a 20 years old girl");
assert_eq!(bob_info, "Bob is a 32 years old man");
see more: lazy_parse
Example 2: Eagerly parse template:
let system = lazy_template::simple_curly_braces();
let parsed_template = system
.eager_parse::<Vec<_>>("{name} is a {age} years old {gender}")
.unwrap();
let alice_info = parsed_template
.to_template()
.to_string(|query| match query {
"name" => Ok("Alice"),
"age" => Ok("20"),
"gender" => Ok("girl"),
_ => Err(format!("Can't answer {query}")),
})
.unwrap();
let bob_info = parsed_template
.to_template()
.to_string(|query| match query {
"name" => Ok("Bob"),
"age" => Ok("32"),
"gender" => Ok("man"),
_ => Err(format!("Can't answer {query}")),
})
.unwrap();
assert_eq!(alice_info, "Alice is a 20 years old girl");
assert_eq!(bob_info, "Bob is a 32 years old man");
see more: eager_parse
Re-exports§
pub use enclosed::EnclosedTemplateParser;
Modules§
Structs§
Enums§
Traits§
- Into
Skip OrFatal - Trait of error type of a component parser. It checks whether the error means “skip” or “fatal”.
- Into
Template System - Convert a parser into a
TemplateSystem
. - Parse
- Parse a segment.
- Render
- Represent the ability of a segment of a string template to render template output.
Functions§
- simple_
curly_ braces - Create a simple template of string interpolation with curly braces.