Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
String template library that send queries to function responder and interpolate the responses
License
KSXGitHub/lazy-template
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Imagine that you need to interpolate strings, but the template variables are not quite cheap to load (such as system resources, network requests, etc). And since the templates are user-provided, you can't know ahead of time which variables you would need to load. You would need a flexible string template library that doesn't require calculating all these variables upfront.
Instead of requiring a complete set of inputs (such as via astruct, aHashMap, 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.
While this library provides a preset parser for a preset template syntax (calledcurly_braces) that works out-of-the-box, it is also flexible enough to allow advanced users to provide their own component parsers or even an entirely new template parser.
Go todocs.rs.
This is the most basic usage. There are more in thedocumentation.
let system = lazy_template::simple_curly_braces();let template = system.lazy_parse("{name} is a {age} years old {descriptor}");let output = template.to_string(|query|match query{"name" =>Ok("Alice"),"age" =>Ok("20"),"descriptor" =>Ok("girl"), _ =>Err(format!("Can't answer {query}")),}).unwrap();assert_eq!(output,"Alice is a 20 years old girl");
Thequery in the example above is merely a variable name, but not necessarily. In more advanced use cases,query could be an expression, a command, a network request, or request for some system resource that would take time to load.
About
String template library that send queries to function responder and interpolate the responses
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.