pub struct Template<'a> { /* private fields */ }
Expand description
Templates allows for string replacement by name
use markings::{Template, Args, Opts};
// parse a template using the default options
// the template is clonable so you don't have to reparse it
let template = Template::parse("hello, ${world}${end}", Opts::default())
.unwrap();
// build re-usable args that act as the replacements for the keys in the template
let args = Args::new()
.with("world", &"world")
.with("end", &(0x21 as char));
// apply the args to the template, consuming the template
let template = template
.apply(&args)
.unwrap();
// you'll get a String out, hopefully, that has your new message
assert_eq!(template, "hello, world!");
See Template::apply
for applying arguments to this template.
See Opts
for a way to change the behavior of the parser
Implementations§
Source§impl<'a> Template<'a>
impl<'a> Template<'a>
Sourcepub fn parse(input: &'a str, opts: Opts) -> Result<Self, Error>
pub fn parse(input: &'a str, opts: Opts) -> Result<Self, Error>
Parses a new template from a string
The syntax is extremely basic: just ${key}
The key gets replaced by a value matching it during the Template::apply
call
Sourcepub fn apply<'k>(self, args: &Args<'k>) -> Result<String, Error>
pub fn apply<'k>(self, args: &Args<'k>) -> Result<String, Error>
Apply the arguments to the template
One can use the Args
builder to make this less tedious
Sourcepub fn find_keys(input: &str) -> Result<Vec<&str>, Error>
pub fn find_keys(input: &str) -> Result<Vec<&str>, Error>
Find all the keys in the input string, returning them in a Vec
This is exposed as a convenient function for doing pre-parsing.
This returns an error if there are:
- nested templates
- mismatched braces
let keys = Template::find_keys("${this} is a ${test} ${with some keys}").unwrap();
assert_eq!(keys, vec!["this", "test", "with some keys"]);
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Template<'a>
impl<'a> RefUnwindSafe for Template<'a>
impl<'a> Send for Template<'a>
impl<'a> Sync for Template<'a>
impl<'a> Unpin for Template<'a>
impl<'a> UnwindSafe for Template<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more