5 releases

Uses new Rust 2021

0.2.1 Mar 26, 2022
0.2.0 Feb 26, 2022
0.2.0-beta.1 Jan 23, 2022
0.1.0 Jan 28, 2020

#43 in Template engine

Download history 356/week @ 2022-08-07 193/week @ 2022-08-14 163/week @ 2022-08-21 159/week @ 2022-08-28 314/week @ 2022-09-04 295/week @ 2022-09-11 241/week @ 2022-09-18 200/week @ 2022-09-25 90/week @ 2022-10-02 108/week @ 2022-10-09 113/week @ 2022-10-16 126/week @ 2022-10-23 79/week @ 2022-10-30 134/week @ 2022-11-06 189/week @ 2022-11-13 127/week @ 2022-11-20

532 downloads per month

MIT/Apache

31KB
666 lines

FAR

Find And Replace string template engine

version license downloads


Overview

Provided with a template and a map, FAR will attempt to find all the keys (delimited with {{ and }}) in the template and replace them with the corresponding value in the map. By default, map values are rendered by their Display impl. For example:

# use far::{find, Render, Errors};
# fn main() -> Result<(), Errors> {
let template = "{{specific}} are my favorite {{category}}.";

#[derive(Render)]
struct Replacements {
    specific: String,
    category: String,
}

let found = find(template)?;

let replacements = Replacements {
    specific: "Cats".to_owned(),
    category: "animal".to_owned(),
};

let s = found.replace(&replacements);

assert_eq!(s, "Cats are my favorite animal.");
# Ok(())
# }

If it fails for some reason, an explanation of why will be returned:

# use far::{find, Render};
// Note the typo ----------------------------> vvvvvvvv
let template = "{{specific}} are my favorite {{catglory}}.";

#[derive(Debug, Render)]
struct Replacements {
    specific: String,
    category: String,
}

let errors = find::<_, Replacements>(template).unwrap_err();

assert_eq!(
    format!("{}", errors),
    r#"missing key "category", extraneous key "catglory""#
);

Template authors can write {{{{}} or {{}}}} to get a literal {{ or }} respectively.

Additional examples and weird edge-case behaviors can be found in core/src/tests.

License

This project is licensed under either of

at your option.

Dependencies

~1–1.6MB
~32K SLoC