Merge branch 'exists' into render

This commit is contained in:
Tom Alexander 2020-05-06 20:33:02 -04:00
commit 467a810569
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
29 changed files with 77 additions and 8 deletions

View File

@ -0,0 +1 @@
Exists appears to follow the same truthiness rules that sections follow, rather than merely checking if a value exists.

View File

@ -0,0 +1 @@
{"things": ["Alice", "Bob", "Chris"]}

View File

@ -0,0 +1 @@
{"things": {}}

View File

@ -0,0 +1 @@
["cat", "dog"]

View File

@ -0,0 +1 @@
{"things": {"name": "Alice", "keyboard": "K-Type"}}

View File

@ -0,0 +1 @@
{"there_are_no_things": 4}

View File

@ -0,0 +1 @@
{"things": "just a string"}

View File

@ -0,0 +1 @@
{"things": false}

View File

@ -0,0 +1 @@
{"things": null}

View File

@ -0,0 +1 @@
{"things": 0}

View File

@ -0,0 +1 @@
{"things": ""}

View File

@ -0,0 +1 @@
{"things": []}

View File

@ -0,0 +1,5 @@
{?things}
Thing: {things}
{:else}
No things {.}
{/things}

View File

@ -0,0 +1 @@
{"things": ["Alice", "Bob", "Chris"]}

View File

@ -0,0 +1 @@
{"things": {}}

View File

@ -0,0 +1 @@
["cat", "dog"]

View File

@ -0,0 +1 @@
{"things": {"name": "Alice", "keyboard": "K-Type"}}

View File

@ -0,0 +1 @@
{"there_are_no_things": 4}

View File

@ -0,0 +1 @@
{"things": "just a string"}

View File

@ -0,0 +1 @@
{"things": false}

View File

@ -0,0 +1 @@
{"things": null}

View File

@ -0,0 +1 @@
{"things": 0}

View File

@ -0,0 +1 @@
{"things": ""}

View File

@ -0,0 +1 @@
{"things": []}

View File

@ -0,0 +1,5 @@
{^things}
Thing: {things}
{:else}
No things {.}
{/things}

View File

@ -0,0 +1 @@
{"name": false}

View File

@ -0,0 +1 @@
{"name": true}

View File

@ -0,0 +1 @@
{"name": [[], {}, true, false, ""]}

View File

@ -99,11 +99,17 @@ impl<'a> DustRenderer<'a> {
}
DustTag::DTReference(reference) => {
let val = walk_path(breadcrumbs, &reference.path.keys);
if let Err(RenderError::NotFound { .. }) = val {
// If reference does not exist in the context, it becomes an empty string
return Ok("".to_owned());
} else {
return val?.render(&reference.filters);
match val {
Err(RenderError::NotFound { .. }) => return Ok("".to_owned()),
Ok(final_val) => {
let loop_elements = final_val.get_loop_elements()?;
if loop_elements.is_empty() {
return Ok("".to_owned());
} else {
return final_val.render(&reference.filters);
}
}
Err(render_error) => return Err(render_error),
}
}
DustTag::DTSection(container) => {
@ -114,9 +120,6 @@ impl<'a> DustRenderer<'a> {
// an empty array or null), Dust uses the
// original context before walking the path as
// the context for rendering the else block
//
// TODO: do filters apply? I don't think so
// but I should test
return match &container.else_contents {
Some(body) => self.render_body(&body, breadcrumbs),
None => Ok("".to_owned()),
@ -139,6 +142,36 @@ impl<'a> DustRenderer<'a> {
}
}
}
DustTag::DTExists(container) => {
let val = walk_path(breadcrumbs, &container.path.keys);
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?;
if loop_elements.is_empty() {
return match &container.else_contents {
Some(body) => self.render_body(&body, breadcrumbs),
None => Ok("".to_owned()),
};
} else {
return match &container.contents {
None => Ok("".to_owned()),
Some(body) => self.render_body(&body, breadcrumbs),
};
}
}
DustTag::DTNotExists(container) => {
let val = walk_path(breadcrumbs, &container.path.keys);
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?;
if !loop_elements.is_empty() {
return match &container.else_contents {
Some(body) => self.render_body(&body, breadcrumbs),
None => Ok("".to_owned()),
};
} else {
return match &container.contents {
None => Ok("".to_owned()),
Some(body) => self.render_body(&body, breadcrumbs),
};
}
}
_ => (), // TODO: Implement the rest
}
Ok("".to_owned())