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

@@ -85,10 +85,10 @@ impl<'a> DustRenderer<'a> {
C: ContextElement,
{
match tag {
DustTag::DTComment(comment) => (),
DustTag::DTComment(_comment) => (),
DustTag::DTReference(reference) => {
let val = walk_path(context, &reference.path.keys)?;
return Ok(val.render());
return val.render();
}
_ => (), // TODO: Implement the rest
}
@@ -120,20 +120,20 @@ mod tests {
impl<I: ContextElement> ContextElement for HashMap<&str, I> {}
impl Renderable for u32 {
fn render(&self) -> std::string::String {
self.to_string()
fn render(&self) -> Result<String, RenderError> {
Ok(self.to_string())
}
}
impl Renderable for &str {
fn render(&self) -> std::string::String {
self.to_string()
fn render(&self) -> Result<String, RenderError> {
Ok(self.to_string())
}
}
impl<I: ContextElement> Renderable for HashMap<&str, I> {
fn render(&self) -> std::string::String {
panic!("Attempted to render a hashmap");
fn render(&self) -> Result<String, RenderError> {
Err(RenderError::CantRender { elem: self })
}
}
@@ -182,16 +182,23 @@ mod tests {
.iter()
.cloned()
.collect();
assert_eq!(walk_path(&context, &vec!["cat"]).unwrap().render(), "kitty");
assert_eq!(
walk_path(&number_context, &vec!["tiger"]).unwrap().render(),
"3"
walk_path(&context, &vec!["cat"]).unwrap().render().unwrap(),
"kitty".to_owned()
);
assert_eq!(
walk_path(&number_context, &vec!["tiger"])
.unwrap()
.render()
.unwrap(),
"3".to_owned()
);
assert_eq!(
walk_path(&deep_context, &vec!["tiger", "food"])
.unwrap()
.render(),
"people"
.render()
.unwrap(),
"people".to_owned()
);
}
}