3 unstable releases

0.2.1 Jun 18, 2021
0.2.0 Jun 18, 2021
0.1.0 Jun 17, 2021

#2222 in Rust patterns

MIT/Apache

11KB
116 lines

beard

In opposition to mustache. Here the goal instead of mustache is to leverage as much as possible rust's type system to detect error case and therefor to make the rendering deterministic. If you are looking for something that is going to be portable outside of rust you should checkout mustache.

beard is a macro that will generate the necessary rust code to serialise the given template. You can achieve the same thing by writing the code yourself (calling std::io::Write appropriate methods). beard is simply an help to do that and to make it easier to maintain the templates.

How it works

string literals

Any string literal will be written as is in the output.

use beard::beard;
#
beard! {
output,
"List of items:\n"
"\n"
"* item 1\n"
"* item 2\n"
};

serialising any impl of Display

You can intersperse serialised object that implements std::fmt::Display, just use the curly bracket as you would if you were to use in std::format macro, except outside of the string.

Interestingly it can also do any kind of operations within the brackets so long it returns something that implements std::fmt::Display.

use beard::beard;
#
let value = 42;
beard! {
output,
{ value } "\n"
{ 1 + 2 } "\n"
};

serialising array of bytes

In case the value is already an array and there is no need to run std::fmt::Display's formatting to String as intermediate.

use beard::beard;
#
let value = b"some array";
beard! {
output,
[{ value }] "\n"
};

Calling function to serialize

Would you need to perform some operation with the std::io::Write object you can capture the variable by calling a lambda (it's not really one).

use beard::beard;
#
let value = b"some array";
beard! {
output,
|| {
// do other operations
output.write_all(b"some bytes")?;
}
};

if and if let statement

There are times where one needs to serialize or not some parts.

use beard::beard;
#
let value = false;
let optional = Some("something");
beard! {
output,
if (value) {
"Value is true\n"
} else {
"Value is false\n"
}

"Or do some pattern matching\n"
if let Some(value) = (optional) {
"We have " { value } "\n"
}
};

for loop, iterating on items

Shall you need to print the items of a list or anything that implements std::iter::IntoIterator.

use beard::beard;
#
let shopping_list = ["apple", "pasta", "tomatoes", "garlic", "mozzarella"];
beard! {
output,
"Shopping list\n"
"\n"
for (index, item) in (shopping_list.iter().enumerate()) {
{index} ". " { item } "\n"
}
};

Example

use beard::beard;
#
let name = "Arthur";
let list = ["Bubble Bath", "Unicorn Crunchy Oat"];

beard! {
output,
"Hi " { name } "\n"
"\n"
"Confirmation order about the following items:\n"
for item in ( list ) {
" * " { item } "\n"
}
"\n"
"Your order will be ship to you once everything is ready.\n"
};

The Example below will generate a string in the output:

Hi Arthur

Confirmation order about the following items:
* Bubble Bath
* Unicorn Crunch Oat

Your order will be ship to you once everything is ready.

No runtime deps