From f2f640401777fb915c7c09ee430b665b42305a27 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 9 May 2020 14:18:45 -0400 Subject: [PATCH] Compiles again. --- src/bin.rs | 58 +++++++++++------------------- src/renderer/parameters_context.rs | 5 +-- src/renderer/renderer.rs | 8 ++--- 3 files changed, 27 insertions(+), 44 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index e02e3d9..12e1bd3 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -8,6 +8,7 @@ use renderer::DustRenderer; use renderer::Loopable; use renderer::RenderError; use renderer::Renderable; +use renderer::WalkError; use renderer::Walkable; use std::env; use std::fs; @@ -91,36 +92,17 @@ impl Renderable 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 { - serde_json::Value::Null => Err(RenderError::CantWalk { - segment: segment.to_string(), - elem: self, - }), - serde_json::Value::Bool(_boolean) => Err(RenderError::CantWalk { - segment: segment.to_string(), - elem: self, - }), - 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 _) - .ok_or(RenderError::CantWalk { - segment: segment.to_string(), - elem: self, - }) - } + serde_json::Value::Null => Err(WalkError::CantWalk), + serde_json::Value::Bool(_boolean) => Err(WalkError::CantWalk), + serde_json::Value::Number(_num) => Err(WalkError::CantWalk), + serde_json::Value::String(_string) => Err(WalkError::CantWalk), + serde_json::Value::Array(_arr) => Err(WalkError::CantWalk), + serde_json::Value::Object(obj) => obj + .get(segment) + .map(|val| val as _) + .ok_or(WalkError::CantWalk), } } } @@ -128,30 +110,30 @@ impl Walkable for serde_json::Value { impl Loopable for serde_json::Value { fn get_loop_elements(&self) -> Vec<&dyn ContextElement> { match self { - serde_json::Value::Null => Ok(Vec::new()), + serde_json::Value::Null => Vec::new(), serde_json::Value::Bool(boolean) => { if *boolean { - Ok(vec![self]) + vec![self] } 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) => { if string_value.is_empty() { - Ok(Vec::new()) + Vec::new() } else { - Ok(vec![self]) + vec![self] } } serde_json::Value::Array(array_value) => { if array_value.is_empty() { - Ok(Vec::new()) + Vec::new() } 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], } } } diff --git a/src/renderer/parameters_context.rs b/src/renderer/parameters_context.rs index 0a3937e..562933a 100644 --- a/src/renderer/parameters_context.rs +++ b/src/renderer/parameters_context.rs @@ -39,8 +39,9 @@ impl<'a> Renderable for ParametersContext<'a> { } impl<'a> Loopable for ParametersContext<'a> { - fn get_loop_elements(&self) -> Result, RenderError> { - Ok(vec![self]) + fn get_loop_elements(&self) -> Vec<&dyn ContextElement> { + // TODO: Would this even ever be called? Won't matter, but I'd like to know. + vec![self] } } diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 8f74c93..0ba309b 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -104,7 +104,7 @@ impl<'a> DustRenderer<'a> { match val { Err(WalkError::CantWalk) => return Ok("".to_owned()), Ok(final_val) => { - let loop_elements = final_val.get_loop_elements()?; + let loop_elements = final_val.get_loop_elements(); if loop_elements.is_empty() { return Ok("".to_owned()); } else { @@ -115,7 +115,7 @@ impl<'a> DustRenderer<'a> { } DustTag::DTSection(container) => { 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() { // Oddly enough if the value is falsey (like // an empty array or null), Dust uses the @@ -145,7 +145,7 @@ 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)?; + 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), @@ -160,7 +160,7 @@ impl<'a> DustRenderer<'a> { } DustTag::DTNotExists(container) => { 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() { return match &container.else_contents { Some(body) => self.render_body(&body, breadcrumbs),