summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2021-08-07 15:37:51 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2021-09-07 17:32:16 +0100
commitb443c533f9e5a0bdbf8b380a8d7d59e46c28858b (patch)
treefe01f78e107ca092a3d13875ddff704c6ec2afce /src
parent1afd692bacf6c95d5897d4af74f6f2d4f8b91c1f (diff)
downloadsubplot-b443c533f9e5a0bdbf8b380a8d7d59e46c28858b.tar.gz
chore: Unwind global template name
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src')
-rw-r--r--src/bin/subplot.rs7
-rw-r--r--src/bindings.rs4
-rw-r--r--src/codegen.rs17
-rw-r--r--src/metadata.rs9
-rw-r--r--src/resource.rs15
-rw-r--r--src/templatespec.rs2
6 files changed, 27 insertions, 27 deletions
diff --git a/src/bin/subplot.rs b/src/bin/subplot.rs
index b8524e1..22eda3b 100644
--- a/src/bin/subplot.rs
+++ b/src/bin/subplot.rs
@@ -414,7 +414,12 @@ impl Codegen {
}
let spec = template_spec(&doc)?;
- generate_test_program(&mut doc, &spec, &self.output)?;
+ let template = doc
+ .meta()
+ .template_name()
+ .ok_or_else(|| anyhow::anyhow!("No template name given"))?
+ .to_string();
+ generate_test_program(&mut doc, &spec, &self.output, &template)?;
if self.run {
let run = match spec.run() {
diff --git a/src/bindings.rs b/src/bindings.rs
index 4447c66..c805965 100644
--- a/src/bindings.rs
+++ b/src/bindings.rs
@@ -506,11 +506,11 @@ impl Bindings {
/// Add bindings from a file.
#[instrument(level = "trace", skip(self))]
- pub fn add_from_file<P>(&mut self, filename: P) -> Result<()>
+ pub fn add_from_file<P>(&mut self, filename: P, template: Option<&str>) -> Result<()>
where
P: AsRef<Path> + Debug,
{
- let yaml = resource::read_as_string(filename.as_ref())
+ let yaml = resource::read_as_string(filename.as_ref(), template)
.map_err(|e| SubplotError::BindingsFileNotFound(filename.as_ref().into(), e))?;
event!(Level::TRACE, "Loaded file content");
self.add_from_yaml(&yaml).map_err(|e| {
diff --git a/src/codegen.rs b/src/codegen.rs
index d27a4d8..f48f049 100644
--- a/src/codegen.rs
+++ b/src/codegen.rs
@@ -27,14 +27,17 @@ pub fn generate_test_program(
doc: &mut Document,
spec: &TemplateSpec,
filename: &Path,
+ template: &str,
) -> Result<(), SubplotError> {
- let context = context(doc)?;
- let code = tera(spec)?.render("template", &context).expect("render");
+ let context = context(doc, template)?;
+ let code = tera(spec, template)?
+ .render("template", &context)
+ .expect("render");
write(filename, &code)?;
Ok(())
}
-fn context(doc: &mut Document) -> Result<Context, SubplotError> {
+fn context(doc: &mut Document, template: &str) -> Result<Context, SubplotError> {
let mut context = Context::new();
context.insert("scenarios", &doc.matched_scenarios()?);
context.insert("files", doc.files());
@@ -42,7 +45,7 @@ fn context(doc: &mut Document) -> Result<Context, SubplotError> {
let funcs_filenames = doc.meta().functions_filenames();
let mut funcs = vec![];
for filename in funcs_filenames {
- let content = resource::read_as_string(filename)
+ let content = resource::read_as_string(filename, Some(template))
.map_err(|err| SubplotError::FunctionsFileNotFound(filename.into(), err))?;
funcs.push(Func::new(filename, content));
}
@@ -51,7 +54,7 @@ fn context(doc: &mut Document) -> Result<Context, SubplotError> {
Ok(context)
}
-fn tera(tmplspec: &TemplateSpec) -> Result<Tera, SubplotError> {
+fn tera(tmplspec: &TemplateSpec, templatename: &str) -> Result<Tera, SubplotError> {
// Tera insists on a glob, but we want to load a specific template
// only, so we use a glob that doesn't match anything.
let mut tera = Tera::new("/..IGNORE-THIS../..SUBPLOT-TERA-NOT-EXIST../*").expect("new");
@@ -61,12 +64,12 @@ fn tera(tmplspec: &TemplateSpec) -> Result<Tera, SubplotError> {
let dirname = tmplspec.template_filename().parent().unwrap();
for helper in tmplspec.helpers() {
let helper_path = dirname.join(helper);
- let helper_content = resource::read_as_string(helper_path)?;
+ let helper_content = resource::read_as_string(helper_path, Some(templatename))?;
let helper_name = helper.display().to_string();
tera.add_raw_template(&helper_name, &helper_content)
.map_err(|err| SubplotError::TemplateError(helper_name.to_string(), err))?;
}
- let template = resource::read_as_string(tmplspec.template_filename())?;
+ let template = resource::read_as_string(tmplspec.template_filename(), Some(templatename))?;
tera.add_raw_template("template", &template)
.map_err(|err| {
SubplotError::TemplateError(tmplspec.template_filename().display().to_string(), err)
diff --git a/src/metadata.rs b/src/metadata.rs
index be15380..3fd2f08 100644
--- a/src/metadata.rs
+++ b/src/metadata.rs
@@ -1,4 +1,4 @@
-use crate::{resource, Result};
+use crate::Result;
use crate::{Bindings, TemplateSpec};
use std::fmt::Debug;
@@ -48,7 +48,6 @@ impl Metadata {
"Loaded basic metadata"
);
let (template, spec) = if let Some((template, spec)) = get_template_spec(&doc.meta)? {
- resource::set_template(&template);
(Some(template), Some(spec))
} else {
(None, None)
@@ -56,7 +55,7 @@ impl Metadata {
event!(Level::TRACE, ?template, ?spec, "Loaded template spec");
let mut bindings = Bindings::new();
- get_bindings(&bindings_filenames, &mut bindings)?;
+ get_bindings(&bindings_filenames, &mut bindings, template.as_deref())?;
event!(Level::TRACE, "Loaded all metadata successfully");
Ok(Metadata {
title,
@@ -294,12 +293,12 @@ mod test_join {
}
}
-fn get_bindings<P>(filenames: &[P], bindings: &mut Bindings) -> Result<()>
+fn get_bindings<P>(filenames: &[P], bindings: &mut Bindings, template: Option<&str>) -> Result<()>
where
P: AsRef<Path> + Debug,
{
for filename in filenames {
- bindings.add_from_file(filename)?;
+ bindings.add_from_file(filename, template)?;
}
Ok(())
}
diff --git a/src/resource.rs b/src/resource.rs
index 0149cd7..bd6fc34 100644
--- a/src/resource.rs
+++ b/src/resource.rs
@@ -45,7 +45,6 @@ lazy_static! {
let ret = Vec::new();
Mutex::new(ret)
};
- static ref TEMPLATE_NAME: Mutex<Option<String>> = Mutex::new(None);
}
static EMBEDDED_FILES: &[(&str, &[u8])] = include!(concat!(env!("OUT_DIR"), "/embedded_files.inc"));
@@ -63,11 +62,6 @@ fn add_search_path<P: AsRef<Path>>(path: P) {
.push(path.as_ref().into());
}
-/// Set the template name, for use in searching for content...
-pub fn set_template(template: &str) {
- *TEMPLATE_NAME.lock().expect("Unable to lock TEMPLATE_NAME") = Some(template.to_string());
-}
-
/// Open a file for reading, honouring search paths established during
/// startup, and falling back to potentially embedded file content.
///
@@ -79,13 +73,12 @@ pub fn set_template(template: &str) {
///
/// Then we repeat all the above, inserting the template name in the subpath
/// too.
-fn open<P: AsRef<Path>>(subpath: P) -> io::Result<Box<dyn Read>> {
+fn open<P: AsRef<Path>>(subpath: P, template: Option<&str>) -> io::Result<Box<dyn Read>> {
let subpath = subpath.as_ref();
match internal_open(subpath) {
Ok(r) => Ok(r),
Err(e) => {
- let template = TEMPLATE_NAME.lock().expect("Unable to lock TEMPLATE_NAME");
- if let Some(templ) = template.as_deref() {
+ if let Some(templ) = template {
let subpath = Path::new(templ).join(subpath);
match internal_open(&subpath) {
Ok(r) => Ok(r),
@@ -136,8 +129,8 @@ fn internal_open(subpath: &Path) -> io::Result<Box<dyn Read>> {
/// Read a file, honouring search paths established during startup, and
/// falling back to potentially embedded file content
-pub fn read_as_string<P: AsRef<Path>>(subpath: P) -> io::Result<String> {
- let mut f = open(subpath)?;
+pub fn read_as_string<P: AsRef<Path>>(subpath: P, template: Option<&str>) -> io::Result<String> {
+ let mut f = open(subpath, template)?;
let mut ret = String::with_capacity(8192);
f.read_to_string(&mut ret)?;
Ok(ret)
diff --git a/src/templatespec.rs b/src/templatespec.rs
index b64d282..5d7150b 100644
--- a/src/templatespec.rs
+++ b/src/templatespec.rs
@@ -41,7 +41,7 @@ impl TemplateSpec {
/// Read a template.yaml file and create the corresponding TemplateSpec.
pub fn from_file(filename: &Path) -> Result<TemplateSpec> {
- let yaml = resource::read_as_string(filename)?;
+ let yaml = resource::read_as_string(filename, None)?;
let spec = TemplateSpec::from_yaml(&yaml)?;
let dirname = match filename.parent() {
Some(x) => x,