new_string_template/lib.rs
1/*!
2Simple Customizable String-Templating Library for Rust.
3
4This Library is inspired by [`string_template`](https://github.com/michaelr524/string_template)
5
6# Usage
7
8Add this to your `Cargo.toml` (or use `cargo-add`):
9
10```toml
11[dependencies]
12new_string_template = "1.5"
13```
14
15Example with 2 data points (with fail enabled):
16
17```rust
18use new_string_template::template::Template;
19use std::collections::HashMap;
20
21let templ_str = "Something {data1} be {data2}, and { not here }";
22let templ = Template::new(templ_str);
23let data = {
24 let mut map = HashMap::new();
25 map.insert("data1", "should");
26 map.insert("data2", "here");
27 map
28};
29
30let rendered = templ.render(&data).expect("Expected Result to be Ok");
31assert_eq!("Something should be here, and { not here }", rendered);
32```
33
34Example with 1 data point (with fail disabled):
35
36```rust
37use new_string_template::template::Template;
38use std::collections::HashMap;
39
40let templ_str = "Something {data1} be {data2}, and { not here }";
41let templ = Template::new(templ_str);
42let data = {
43 let mut map = HashMap::new();
44 map.insert("data1", "should");
45 // map.insert("data2", "here");
46 map
47};
48
49let rendered = templ.render_nofail(&data);
50assert_eq!("Something should be {data2}, and { not here }", rendered);
51```
52
53Example with Custom Regex:
54
55```rust
56use new_string_template::template::Template;
57use std::collections::HashMap;
58use regex::Regex;
59
60// The following regex requires at least one space between "{{" and "}}" and allows variables with spaces
61let custom_regex = Regex::new(r"(?mi)\{\{\s+([^\}]+)\s+\}\}").unwrap();
62let templ_str = "Something {{ data1 }} be {{ data2 }}, and {{ data 3 }}";
63let templ = Template::new(templ_str).with_regex(&custom_regex);
64let data = {
65 let mut map = HashMap::new();
66 map.insert("data1", "should");
67 map.insert("data2", "here");
68 map.insert("data 3", "here too");
69 map
70};
71
72let rendered = templ.render_nofail(&data);
73assert_eq!("Something should be here, and here too", rendered);
74```
75
76Note: with the default regex, a template-variable can have spaces or none at all.
77*/
78
79pub mod error;
80pub mod template;