diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-01-23 13:14:50 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-01-23 13:14:50 +0000 |
commit | 854d78e02d501c6c8f1a469b3eae1087fec46855 (patch) | |
tree | 05ba3bb7c1d82ddd435e79a103a642fe6582c5ef /share | |
parent | 6e12f4fd1834689c0149cc13ab04f9965a1faa0e (diff) | |
download | subplot-854d78e02d501c6c8f1a469b3eae1087fec46855.tar.gz |
share/rust: Add macro for step builder and use it for cleanup too
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'share')
-rw-r--r-- | share/rust/template/macros.rs.tera | 30 | ||||
-rw-r--r-- | share/rust/template/template.rs.tera | 33 | ||||
-rw-r--r-- | share/rust/template/template.yaml | 2 |
3 files changed, 36 insertions, 29 deletions
diff --git a/share/rust/template/macros.rs.tera b/share/rust/template/macros.rs.tera new file mode 100644 index 0000000..164421d --- /dev/null +++ b/share/rust/template/macros.rs.tera @@ -0,0 +1,30 @@ +{%- macro builder(stepfn, step) -%} +{{stepfn}}::Builder::default() + {% for part in step.parts %}{% if part.CapturedText is defined -%} + {%- set name = part.CapturedText.name -%} + {%- set text = part.CapturedText.text -%} + {%- set type = step.types[name] | default(value='text') -%} + .{{name}}( + {% if type in ['number', 'int', 'uint'] %}{{text}} + {%- elif type in ['text', 'word']%} + // "{{text | commentsafe }}" + &base64_decode("{{text | base64}}" + ) + {%- elif type in ['file'] %} + { + use std::path::PathBuf; + // {{ text | commentsafe }} + let target_name: PathBuf = base64_decode("{{ text | base64 }}").into(); + SUBPLOT_EMBEDDED_FILES + .iter() + .find(|df| df.name() == target_name) + .expect("Unable to find file at runtime") + .clone() + } + {%- else %} /* WOAH unknown type {{step.types[name]}} */ {{text}} + {%- endif %} + ) + {% endif -%} + {% endfor -%} + .build() +{%- endmacro builder -%} diff --git a/share/rust/template/template.rs.tera b/share/rust/template/template.rs.tera index c972d37..c94d63c 100644 --- a/share/rust/template/template.rs.tera +++ b/share/rust/template/template.rs.tera @@ -1,3 +1,5 @@ +{% import "macros.rs.tera" as macros %} + use subplotlib::prelude::*; {% for func in functions %} @@ -29,36 +31,9 @@ lazy_static! { fn {{ scenario.title | nameslug }}() { let mut scenario = Scenario::new(&base64_decode("{{scenario.title | base64}}")); {% for step in scenario.steps %} - let step = {{step.function}}::Builder::default() - {% for part in step.parts %}{% if part.CapturedText is defined -%} - {%- set name = part.CapturedText.name -%} - {%- set text = part.CapturedText.text -%} - {%- set type = step.types[name] | default(value='text') -%} - .{{name}}( - {% if type in ['number', 'int', 'uint'] %}{{text}} - {%- elif type in ['text', 'word']%} - // "{{text | commentsafe }}" - &base64_decode("{{text | base64}}" - ) - {%- elif type in ['file'] %} - { - use std::path::PathBuf; - // {{ text | commentsafe }} - let target_name: PathBuf = base64_decode("{{ text | base64 }}").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - } - {%- else %} /* WOAH unknown type {{step.types[name]}} */ {{text}} - {%- endif %} - ) - {% endif -%} - {% endfor -%} - .build(); + let step = {{ macros::builder(stepfn=step.function, step=step) }}; {%- if step.cleanup %} - let cleanup = {{step.cleanup}}::Builder::default().build(); + let cleanup = {{ macros::builder(stepfn=step.cleanup, step=step) }}; scenario.add_step(step, Some(cleanup)); {%- else %} scenario.add_step(step, None); diff --git a/share/rust/template/template.yaml b/share/rust/template/template.yaml index 110f5df..cedb16b 100644 --- a/share/rust/template/template.yaml +++ b/share/rust/template/template.yaml @@ -1,2 +1,4 @@ template: template.rs.tera run: cargo test +helpers: + - macros.rs.tera |