Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rhettjel/rust-boilerplate
https://github.com/rhettjel/rust-boilerplate
Last synced: 2 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/rhettjel/rust-boilerplate
- Owner: rhettjel
- License: cc0-1.0
- Created: 2024-06-20T07:40:37.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2024-06-20T07:43:18.000Z (6 months ago)
- Last Synced: 2024-12-09T19:43:32.116Z (13 days ago)
- Language: Rust
- Size: 65.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING
- License: LICENSE
Awesome Lists containing this project
README
boilerplate
`boilerplate` is a statically-checked Rust template engine with no runtime
dependencies. There are two ways to use boilerplate,
`boilerplate::boilerplate`, a function-like macro, and
`boilerplate::Boilerplate`, a derive macro.Function-like Macro
-------------------```rust
use boilerplate::boilerplate;let foo = true;
let bar: Result<&str, &str> = Ok("yassss");let output = boilerplate!(
"%% if foo {
Foo was true!
%% }
%% match bar {
%% Ok(ok) => {
Pretty good: {{ ok }}
%% }
%% Err(err) => {
Not so great: {{ err }}
%% }
%% }
");assert_eq!(output, "Foo was true!\nPretty good: yassss\n");
```Derive Macro
------------Derive `Boilerplate` on the type you want to use as a template context:
```rust
use boilerplate::Boilerplate;#[derive(Boilerplate)]
struct MyTemplateTxt {
foo: bool,
bar: Result>,
}
````boilerplate` template code and interpolations are Rust, so errors are checked
at compile time and the template language is easy to learn:```text
%% if self.foo {
Foo was true!
%% }
%% match &self.bar {
%% Ok(ok) => {
Pretty good: {{ ok }}
%% }
%% Err(err) => {
Not so great: {{ err }}
%% }
%% }
```The `Boilerplate` macro provides a `Display` implementation, so you can
instantiate a template context and convert it to a string:```rust
let rendered = MyTemplateTxt { foo: true, bar: Ok("hello".into()) }.to_string();
```Or use it in a format string:
```rust
println!("The output is: {}", MyTemplateTxt { foo: false, bar: Err("hello".into()) });
````boilerplate`'s implementation is exceedingly simple. Try using
[cargo-expand](https://github.com/dtolnay/cargo-expand) to expand the
`Boilerplate` macro and inspect derived `Display` implementations and debug
template issues.Quick Start
-----------Add `boilerplate` to your project's `Cargo.toml`:
```toml
[dependencies]
boilerplate = "*"
```Create a template in `templates/my-template.txt`:
```text
Foo is {{self.n}}!
```Define, instantiate, and render the template context:
```rust
use boilerplate::Boilerplate;#[derive(Boilerplate)]
struct MyTemplateTxt {
n: u32,
}assert_eq!(MyTemplateTxt { n: 10 }.to_string(), "Foo is 10!\n");
```Examples
--------See [the docs](https://docs.rs/boilerplate/latest/boilerplate/) for more information and examples.