Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

String template library that send queries to function responder and interpolate the responses

License

NotificationsYou must be signed in to change notification settings

KSXGitHub/lazy-template

Repository files navigation

Description

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.

Documentation

Go todocs.rs.

Basic Usage

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.

License

MIT ©Hoàng Văn Khải

About

String template library that send queries to function responder and interpolate the responses

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp