From 7a8247f38acdde1112d8b1de5b2b6c599183f50b Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 10 May 2020 14:22:59 -0400 Subject: [PATCH] Getting the left and right sides. --- js/test_cases/helpers_eq/README.md | 5 +++++ js/test_cases/helpers_eq/main.dust | 5 +++++ src/parser/parser.rs | 8 ++++---- src/renderer/renderer.rs | 24 ++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 js/test_cases/helpers_eq/README.md diff --git a/js/test_cases/helpers_eq/README.md b/js/test_cases/helpers_eq/README.md new file mode 100644 index 0000000..584c2f5 --- /dev/null +++ b/js/test_cases/helpers_eq/README.md @@ -0,0 +1,5 @@ +Without a key parameter, neither the main block nor the else block is rendered. + +Without a value parameter, it probably uses "null" but I haven't yet confirmed that with additional testing. Potentially could also just be making all falsey values equal. + +Literal values work in both keys and values. diff --git a/js/test_cases/helpers_eq/main.dust b/js/test_cases/helpers_eq/main.dust index 37a9de8..3d2c60b 100644 --- a/js/test_cases/helpers_eq/main.dust +++ b/js/test_cases/helpers_eq/main.dust @@ -8,3 +8,8 @@ beta is {beta}{~n} {@eq key=int value="7"}int is equal to "7"{:else}int does not equal "7"{/eq}{~n} {@eq key=int value=7}int is equal to 7{:else}int does not equal 7{/eq}{~n} {@eq key=alpha value=beta}alpha is equal to beta{:else}alpha does not equal beta{/eq}{~n} +{@eq value=beta}missing key is true{:else}missing key is false{/eq}{~n} +{@eq value=gamma}missing key and non-existent value is true{:else}missing key and non-existent value is false{/eq}{~n} +{@eq key=alpha}missing value is true{:else}missing value is false{/eq}{~n} +{@eq key=gamma}missing value and non-existent key is true{:else}missing value and non-existent key is false{/eq}{~n} +{@eq key="master" value="master"}"master" is equal to "master"{:else}"master" does not equal "master"{/eq}{~n} diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 768ec88..538e159 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -106,10 +106,10 @@ pub struct NamedBlock<'a> { #[derive(Clone, Debug, PartialEq)] pub struct ParameterizedBlock<'a> { - name: &'a str, - params: Vec>, - contents: Option>, - else_contents: Option>, + pub name: &'a str, + pub params: Vec>, + pub contents: Option>, + pub else_contents: Option>, } #[derive(Clone, Debug, PartialEq)] diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index e6b7bd4..a528662 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -1,6 +1,8 @@ use crate::parser::template; use crate::parser::Body; use crate::parser::DustTag; +use crate::parser::KVPair; +use crate::parser::RValue; use crate::parser::Special; use crate::parser::Template; use crate::parser::TemplateElement; @@ -222,6 +224,28 @@ impl<'a> DustRenderer<'a> { }, }; } + DustTag::DTHelperEquals(parameterized_block) => { + let param_map: HashMap<&'a str, &'a RValue<'a>> = parameterized_block + .params + .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), + }, + }; + 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), + }, + }; + } _ => (), // TODO: Implement the rest } Ok("".to_owned())