Switch the Renderable trait over to the returning a RenderError

This commit is contained in:
Tom Alexander
2020-04-12 21:03:55 -04:00
parent 9c7883358a
commit 610adc8b72
4 changed files with 36 additions and 22 deletions

View File

@@ -73,14 +73,14 @@ fn read_context_from_stdin() -> serde_json::Value {
impl ContextElement for serde_json::Value {}
impl Renderable for serde_json::Value {
fn render(&self) -> String {
fn render(&self) -> Result<String, RenderError> {
match self {
serde_json::Value::Null => "".to_owned(),
serde_json::Value::Bool(boolean) => boolean.to_string(),
serde_json::Value::Number(num) => num.to_string(),
serde_json::Value::String(string) => string.to_string(),
serde_json::Value::Array(_arr) => panic!("Attempted to render an array"),
serde_json::Value::Object(_obj) => panic!("Attempted to render an object"),
serde_json::Value::Null => Ok("".to_owned()),
serde_json::Value::Bool(boolean) => Ok(boolean.to_string()),
serde_json::Value::Number(num) => Ok(num.to_string()),
serde_json::Value::String(string) => Ok(string.to_string()),
serde_json::Value::Array(_arr) => Err(RenderError::CantRender { elem: self }),
serde_json::Value::Object(_obj) => Err(RenderError::CantRender { elem: self }),
}
}
}
@@ -105,7 +105,6 @@ impl Walkable for serde_json::Value {
elem: self,
}),
serde_json::Value::Array(_arr) => todo!("Arrays not supported yet"),
// TODO: Handle this error better
serde_json::Value::Object(obj) => {
obj.get(segment)
.map(|val| val as _)