From 9c7883358a0d31d91e68f9a24bd7470e087096a9 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 12 Apr 2020 20:52:30 -0400 Subject: [PATCH] Better error handling added to walkable --- src/bin.rs | 35 ++++++++++++++++++++++++----------- src/renderer/renderer.rs | 25 +++++++++++++++---------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index d334aef..7bad3b5 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -88,19 +88,32 @@ impl Renderable for serde_json::Value { impl Walkable for serde_json::Value { fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> { match self { - serde_json::Value::Null => panic!("Attempted to walk to {} on a null value", segment), - serde_json::Value::Bool(_boolean) => { - panic!("Attempted to walk to {} on a boolean value", segment) - } - serde_json::Value::Number(_num) => { - panic!("Attempted to walk to {} on a number value", segment) - } - serde_json::Value::String(_string) => { - panic!("Attempted to walk to {} on a string value", segment) - } + 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) => todo!("Arrays not supported yet"), // TODO: Handle this error better - serde_json::Value::Object(obj) => Ok(obj.get(segment).unwrap()), + serde_json::Value::Object(obj) => { + obj.get(segment) + .map(|val| val as _) + .ok_or(RenderError::WontWalk { + segment: segment.to_string(), + elem: self, + }) + } } } } diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 80cd62e..b53b56c 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -139,24 +139,29 @@ mod tests { impl Walkable for HashMap<&str, I> { fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> { - Ok(self.get(segment).unwrap()) - // TODO: Handle error here better - // self.get(segment).ok_or(RenderError::WontWalk { - // segment: segment.to_string(), - // elem: self, - // }) + let child = self.get(segment).ok_or(RenderError::WontWalk { + segment: segment.to_string(), + elem: self, + })?; + Ok(child) } } impl Walkable for &str { - fn walk(&self, _segment: &str) -> Result<&dyn ContextElement, RenderError> { - panic!("Tried to walk down a str"); + fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> { + Err(RenderError::CantWalk { + segment: segment.to_string(), + elem: self, + }) } } impl Walkable for u32 { - fn walk(&self, _segment: &str) -> Result<&dyn ContextElement, RenderError> { - panic!("Tried to walk down a str"); + fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> { + Err(RenderError::CantWalk { + segment: segment.to_string(), + elem: self, + }) } }