Math helper nearly done, test almost passes.

This commit is contained in:
Tom Alexander 2020-06-13 21:28:21 -04:00
parent 28a4fea96f
commit d9f10290f5
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 53 additions and 4 deletions

View File

@ -68,6 +68,16 @@ impl<'a> ParametersContext<'a> {
}
}
pub fn from_values(
parent: Option<&'a ParametersContext<'a>>,
params: HashMap<&'a str, Option<BreadcrumbTreeElement<'a>>>,
) -> Self {
ParametersContext {
parent: parent,
params: params,
}
}
pub fn contains_key(&self, segment: &str) -> bool {
self.params.contains_key(segment)
|| self

View File

@ -665,15 +665,54 @@ impl<'a> DustRenderer<'a> {
})
.unwrap_or(Ok("".to_owned()));
}
Some(_) => {
// TODO: math with body, calculate the value and use it like a select block
Some(body) => {
// Calculate the value
let calculated_value = self.perform_math_operation(breadcrumbs, &param_map);
// Generate a ParametersContext with the result of the math operation as key
let calculated_value =
match self.perform_math_operation(breadcrumbs, &param_map) {
None => return Ok("".to_owned()),
Some(val) => val,
};
// Generate a ParametersContext with the result of the math operation as key
let converted_value: BreadcrumbTreeElement<'_> = calculated_value.into();
let calculated_param_map: HashMap<&str, Option<BreadcrumbTreeElement<'_>>> =
vec![("key", Some(converted_value))].into_iter().collect();
let calculated_context =
ParametersContext::from_values(None, calculated_param_map);
// calculate are_any_checks_true
let are_any_checks_true = body
.elements
.iter()
.filter_map(|te| match te {
TemplateElement::TETag(dt) => match dt {
DustTag::DTHelperEquals(_)
| DustTag::DTHelperNotEquals(_)
| DustTag::DTHelperGreaterThan(_)
| DustTag::DTHelperLessThan(_)
| DustTag::DTHelperGreaterThanOrEquals(_)
| DustTag::DTHelperLessThanOrEquals(_) => Some(dt),
_ => None,
},
_ => None,
})
.map(|dt| {
self.perform_comparison_check(
dt,
new_breadcrumbs_ref,
Some(&calculated_context),
)
})
.any(|check_result| check_result.unwrap_or(false));
// Generate a SelectContext
let select_context =
SelectContext::new(&calculated_context, are_any_checks_true);
// render_maybe_body
return self.render_maybe_body(
&parameterized_block.contents,
new_breadcrumbs_ref,
blocks,
&mut Some(select_context),
);
}
}
}