From a749f91e8eac353cf13d69e61b364bb3d435c1d2 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 7 Jun 2020 16:49:15 -0400 Subject: [PATCH] Ported over the comparison helpers to using the new tap function. --- src/renderer/renderer.rs | 406 ++++++++++++++++++--------------------- 1 file changed, 191 insertions(+), 215 deletions(-) diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index a338ac4..d6faebc 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -375,43 +375,46 @@ impl<'a> DustRenderer<'a> { let param_map = ParametersContext::new(self, breadcrumbs, ¶meterized_block.params); - if !param_map.contains_key("key") { - return Ok("".to_owned()); - } - let left_side = param_map - .walk("key") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - let right_side = param_map - .walk("value") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - // Special case: when comparing two RVPaths, if the - // path points to the same value then they are - // equal. This is particularly important for objects - // which compare memory locations rather than contents - // (javascript object equality). - if Self::are_paths_identical(&left_side, &right_side) - || left_side.as_ref().map(|maybe_ice| { - maybe_ice - .as_ref() - .map(|ice| ice.get_context_element_reference()) - }) == right_side.as_ref().map(|maybe_ice| { - maybe_ice - .as_ref() - .map(|ice| ice.get_context_element_reference()) - }) - { - return self.render_maybe_body( - ¶meterized_block.contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } else { - return self.render_maybe_body( - ¶meterized_block.else_contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } + let left_side = self.tap(breadcrumbs, ¶m_map, "key"); + let right_side = self.tap(breadcrumbs, ¶m_map, "value"); + + let left_side_ce = left_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + let right_side_ce = right_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + return match (left_side_ce, right_side_ce) { + // If "key" is not present at all, return a blank string + (None, _) => Ok("".to_owned()), + (Some(_maybe_left_ce), _) => { + // Special case: when comparing two RVPaths, if the + // path points to the same value then they are + // equal. This is particularly important for objects + // which compare memory locations rather than contents + // (javascript object equality). + if Self::are_paths_identical(&left_side_ce, &right_side_ce) + || left_side_ce.unwrap_or(Err(&WalkError::CantWalk)) + == right_side_ce.unwrap_or(Err(&WalkError::CantWalk)) + { + self.render_maybe_body( + ¶meterized_block.contents, + new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), + blocks, + ) + } else { + self.render_maybe_body( + ¶meterized_block.else_contents, + new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), + blocks, + ) + } + } + }; } DustTag::DTHelperNotEquals(parameterized_block) => { let new_breadcrumbs = self.new_breadcrumbs_partial( @@ -424,43 +427,46 @@ impl<'a> DustRenderer<'a> { let param_map = ParametersContext::new(self, breadcrumbs, ¶meterized_block.params); - if !param_map.contains_key("key") { - return Ok("".to_owned()); - } - let left_side = param_map - .walk("key") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - let right_side = param_map - .walk("value") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - // Special case: when comparing two RVPaths, if the - // path points to the same value then they are - // equal. This is particularly important for objects - // which compare memory locations rather than contents - // (javascript object equality). - if Self::are_paths_identical(&left_side, &right_side) - || left_side.as_ref().map(|maybe_ice| { - maybe_ice - .as_ref() - .map(|ice| ice.get_context_element_reference()) - }) == right_side.as_ref().map(|maybe_ice| { - maybe_ice - .as_ref() - .map(|ice| ice.get_context_element_reference()) - }) - { - return self.render_maybe_body( - ¶meterized_block.else_contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } else { - return self.render_maybe_body( - ¶meterized_block.contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } + let left_side = self.tap(breadcrumbs, ¶m_map, "key"); + let right_side = self.tap(breadcrumbs, ¶m_map, "value"); + + let left_side_ce = left_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + let right_side_ce = right_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + return match (left_side_ce, right_side_ce) { + // If "key" is not present at all, return a blank string + (None, _) => Ok("".to_owned()), + (Some(_maybe_left_ce), _) => { + // Special case: when comparing two RVPaths, if the + // path points to the same value then they are + // equal. This is particularly important for objects + // which compare memory locations rather than contents + // (javascript object equality). + if Self::are_paths_identical(&left_side_ce, &right_side_ce) + || left_side_ce.unwrap_or(Err(&WalkError::CantWalk)) + == right_side_ce.unwrap_or(Err(&WalkError::CantWalk)) + { + self.render_maybe_body( + ¶meterized_block.else_contents, + new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), + blocks, + ) + } else { + self.render_maybe_body( + ¶meterized_block.contents, + new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), + blocks, + ) + } + } + }; } DustTag::DTHelperGreaterThan(parameterized_block) => { let new_breadcrumbs = self.new_breadcrumbs_partial( @@ -472,41 +478,36 @@ impl<'a> DustRenderer<'a> { let param_map = ParametersContext::new(self, breadcrumbs, ¶meterized_block.params); - if !param_map.contains_key("key") { - return Ok("".to_owned()); - } - let left_side = param_map - .walk("key") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - let right_side = param_map - .walk("value") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - match (left_side, right_side) { - (Ok(Some(left_side_unwrapped)), Ok(Some(right_side_unwrapped))) => { - if left_side_unwrapped.get_context_element_reference() - > right_side_unwrapped.get_context_element_reference() - { - return self.render_maybe_body( - ¶meterized_block.contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } else { - return self.render_maybe_body( - ¶meterized_block.else_contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } - } - _ => { - return self.render_maybe_body( - ¶meterized_block.else_contents, + + let left_side = self.tap(breadcrumbs, ¶m_map, "key"); + let right_side = self.tap(breadcrumbs, ¶m_map, "value"); + + let left_side_ce = left_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + let right_side_ce = right_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + return match (left_side_ce, right_side_ce) { + // If "key" is not present at all, return a blank string + (None, _) => Ok("".to_owned()), + (Some(Ok(left_value)), Some(Ok(right_value))) if left_value > right_value => { + self.render_maybe_body( + ¶meterized_block.contents, new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), blocks, ) } - } + _ => self.render_maybe_body( + ¶meterized_block.else_contents, + new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), + blocks, + ), + }; } DustTag::DTHelperGreaterThanOrEquals(parameterized_block) => { let new_breadcrumbs = self.new_breadcrumbs_partial( @@ -518,41 +519,36 @@ impl<'a> DustRenderer<'a> { let param_map = ParametersContext::new(self, breadcrumbs, ¶meterized_block.params); - if !param_map.contains_key("key") { - return Ok("".to_owned()); - } - let left_side = param_map - .walk("key") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - let right_side = param_map - .walk("value") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - match (left_side, right_side) { - (Ok(Some(left_side_unwrapped)), Ok(Some(right_side_unwrapped))) => { - if left_side_unwrapped.get_context_element_reference() - >= right_side_unwrapped.get_context_element_reference() - { - return self.render_maybe_body( - ¶meterized_block.contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } else { - return self.render_maybe_body( - ¶meterized_block.else_contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } - } - _ => { - return self.render_maybe_body( - ¶meterized_block.else_contents, + + let left_side = self.tap(breadcrumbs, ¶m_map, "key"); + let right_side = self.tap(breadcrumbs, ¶m_map, "value"); + + let left_side_ce = left_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + let right_side_ce = right_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + return match (left_side_ce, right_side_ce) { + // If "key" is not present at all, return a blank string + (None, _) => Ok("".to_owned()), + (Some(Ok(left_value)), Some(Ok(right_value))) if left_value >= right_value => { + self.render_maybe_body( + ¶meterized_block.contents, new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), blocks, ) } - } + _ => self.render_maybe_body( + ¶meterized_block.else_contents, + new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), + blocks, + ), + }; } DustTag::DTHelperLessThan(parameterized_block) => { let new_breadcrumbs = self.new_breadcrumbs_partial( @@ -564,41 +560,36 @@ impl<'a> DustRenderer<'a> { let param_map = ParametersContext::new(self, breadcrumbs, ¶meterized_block.params); - if !param_map.contains_key("key") { - return Ok("".to_owned()); - } - let left_side = param_map - .walk("key") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - let right_side = param_map - .walk("value") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - match (left_side, right_side) { - (Ok(Some(left_side_unwrapped)), Ok(Some(right_side_unwrapped))) => { - if left_side_unwrapped.get_context_element_reference() - < right_side_unwrapped.get_context_element_reference() - { - return self.render_maybe_body( - ¶meterized_block.contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } else { - return self.render_maybe_body( - ¶meterized_block.else_contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } - } - _ => { - return self.render_maybe_body( - ¶meterized_block.else_contents, + + let left_side = self.tap(breadcrumbs, ¶m_map, "key"); + let right_side = self.tap(breadcrumbs, ¶m_map, "value"); + + let left_side_ce = left_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + let right_side_ce = right_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + return match (left_side_ce, right_side_ce) { + // If "key" is not present at all, return a blank string + (None, _) => Ok("".to_owned()), + (Some(Ok(left_value)), Some(Ok(right_value))) if left_value < right_value => { + self.render_maybe_body( + ¶meterized_block.contents, new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), blocks, ) } - } + _ => self.render_maybe_body( + ¶meterized_block.else_contents, + new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), + blocks, + ), + }; } DustTag::DTHelperLessThanOrEquals(parameterized_block) => { let new_breadcrumbs = self.new_breadcrumbs_partial( @@ -610,41 +601,36 @@ impl<'a> DustRenderer<'a> { let param_map = ParametersContext::new(self, breadcrumbs, ¶meterized_block.params); - if !param_map.contains_key("key") { - return Ok("".to_owned()); - } - let left_side = param_map - .walk("key") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - let right_side = param_map - .walk("value") - .map(|ice| ice.into_context_element(self, breadcrumbs)); - match (left_side, right_side) { - (Ok(Some(left_side_unwrapped)), Ok(Some(right_side_unwrapped))) => { - if left_side_unwrapped.get_context_element_reference() - <= right_side_unwrapped.get_context_element_reference() - { - return self.render_maybe_body( - ¶meterized_block.contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } else { - return self.render_maybe_body( - ¶meterized_block.else_contents, - new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), - blocks, - ); - } - } - _ => { - return self.render_maybe_body( - ¶meterized_block.else_contents, + + let left_side = self.tap(breadcrumbs, ¶m_map, "key"); + let right_side = self.tap(breadcrumbs, ¶m_map, "value"); + + let left_side_ce = left_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + let right_side_ce = right_side.as_ref().map(|maybe_ice| { + maybe_ice + .as_ref() + .map(|ice| ice.get_context_element_reference()) + }); + return match (left_side_ce, right_side_ce) { + // If "key" is not present at all, return a blank string + (None, _) => Ok("".to_owned()), + (Some(Ok(left_value)), Some(Ok(right_value))) if left_value <= right_value => { + self.render_maybe_body( + ¶meterized_block.contents, new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), blocks, ) } - } + _ => self.render_maybe_body( + ¶meterized_block.else_contents, + new_breadcrumbs.as_ref().unwrap_or(breadcrumbs), + blocks, + ), + }; } DustTag::DTHelperSep(parameterized_block) => { let new_breadcrumbs = self.new_breadcrumbs_partial( @@ -803,13 +789,13 @@ impl<'a> DustRenderer<'a> { name: &P, ) -> Option, WalkError>> where - P: Borrow, + P: AsRef + ?Sized, { - if !parameters.contains_key(name.borrow()) { + if !parameters.contains_key(name.as_ref()) { return None; } let val = parameters - .walk("key") + .walk(name.as_ref()) .map(|ice| ice.into_context_element(self, breadcrumbs)); match val { Ok(Some(ice_result)) => Some(Ok(ice_result)), @@ -924,21 +910,11 @@ impl<'a> DustRenderer<'a> { } fn are_paths_identical<'b>( - left_side: &Result>, WalkError>, - right_side: &Result>, WalkError>, + left_side: &Option>, + right_side: &Option>, ) -> bool { - let left_resolved = left_side.as_ref().map(|maybe_ice| { - maybe_ice - .as_ref() - .map(|ice| ice.get_context_element_reference()) - }); - let right_resolved = right_side.as_ref().map(|maybe_ice| { - maybe_ice - .as_ref() - .map(|ice| ice.get_context_element_reference()) - }); - match (left_resolved, right_resolved) { - (Ok(Some(lce)), Ok(Some(rce))) => lce as *const _ == rce as *const _, + match (left_side, right_side) { + (Some(Ok(lce)), Some(Ok(rce))) => (*lce) as *const _ == (*rce) as *const _, _ => false, } }