Skip to content

Commit

Permalink
whitespace control improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddewie committed Apr 27, 2024
1 parent 8c7841f commit d657ec3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ fn evaluate_template(data: &Value, record: Pair<Rule>) -> Result<String, Templat
}

// Provide the looped results
iterables_results.iter().for_each(|iterable_result|
iterables_results.iter().for_each(|iterable_result| {
// eprintln!("{}", iterable_result.take().as_str());
result.push_str(iterable_result.take().as_str())
}
)
}
Rule::with_template => {
Expand Down
2 changes: 1 addition & 1 deletion src/grammar/template.pest
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ end_template = { linear_whitespace* ~ start_marker ~ whitespace* ~ keyword_end ~
if_elif_else_template = { if_template ~ sub_template ~ (elif_template ~ sub_template)* ~ (else_template ~ sub_template)? ~ end_template }
for_template = { linear_whitespace* ~ start_marker ~ whitespace* ~ keyword_for ~ whitespace+ ~ property ~ whitespace+ ~ "in" ~ whitespace+ ~ expression ~ whitespace* ~ end_marker ~ vertical_whitespace? }
with_template = { linear_whitespace* ~ start_marker ~ whitespace* ~ keyword_with ~ whitespace+ ~ property ~ whitespace+ ~ "=" ~ whitespace+ ~ expression ~ whitespace* ~ end_marker ~ vertical_whitespace? ~ sub_template ~ end_template }
for_else_template = { for_template ~ sub_template ~ else_template? ~ sub_template ~ end_template }
for_else_template = { for_template ~ sub_template ~ (else_template ~ sub_template)? ~ end_template }
comment = { comment_start_marker ~ (!comment_end_marker ~ ANY)+ ~ comment_end_marker ~ vertical_whitespace? }
debug_template = { start_marker ~ whitespace* ~ keyword_debug ~ whitespace+ ~ expression ~ whitespace* ~ end_marker ~ vertical_whitespace? }
template = { comment | debug_template | if_elif_else_template | for_else_template | with_template | expression_template }
Expand Down
6 changes: 5 additions & 1 deletion tests/configuration/vlan.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
vlans:
- id: 10
- id: 20
name: printer
name: printer
- id: 30
name: pc
- id: 40
name: tablet
13 changes: 9 additions & 4 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,12 +1064,17 @@ fn vlan_test() {

assert
.success()
.stdout(r#"vlan 10
vlan 20
name printer
.stdout(r#"vlans
vlan 10
vlan 20
name printer
vlan 30
name pc
vlan 40
name tablet
"#)
.stderr(is_match(r#"^\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z INFO template\] Using template file 'tests/template/vlan.template'
\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z INFO template\] Using configuration file 'tests/configuration/vlan.yml'
\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z INFO template\] Parsing configuration using YAML format
$"#).unwrap());
}
}
13 changes: 7 additions & 6 deletions tests/template/vlan.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% for vlan in vlans %}
vlan {% vlan.id %}
{% if vlan.name %}
name {% vlan.name %}
{% end %}
{% end %}
vlans
{% for vlan in vlans %}
vlan {% vlan.id %}
{% if vlan.name %}
name {% vlan.name %}
{% end %}
{% end %}

0 comments on commit d657ec3

Please sign in to comment.