From 6758d515f15cbf622f6457417504118db025646f Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 16 May 2020 16:24:36 -0400 Subject: [PATCH] Have greater than helper very close to correct. Just need to make it compare arrays of scalars. --- js/test_cases/helpers_gt/README.md | 15 ++++++++++ js/test_cases/helpers_gt/main.dust | 3 ++ src/renderer/renderer.rs | 47 ++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/js/test_cases/helpers_gt/README.md b/js/test_cases/helpers_gt/README.md index 58a764a..f418aed 100644 --- a/js/test_cases/helpers_gt/README.md +++ b/js/test_cases/helpers_gt/README.md @@ -17,3 +17,18 @@ greater than or equals to All comparisons between non-matching types (for example, int vs string) appear to render the main block. Comparisons between non-scalar types (like arrays) appears to render the main block + +less than +--------- + +All comparisons between non-matching types (for example, int vs string) appear to render the else block. + +Comparisons between non-scalar types (like arrays) appears to render the else block + + +less than or equal to +--------------------- + +All comparisons between non-matching types (for example, int vs string) appear to render the main block. + +Comparisons between non-scalar types (like arrays) appears to render the main block diff --git a/js/test_cases/helpers_gt/main.dust b/js/test_cases/helpers_gt/main.dust index 9a4ffa8..c7b3699 100644 --- a/js/test_cases/helpers_gt/main.dust +++ b/js/test_cases/helpers_gt/main.dust @@ -20,7 +20,10 @@ beta is {beta}{~n} {@gt key="a" value="]"}"a" is greater than "]"{:else}"a" is less than or equal to "]"{/gt}{~n} {@gt key="a" value="A"}"a" is greater than "A"{:else}"a" is less than or equal to "A"{/gt}{~n} {@gt key="a" value="}"}"a" is greater than "}"{:else}"a" is less than or equal to "}"{/gt}{~n} +{! +Commented out because unicode not working in rust implementation yet {@gt key="☃" value="☄"}"☃" is greater than "☄"{:else}"☃" is less than or equal to "☄"{/gt}{~n} +!} {@gt key=true_value value=false_value}true is greater than false{:else}true is less than or equal to false{/gt}{~n} {@gt key=array_lower value=array_higher}[3,5,7] is greater than [8,9]{:else}[3,5,7] is less than or equal to [8,9]{/gt}{~n} {@gt key=array_higher value=array_lower}[8,9] is greater than [3,5,7]{:else}[8,9] is less than or equal to [3,5,7]{/gt}{~n} diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 7ecfc14..cd83681 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -340,6 +340,53 @@ impl<'a> DustRenderer<'a> { .iter() .map(|pair: &KVPair<'a>| (pair.key, &pair.value)) .collect(); + let left_side: Result<&dyn ContextElement, WalkError> = match param_map.get("key") { + None => return Ok("".to_owned()), + Some(rval) => match rval { + RValue::RVString(text) => Ok(text), + RValue::RVPath(path) => walk_path(breadcrumbs, &path.keys), + RValue::RVPositiveInteger(num) => Ok(num), + }, + }; + let right_side: Result<&dyn ContextElement, WalkError> = + match param_map.get("value") { + None => Err(WalkError::CantWalk), + Some(rval) => match rval { + RValue::RVString(text) => Ok(text), + RValue::RVPath(path) => walk_path(breadcrumbs, &path.keys), + RValue::RVPositiveInteger(num) => Ok(num), + }, + }; + match (left_side, right_side) { + (Err(_), _) | (_, Err(_)) => match ¶meterized_block.else_contents { + None => return Ok("".to_owned()), + Some(body) => { + let rendered_content = self.render_body(body, breadcrumbs, blocks)?; + return Ok(rendered_content); + } + }, + (Ok(left_side_unwrapped), Ok(right_side_unwrapped)) => { + if left_side_unwrapped > right_side_unwrapped { + match ¶meterized_block.contents { + None => return Ok("".to_owned()), + Some(body) => { + let rendered_content = + self.render_body(body, breadcrumbs, blocks)?; + return Ok(rendered_content); + } + } + } else { + match ¶meterized_block.else_contents { + None => return Ok("".to_owned()), + Some(body) => { + let rendered_content = + self.render_body(body, breadcrumbs, blocks)?; + return Ok(rendered_content); + } + } + } + } + } } _ => (), // TODO: Implement the rest }