From 9baa669dea8e603b9dd6c48fbb3a539127ce70bf Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 10 May 2020 22:04:41 -0400 Subject: [PATCH] Separating out CastToAny. --- src/bin.rs | 6 ++++++ src/renderer/context_element.rs | 26 +++++++++++++++++--------- src/renderer/parameters_context.rs | 17 ++++++++++++++++- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index 661670d..47c977b 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -140,3 +140,9 @@ impl Loopable for serde_json::Value { } } } + +impl CompareContextElement for serde_json::Value { + fn equals(&self, other: &dyn ContextElement) -> bool { + false + } +} diff --git a/src/renderer/context_element.rs b/src/renderer/context_element.rs index 29ca430..8701711 100644 --- a/src/renderer/context_element.rs +++ b/src/renderer/context_element.rs @@ -30,9 +30,11 @@ pub trait Loopable { fn get_loop_elements(&self) -> Vec<&dyn ContextElement>; } -pub trait CompareContextElement { +pub trait CastToAny { fn to_any(&self) -> &dyn Any; +} +pub trait CompareContextElement: CastToAny { fn equals(&self, other: &dyn ContextElement) -> bool; } @@ -46,19 +48,25 @@ impl CloneIntoBoxedContextElement for C { } } -impl CompareContextElement for C { +impl CastToAny for C { fn to_any(&self) -> &dyn Any { self } - - fn equals(&self, other: &dyn ContextElement) -> bool { - other - .to_any() - .downcast_ref::() - .map_or(false, |a| self == a) - } } +// impl CompareContextElement for C { +// fn to_any(&self) -> &dyn Any { +// self +// } + +// fn equals(&self, other: &dyn ContextElement) -> bool { +// other +// .to_any() +// .downcast_ref::() +// .map_or(false, |a| self == a) +// } +// } + impl<'a, 'b> PartialEq<&'b dyn ContextElement> for &'a dyn ContextElement { fn eq(&self, other: &&'b dyn ContextElement) -> bool { self.equals(*other) diff --git a/src/renderer/parameters_context.rs b/src/renderer/parameters_context.rs index f6045b7..6d525ca 100644 --- a/src/renderer/parameters_context.rs +++ b/src/renderer/parameters_context.rs @@ -1,5 +1,6 @@ use crate::parser::KVPair; use crate::parser::{Filter, RValue}; +use crate::renderer::context_element::CastToAny; use crate::renderer::context_element::CloneIntoBoxedContextElement; use crate::renderer::context_element::CompareContextElement; use crate::renderer::context_element::ContextElement; @@ -111,11 +112,13 @@ impl Clone for NewParametersContext { } } -impl CompareContextElement for NewParametersContext { +impl CastToAny for NewParametersContext { fn to_any(&self) -> &dyn Any { self } +} +impl CompareContextElement for NewParametersContext { fn equals(&self, other: &dyn ContextElement) -> bool { // TODO: Does this ever happen? perhaps I should have a panic here. false @@ -197,3 +200,15 @@ impl Walkable for String { Err(WalkError::CantWalk) } } + +impl CompareContextElement for String { + fn equals(&self, other: &dyn ContextElement) -> bool { + // If its a String then compare them directly, otherwise defer + // to the other type's implementation of CompareContextElement + // since the end user could add any type. + match other.to_any().downcast_ref::() { + None => other.equals(self), + Some(other_string) => self == other_string, + } + } +}