Rendering my first template with actually following paths.
Before I was hard-coding the path to the reference. Now I am following the full path programmatically using the new ContextElement and Walkable traits.
This commit is contained in:
parent
e5e1703fab
commit
273f6204d8
@ -56,7 +56,7 @@ fn template_from_file<'a>(file_path: &str, file_contents: &'a str) -> CompiledTe
|
|||||||
.expect("Failed to compile template")
|
.expect("Failed to compile template")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_context_from_stdin() -> serde_json::map::Map<String, serde_json::Value> {
|
fn read_context_from_stdin() -> serde_json::Value {
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
io::stdin()
|
io::stdin()
|
||||||
.read_to_string(&mut buffer)
|
.read_to_string(&mut buffer)
|
||||||
@ -64,7 +64,7 @@ fn read_context_from_stdin() -> serde_json::map::Map<String, serde_json::Value>
|
|||||||
|
|
||||||
let parsed: serde_json::Value = serde_json::from_str(&buffer).expect("Failed to parse json");
|
let parsed: serde_json::Value = serde_json::from_str(&buffer).expect("Failed to parse json");
|
||||||
match parsed {
|
match parsed {
|
||||||
serde_json::Value::Object(obj) => obj,
|
serde_json::Value::Object(obj) => serde_json::value::Value::Object(obj),
|
||||||
_ => panic!("Expected context to be an object"),
|
_ => panic!("Expected context to be an object"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,13 +57,13 @@ pub struct Comment<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
struct Path<'a> {
|
pub struct Path<'a> {
|
||||||
keys: Vec<&'a str>,
|
pub keys: Vec<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Reference<'a> {
|
pub struct Reference<'a> {
|
||||||
path: Path<'a>,
|
pub path: Path<'a>,
|
||||||
filters: Vec<Filter>,
|
filters: Vec<Filter>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,8 +48,7 @@ impl<'a> DustRenderer<'a> {
|
|||||||
|
|
||||||
pub fn render<C>(&self, name: &str, context: &C) -> Result<String, RenderError>
|
pub fn render<C>(&self, name: &str, context: &C) -> Result<String, RenderError>
|
||||||
where
|
where
|
||||||
C: Index<&'a str>,
|
C: ContextElement,
|
||||||
<C as std::ops::Index<&'a str>>::Output: Renderable,
|
|
||||||
{
|
{
|
||||||
let main_template = match self.templates.get(name) {
|
let main_template = match self.templates.get(name) {
|
||||||
Some(tmpl) => tmpl,
|
Some(tmpl) => tmpl,
|
||||||
@ -64,8 +63,7 @@ impl<'a> DustRenderer<'a> {
|
|||||||
|
|
||||||
fn render_template<C>(&self, template: &Template, context: &C) -> Result<String, RenderError>
|
fn render_template<C>(&self, template: &Template, context: &C) -> Result<String, RenderError>
|
||||||
where
|
where
|
||||||
C: Index<&'a str>,
|
C: ContextElement,
|
||||||
<C as std::ops::Index<&'a str>>::Output: Renderable,
|
|
||||||
{
|
{
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
for elem in &template.contents.elements {
|
for elem in &template.contents.elements {
|
||||||
@ -81,13 +79,12 @@ impl<'a> DustRenderer<'a> {
|
|||||||
|
|
||||||
fn render_tag<C>(&self, tag: &DustTag, context: &C) -> Result<String, RenderError>
|
fn render_tag<C>(&self, tag: &DustTag, context: &C) -> Result<String, RenderError>
|
||||||
where
|
where
|
||||||
C: Index<&'a str>,
|
C: ContextElement,
|
||||||
<C as std::ops::Index<&'a str>>::Output: Renderable,
|
|
||||||
{
|
{
|
||||||
match tag {
|
match tag {
|
||||||
DustTag::DTComment(comment) => (),
|
DustTag::DTComment(comment) => (),
|
||||||
DustTag::DTReference(reference) => {
|
DustTag::DTReference(reference) => {
|
||||||
let val = context.index("name");
|
let val = walk_path(context, &reference.path.keys);
|
||||||
return Ok(val.render());
|
return Ok(val.render());
|
||||||
}
|
}
|
||||||
_ => (), // TODO: Implement the rest
|
_ => (), // TODO: Implement the rest
|
||||||
|
Loading…
x
Reference in New Issue
Block a user