https://github.com/cdown/funcfmt
A simple templating crate that allows passing in custom data to functions through function traits.
https://github.com/cdown/funcfmt
closure closure-templates formatter formatting function-template rust rust-lang template template-engine
Last synced: about 1 month ago
JSON representation
A simple templating crate that allows passing in custom data to functions through function traits.
- Host: GitHub
- URL: https://github.com/cdown/funcfmt
- Owner: cdown
- License: mit
- Created: 2023-04-01T05:36:58.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-10-26T19:50:13.000Z (6 months ago)
- Last Synced: 2024-10-26T21:51:48.197Z (6 months ago)
- Topics: closure, closure-templates, formatter, formatting, function-template, rust, rust-lang, template, template-engine
- Language: Rust
- Homepage:
- Size: 60.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# funcfmt | [](https://github.com/cdown/funcfmt/actions?query=branch%3Amaster)
funcfmt is a simple, lightweight templating library that allows templating
using custom runtime context and function traits. It was originally created
for [exifrename](https://github.com/cdown/exifrename), to allow efficiently
processing a format and set of callbacks across thousands of EXIF objects.## Documentation
Documentation is available on [docs.rs](https://docs.rs/funcfmt), or through
`cargo doc --open`.## Usage
To add funcfmt to your dependencies:
```
cargo add funcfmt
```The basic flow of funcfmt looks like this:
1. Given a `FormatMap` called `formatters`, call
`formatters.to_format_pieces()`, which preprocesses everything into a
`FormatPieces`, where `&T` is what your callback function will take as
its only argument. This allows avoiding having to reparse the formatters and
go through the template each time things are processed.
2. Call .render(data) on the `FormatPieces`.A very small example with `String`s passed in, although you can pass an object
of any type:```rust
use funcfmt::{fm, FormatMap, Render, ToFormatPieces};fn main() {
let formatters = FormatMap::from([
fm!("foo", |data| Some(format!("foo: {data}"))),
fm!("bar", |data| Some(format!("bar: {data}"))),
fm!("baz", |data| Some(format!("baz: {data}"))),
]);let fp = formatters.to_format_pieces("{foo}, {bar}").unwrap();
// foo: some data, bar: some data
let data_one = String::from("some data");
println!("{}", fp.render(&data_one).unwrap());// foo: other data, bar: other data
// note that this doesn't require processing the format again
let data_two = String::from("other data");
println!("{}", fp.render(&data_two).unwrap());
}
```