Crates.io | far |
lib.rs | far |
version | 0.2.1 |
source | src |
created_at | 2020-01-28 07:29:21.15414 |
updated_at | 2022-03-26 17:25:37.933312 |
description | Find And Replace string template engine |
homepage | https://or.computer.surgery/charles/far |
repository | https://or.computer.surgery/charles/far |
max_upload_size | |
id | 202689 |
size | 28,189 |
Find And Replace string template engine
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
.
This project is licensed under either of
at your option.