Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make formatters work with @-keywords #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 51 additions & 8 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,42 @@ impl<'template> Template<'template> {
program_counter += 1;
}
Instruction::FormattedValue(path, name) => {
// The @ keywords aren't supported for formatted values. Should they be?
let value_to_render = render_context.lookup(path)?;
match formatter_registry.get(name) {
Some(formatter) => {
let formatter_result = formatter(value_to_render, output);
if let Err(err) = formatter_result {
return Err(called_formatter_error(self.original_text, name, err));
let formatter = match formatter_registry.get(name) {
Some(formatter) => formatter,
None => return Err(unknown_formatter(self.original_text, name)),
};
let first = path.first().unwrap();
let formatter_result = if first.starts_with('@') {
// Same as above, functions are hardcoded.
let first: &str = &*first;
match first {
"@index" => {
let value_to_render =
Value::Number(render_context.lookup_index()?.0.into());
formatter(&value_to_render, output)
}
"@first" => {
let value_to_render =
Value::Bool(render_context.lookup_index()?.0 == 0);
formatter(&value_to_render, output)
}
"@last" => {
let (index, length) = render_context.lookup_index()?;
let value_to_render = Value::Bool(index == length - 1);
formatter(&value_to_render, output)
}
"@root" => {
let value_to_render = render_context.lookup_root()?;
formatter(value_to_render, output)
}
_ => panic!(), // This should have been caught by the parser.
}
None => return Err(unknown_formatter(self.original_text, name)),
} else {
let value_to_render = render_context.lookup(path)?;
formatter(value_to_render, output)
};
if let Err(err) = formatter_result {
return Err(called_formatter_error(self.original_text, name, err));
}
program_counter += 1;
}
Expand Down Expand Up @@ -701,6 +727,23 @@ mod test {
assert_eq!("2", &string);
}

#[test]
fn test_for_loop_index_formatted() {
let template = compile("{{ for a in array }}{ @index | my_formatter }{{ endfor }}");
let context = context();
let template_registry = other_templates();
let formatter_registry = formatters();
let string = template
.render(
&context,
&template_registry,
&formatter_registry,
&default_formatter(),
)
.unwrap();
assert_eq!("{0}{1}{2}", &string);
}

#[test]
fn test_whitespace_stripping_value() {
let template = compile("1 \n\t {- number -} \n 1");
Expand Down