3 releases

0.2.1 Mar 14, 2023
0.2.0 Feb 26, 2022
0.2.0-beta.0 Jan 23, 2022

#117 in #templating

Download history 12/week @ 2023-12-21 10/week @ 2023-12-28 35/week @ 2024-01-04 36/week @ 2024-01-11 5/week @ 2024-01-18 31/week @ 2024-01-25 13/week @ 2024-02-01 19/week @ 2024-02-08 69/week @ 2024-02-15 84/week @ 2024-02-22 43/week @ 2024-02-29 75/week @ 2024-03-07 68/week @ 2024-03-14 71/week @ 2024-03-21 88/week @ 2024-03-28 44/week @ 2024-04-04

286 downloads per month
Used in 2 crates (via far)

MIT/Apache

12KB
173 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.


lib.rs:

Procedural macros for the far crate

You should probably just be looking at the documentation for the far crate instead.

Dependencies

~3.5MB
~72K SLoC