pub struct TemplateSystem<Parser, Query> { /* private fields */ }
Implementations§
Source§impl<Parser, Query> TemplateSystem<Parser, Query>
impl<Parser, Query> TemplateSystem<Parser, Query>
Source§impl<'a, Parser, Query> TemplateSystem<Parser, Query>where
Parser: Parse<'a>,
impl<'a, Parser, Query> TemplateSystem<Parser, Query>where
Parser: Parse<'a>,
Sourcepub fn lazy_parse(
&'a self,
text: &'a str,
) -> Template<LazyParseIter<'a, Parser>, Query>
pub fn lazy_parse( &'a self, text: &'a str, ) -> Template<LazyParseIter<'a, Parser>, Query>
Create a Template
from a template string.
use lazy_template::{Template, simple_curly_braces};
let system = simple_curly_braces();
let template: Template<_, _> = system.lazy_parse("{name} is a {age} years old {gender}");
let output = template
.to_string(|query| match query {
"name" => Ok("Alice"),
"age" => Ok("20"),
"gender" => Ok("girl"),
_ => Err(format!("Can't answer {query:?}")),
})
.unwrap();
assert_eq!(output, "Alice is a 20 years old girl");
Template
only parses each segment just before it is needed, meaning that even a template with syntax error
can produce a partial output:
let template_string = "{name} is a {age} years } old {gender})"; // incorrectly placed closing curly bracket
let mut output = String::new();
let error = lazy_template::simple_curly_braces()
.lazy_parse(template_string)
.write_to(&mut output, |query| match query {
"name" => Ok("Alice"),
"age" => Ok("20"),
"gender" => Ok("girl"),
_ => Err(format!("Can't answer {query:?}")),
})
.unwrap_err();
assert_eq!(output, "Alice is a 20 years "); // output is partially written
Sourcepub fn eager_parse<SegmentContainer>(
&'a self,
text: &'a str,
) -> Result<ParsedTemplate<SegmentContainer, Query>, Parser::Error>where
SegmentContainer: FromIterator<Parser::Output>,
pub fn eager_parse<SegmentContainer>(
&'a self,
text: &'a str,
) -> Result<ParsedTemplate<SegmentContainer, Query>, Parser::Error>where
SegmentContainer: FromIterator<Parser::Output>,
Parse the template string ahead of time.
The returned parsed template can be used multiple times with different responders to generate different outputs:
let system = lazy_template::simple_curly_braces();
let parsed_template = system.eager_parse::<Vec<_>>("Hello, {name}!").unwrap();
let output = parsed_template
.to_template()
.to_string(|query| (query == "name").then_some("Alice").ok_or("Invalid query"))
.unwrap();
assert_eq!(output, "Hello, Alice!");
let output = parsed_template
.to_template()
.to_string(|query| (query == "name").then_some("Bob").ok_or("Invalid query"))
.unwrap();
assert_eq!(output, "Hello, Bob!");
Unlike lazy_parse
, this function would fail if the template fails to parse (e.g. syntax error):
let template_string = "Hello, {name!"; // missing a closing curly bracket
let error = lazy_template::simple_curly_braces()
.eager_parse::<Vec<_>>(template_string)
.unwrap_err();
Trait Implementations§
Source§impl<Parser: Clone, Query: Clone> Clone for TemplateSystem<Parser, Query>
impl<Parser: Clone, Query: Clone> Clone for TemplateSystem<Parser, Query>
Source§fn clone(&self) -> TemplateSystem<Parser, Query>
fn clone(&self) -> TemplateSystem<Parser, Query>
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreimpl<Parser: Copy, Query: Copy> Copy for TemplateSystem<Parser, Query>
Auto Trait Implementations§
impl<Parser, Query> Freeze for TemplateSystem<Parser, Query>where
Parser: Freeze,
impl<Parser, Query> RefUnwindSafe for TemplateSystem<Parser, Query>where
Parser: RefUnwindSafe,
Query: RefUnwindSafe,
impl<Parser, Query> Send for TemplateSystem<Parser, Query>
impl<Parser, Query> Sync for TemplateSystem<Parser, Query>
impl<Parser, Query> Unpin for TemplateSystem<Parser, Query>
impl<Parser, Query> UnwindSafe for TemplateSystem<Parser, Query>where
Parser: UnwindSafe,
Query: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<X> Pipe for X
impl<X> Pipe for X
Source§fn pipe_ref<'a, Return, Function>(&'a self, f: Function) -> Returnwhere
Function: FnOnce(&'a Self) -> Return,
fn pipe_ref<'a, Return, Function>(&'a self, f: Function) -> Returnwhere
Function: FnOnce(&'a Self) -> Return,
Source§fn pipe_mut<'a, Return, Function>(&'a mut self, f: Function) -> Returnwhere
Function: FnOnce(&'a mut Self) -> Return,
fn pipe_mut<'a, Return, Function>(&'a mut self, f: Function) -> Returnwhere
Function: FnOnce(&'a mut Self) -> Return,
Source§fn pipe_as_ref<'a, Param, Return, Function>(&'a self, f: Function) -> Return
fn pipe_as_ref<'a, Param, Return, Function>(&'a self, f: Function) -> Return
Apply
f
to &self
where f
takes a single parameter of type Param
and Self
implements trait AsRef<Param>
. Read moreSource§fn pipe_as_mut<'a, Param, Return, Function>(&'a mut self, f: Function) -> Return
fn pipe_as_mut<'a, Param, Return, Function>(&'a mut self, f: Function) -> Return
Apply
f
to &mut self
where f
takes a single parameter of type Param
and Self
implements trait AsMut<Param>
. Read moreSource§fn pipe_deref<'a, Param, Return, Function>(&'a self, f: Function) -> Return
fn pipe_deref<'a, Param, Return, Function>(&'a self, f: Function) -> Return
Apply
f
to &self
where f
takes a single parameter of type Param
and Self
implements trait Deref<Target = Param>
. Read moreSource§fn pipe_deref_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function,
) -> Returnwhere
Self: DerefMut<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
fn pipe_deref_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function,
) -> Returnwhere
Self: DerefMut<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
Apply
f
to &mut self
where f
takes a single parameter of type Param
and Self
implements trait [DerefMut<Target = Param>
]. Read moreSource§fn pipe_borrow<'a, Param, Return, Function>(&'a self, f: Function) -> Return
fn pipe_borrow<'a, Param, Return, Function>(&'a self, f: Function) -> Return
Apply
f
to &self
where f
takes a single parameter of type Param
and Self
implements trait Borrow<Param>
. Read moreSource§fn pipe_borrow_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function,
) -> Return
fn pipe_borrow_mut<'a, Param, Return, Function>( &'a mut self, f: Function, ) -> Return
Apply
f
to &mut self
where f
takes a single parameter of type Param
and Self
implements trait BorrowMut<Param>
. Read more