Compiles again.

This commit is contained in:
Tom Alexander 2020-05-09 14:18:45 -04:00
parent d758a71fb4
commit f2f6404017
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 27 additions and 44 deletions

View File

@ -8,6 +8,7 @@ use renderer::DustRenderer;
use renderer::Loopable; use renderer::Loopable;
use renderer::RenderError; use renderer::RenderError;
use renderer::Renderable; use renderer::Renderable;
use renderer::WalkError;
use renderer::Walkable; use renderer::Walkable;
use std::env; use std::env;
use std::fs; use std::fs;
@ -91,36 +92,17 @@ impl Renderable for serde_json::Value {
} }
impl Walkable for serde_json::Value { impl Walkable for serde_json::Value {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> { fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> {
match self { match self {
serde_json::Value::Null => Err(RenderError::CantWalk { serde_json::Value::Null => Err(WalkError::CantWalk),
segment: segment.to_string(), serde_json::Value::Bool(_boolean) => Err(WalkError::CantWalk),
elem: self, serde_json::Value::Number(_num) => Err(WalkError::CantWalk),
}), serde_json::Value::String(_string) => Err(WalkError::CantWalk),
serde_json::Value::Bool(_boolean) => Err(RenderError::CantWalk { serde_json::Value::Array(_arr) => Err(WalkError::CantWalk),
segment: segment.to_string(), serde_json::Value::Object(obj) => obj
elem: self, .get(segment)
}),
serde_json::Value::Number(_num) => Err(RenderError::CantWalk {
segment: segment.to_string(),
elem: self,
}),
serde_json::Value::String(_string) => Err(RenderError::CantWalk {
segment: segment.to_string(),
elem: self,
}),
serde_json::Value::Array(_arr) => Err(RenderError::CantWalk {
segment: segment.to_string(),
elem: self,
}),
serde_json::Value::Object(obj) => {
obj.get(segment)
.map(|val| val as _) .map(|val| val as _)
.ok_or(RenderError::CantWalk { .ok_or(WalkError::CantWalk),
segment: segment.to_string(),
elem: self,
})
}
} }
} }
} }
@ -128,30 +110,30 @@ impl Walkable for serde_json::Value {
impl Loopable for serde_json::Value { impl Loopable for serde_json::Value {
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> { fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
match self { match self {
serde_json::Value::Null => Ok(Vec::new()), serde_json::Value::Null => Vec::new(),
serde_json::Value::Bool(boolean) => { serde_json::Value::Bool(boolean) => {
if *boolean { if *boolean {
Ok(vec![self]) vec![self]
} else { } else {
Ok(Vec::new()) Vec::new()
} }
} }
serde_json::Value::Number(_num) => Ok(vec![self]), serde_json::Value::Number(_num) => vec![self],
serde_json::Value::String(string_value) => { serde_json::Value::String(string_value) => {
if string_value.is_empty() { if string_value.is_empty() {
Ok(Vec::new()) Vec::new()
} else { } else {
Ok(vec![self]) vec![self]
} }
} }
serde_json::Value::Array(array_value) => { serde_json::Value::Array(array_value) => {
if array_value.is_empty() { if array_value.is_empty() {
Ok(Vec::new()) Vec::new()
} else { } else {
Ok(array_value.iter().map(|x| x as _).collect()) array_value.iter().map(|x| x as _).collect()
} }
} }
serde_json::Value::Object(_obj) => Ok(vec![self]), serde_json::Value::Object(_obj) => vec![self],
} }
} }
} }

View File

@ -39,8 +39,9 @@ impl<'a> Renderable for ParametersContext<'a> {
} }
impl<'a> Loopable for ParametersContext<'a> { impl<'a> Loopable for ParametersContext<'a> {
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> { fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
Ok(vec![self]) // TODO: Would this even ever be called? Won't matter, but I'd like to know.
vec![self]
} }
} }

View File

@ -104,7 +104,7 @@ impl<'a> DustRenderer<'a> {
match val { match val {
Err(WalkError::CantWalk) => return Ok("".to_owned()), Err(WalkError::CantWalk) => return Ok("".to_owned()),
Ok(final_val) => { Ok(final_val) => {
let loop_elements = final_val.get_loop_elements()?; let loop_elements = final_val.get_loop_elements();
if loop_elements.is_empty() { if loop_elements.is_empty() {
return Ok("".to_owned()); return Ok("".to_owned());
} else { } else {
@ -115,7 +115,7 @@ impl<'a> DustRenderer<'a> {
} }
DustTag::DTSection(container) => { DustTag::DTSection(container) => {
let val = walk_path(breadcrumbs, &container.path.keys); let val = walk_path(breadcrumbs, &container.path.keys);
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?; let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val);
if loop_elements.is_empty() { if loop_elements.is_empty() {
// Oddly enough if the value is falsey (like // Oddly enough if the value is falsey (like
// an empty array or null), Dust uses the // an empty array or null), Dust uses the
@ -145,7 +145,7 @@ impl<'a> DustRenderer<'a> {
} }
DustTag::DTExists(container) => { DustTag::DTExists(container) => {
let val = walk_path(breadcrumbs, &container.path.keys); let val = walk_path(breadcrumbs, &container.path.keys);
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?; let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val);
if loop_elements.is_empty() { if loop_elements.is_empty() {
return match &container.else_contents { return match &container.else_contents {
Some(body) => self.render_body(&body, breadcrumbs), Some(body) => self.render_body(&body, breadcrumbs),
@ -160,7 +160,7 @@ impl<'a> DustRenderer<'a> {
} }
DustTag::DTNotExists(container) => { DustTag::DTNotExists(container) => {
let val = walk_path(breadcrumbs, &container.path.keys); let val = walk_path(breadcrumbs, &container.path.keys);
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?; let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val);
if !loop_elements.is_empty() { if !loop_elements.is_empty() {
return match &container.else_contents { return match &container.else_contents {
Some(body) => self.render_body(&body, breadcrumbs), Some(body) => self.render_body(&body, breadcrumbs),