[][src]Struct markings::Template

pub struct Template<'a> { /* fields omitted */ }

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

impl<'a> Clone for Template<'a>[src]

impl<'a> Debug for Template<'a>[src]

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.