[−][src]Struct markings::Template
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
Methods
impl<'a> Template<'a>
[src]
pub fn parse(input: &'a str, opts: Opts) -> Result<Self, Error>
[src]
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
pub fn is_empty(&self) -> bool
[src]
Was this template empty?
pub fn apply<'k>(self, args: &Args<'k>) -> Result<String, Error>
[src]
Apply the arguments to the template
One can use the Args
builder to make this less tedious
pub fn find_keys(input: &str) -> Result<Vec<&str>, Error>
[src]
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> 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
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,