Merge branch 'trait_wrapper' into render
This commit is contained in:
commit
82fb4964ee
@ -1,8 +1,9 @@
|
|||||||
use crate::parser::Filter;
|
use crate::parser::Filter;
|
||||||
use crate::renderer::errors::RenderError;
|
use crate::renderer::errors::RenderError;
|
||||||
|
use crate::renderer::renderer::RenderWrapper;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
pub trait ContextElement: Debug + Walkable + Renderable + Loopable {}
|
pub trait ContextElement: Debug + RenderWrapper + Walkable + Renderable + Loopable {}
|
||||||
|
|
||||||
pub trait Walkable {
|
pub trait Walkable {
|
||||||
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError>;
|
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError>;
|
||||||
|
@ -19,6 +19,24 @@ pub struct DustRenderer<'a> {
|
|||||||
templates: HashMap<String, &'a Template<'a>>,
|
templates: HashMap<String, &'a Template<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait RenderWrapper {
|
||||||
|
fn render_body<'a>(
|
||||||
|
&'a self,
|
||||||
|
renderer: &'a DustRenderer,
|
||||||
|
body: &Body,
|
||||||
|
) -> Result<String, RenderError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C: ContextElement> RenderWrapper for C {
|
||||||
|
fn render_body<'a>(
|
||||||
|
&'a self,
|
||||||
|
renderer: &'a DustRenderer,
|
||||||
|
body: &Body,
|
||||||
|
) -> Result<String, RenderError<'a>> {
|
||||||
|
renderer.render_body(body, self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn compile_template<'a>(
|
pub fn compile_template<'a>(
|
||||||
source: &'a str,
|
source: &'a str,
|
||||||
name: String,
|
name: String,
|
||||||
@ -44,7 +62,7 @@ impl<'a> DustRenderer<'a> {
|
|||||||
.insert(template.name.clone(), &template.template);
|
.insert(template.name.clone(), &template.template);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render<C>(&self, name: &str, context: &'a C) -> Result<String, RenderError<'a>>
|
pub fn render<C>(&'a self, name: &str, context: &'a C) -> Result<String, RenderError<'a>>
|
||||||
where
|
where
|
||||||
C: ContextElement,
|
C: ContextElement,
|
||||||
{
|
{
|
||||||
@ -60,7 +78,7 @@ impl<'a> DustRenderer<'a> {
|
|||||||
self.render_body(&main_template.contents, context)
|
self.render_body(&main_template.contents, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_body<C>(&self, body: &Body, context: &'a C) -> Result<String, RenderError<'a>>
|
fn render_body<C>(&'a self, body: &Body, context: &'a C) -> Result<String, RenderError<'a>>
|
||||||
where
|
where
|
||||||
C: ContextElement,
|
C: ContextElement,
|
||||||
{
|
{
|
||||||
@ -76,7 +94,7 @@ impl<'a> DustRenderer<'a> {
|
|||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_tag<C>(&self, tag: &DustTag, context: &'a C) -> Result<String, RenderError<'a>>
|
fn render_tag<C>(&'a self, tag: &DustTag, context: &'a C) -> Result<String, RenderError<'a>>
|
||||||
where
|
where
|
||||||
C: ContextElement,
|
C: ContextElement,
|
||||||
{
|
{
|
||||||
@ -111,19 +129,18 @@ impl<'a> DustRenderer<'a> {
|
|||||||
None => Ok("".to_owned()),
|
None => Ok("".to_owned()),
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
match container.contents {
|
match &container.contents {
|
||||||
None => Ok("".to_owned()),
|
None => return Ok("".to_owned()),
|
||||||
Some(body) => {
|
Some(body) => {
|
||||||
let rendered_results: Result<Vec<String>, RenderError> =
|
let rendered_results: Result<Vec<String>, RenderError> =
|
||||||
loop_elements
|
loop_elements
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|array_elem| self.render_body(&body, array_elem))
|
.map(|array_elem| array_elem.render_body(self, &body))
|
||||||
.collect();
|
.collect();
|
||||||
let rendered_slice: &[String] = &rendered_results?;
|
let rendered_slice: &[String] = &rendered_results?;
|
||||||
return Ok(rendered_slice.join(""));
|
return Ok(rendered_slice.join(""));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return Ok("".to_owned());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user