summaryrefslogtreecommitdiff
path: root/src/templatespec.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-09-03 10:21:08 +0300
committerLars Wirzenius <liw@liw.fi>2020-09-04 10:30:18 +0300
commit803a8b1d879e0c7ec005b2652f06459f58956379 (patch)
treed7e87cb4c403bede8d4b83bc72f2d44149a6341d /src/templatespec.rs
parent6c88187985ce5bb9203619181ab667b541c7cd55 (diff)
downloadsubplot-803a8b1d879e0c7ec005b2652f06459f58956379.tar.gz
feat(codegen): allow templates to list helper files
A template's YAML file can now list any additional files, called helpers. An empty list is OK, as is not having the 'helper' key in the YAML file. The helpers are loaded as Tera templates so they can be included in the main template. This will allow us to split out un-templated code out of the main template. The helpers can then be linted and unit tested separately. This commit only adds the mechanism, and fixes up the YAML files. Later commits will actually split the main Python template into helpers, and add unit tests. refactor: use .join
Diffstat (limited to 'src/templatespec.rs')
-rw-r--r--src/templatespec.rs16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/templatespec.rs b/src/templatespec.rs
index dc85f1d..23f5c86 100644
--- a/src/templatespec.rs
+++ b/src/templatespec.rs
@@ -15,6 +15,8 @@ use std::path::{Path, PathBuf};
#[derive(Debug, Deserialize)]
pub struct TemplateSpec {
template: PathBuf,
+ #[serde(default)]
+ helpers: Vec<PathBuf>,
run: Option<String>,
}
@@ -25,9 +27,15 @@ impl TemplateSpec {
}
// Create a new TemplateSpec.
- fn new(basedir: &Path, template: &Path, run: Option<&str>) -> TemplateSpec {
+ fn new(
+ basedir: &Path,
+ template: &Path,
+ helpers: Vec<PathBuf>,
+ run: Option<&str>,
+ ) -> TemplateSpec {
TemplateSpec {
template: basedir.to_path_buf().join(template),
+ helpers,
run: match run {
Some(x) => Some(x.to_string()),
None => None,
@@ -52,6 +60,7 @@ impl TemplateSpec {
Ok(TemplateSpec::new(
&dirname,
spec.template_filename(),
+ spec.helpers().map(|p| p.to_path_buf()).collect(),
spec.run(),
))
}
@@ -61,6 +70,11 @@ impl TemplateSpec {
&self.template
}
+ /// Return iterator for names of helper files.
+ pub fn helpers(&self) -> impl Iterator<Item = &Path> {
+ self.helpers.iter().map(|p| p.as_path())
+ }
+
/// Return command to run the generated test program, if specified.
///
/// The name of the test program gets appended.