Better error handling added to walkable
This commit is contained in:
parent
a647ed6d43
commit
9c7883358a
35
src/bin.rs
35
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -139,24 +139,29 @@ mod tests {
|
||||
|
||||
impl<I: ContextElement> 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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user