Skip to content

Commit

Permalink
Improving template naming scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbor committed Jul 20, 2024
1 parent 5ae0e62 commit ed99f9d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub enum ZinnError {

#[error("Unable to change directory")]
ChdirError(),

#[error("Colon (':') in template name (\"{0}\")")]
ColonInTemplateName(String),
}

pub fn die(e: impl Into<ZinnError>) -> ! {
Expand Down
36 changes: 16 additions & 20 deletions src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use serde::{Deserialize, Serialize};
use crate::barkeeper::ThreadStateTracker;
use crate::error::*;
use crate::queue::JobState;
use crate::render_component;
use crate::Options;
use crate::TemplateType;


/// Template for a job as described in the Zinnfile
Expand Down Expand Up @@ -119,35 +119,40 @@ impl JobDescription {
// render input files
let mut inputs = Vec::new();
if let Some(input_str) = &self.inputs {
let rendered_input_str = Self::render_component(TemplateType::Inputs, &["", "", &name], input_str, handlebars, &combined_vars)?;
let template_path = ["jobs", &name, "inputs"];
let rendered_input_str = render_component(&template_path, input_str, handlebars, &combined_vars)?;
let additional_inputs = rendered_input_str.split(char::is_whitespace)
.filter(|v| !v.is_empty())
.map(|s| s.to_owned());
inputs.extend(additional_inputs)
}
for input in &self.input_list {
let rendered = Self::render_component(TemplateType::InputListElem, &[input, "", &name], input, handlebars, &combined_vars)?;
for (i, input) in self.input_list.iter().enumerate() {
let template_path = ["jobs", &name, "input-list", &i.to_string()];
let rendered = render_component(&template_path, input, handlebars, &combined_vars)?;
inputs.push(rendered);
}

// render output files
let mut outputs = Vec::new();
if let Some(output_str) = &self.outputs {
let rendered_output_str = Self::render_component(TemplateType::Outputs, &["", "", &name], output_str, handlebars, &combined_vars)?;
let template_path = ["jobs", &name, "outputs"];
let rendered_output_str = render_component(&template_path, output_str, handlebars, &combined_vars)?;
let additional_outputs = rendered_output_str .split(char::is_whitespace)
.filter(|v| !v.is_empty())
.map(|s| s.to_owned());
outputs.extend(additional_outputs)
}
for (i, output) in self.output_list.iter().enumerate() {
let rendered = Self::render_component(TemplateType::OutputListElem, &[&i.to_string(), "", &name], output, handlebars, &combined_vars)?;
let template_path = ["jobs", &name, "output-list", &i.to_string()];
let rendered = render_component(&template_path, output, handlebars, &combined_vars)?;
outputs.push(rendered);
}

for (i, dep) in self.requires.iter().enumerate() {
let mut realized_dep_params = dep.with.clone();
for (key, val) in &mut realized_dep_params {
*val = Self::render_component(TemplateType::DependencyParam, &[&i.to_string(), key, &name], val, handlebars, &combined_vars)?;
let template_path = ["jobs", &name, "requires", &i.to_string(), key];
*val = render_component(&template_path, val, handlebars, &combined_vars)?;
}

let dep_desc = match job_descriptions.get(&dep.job) {
Expand All @@ -156,7 +161,8 @@ impl JobDescription {
};

if let Some(with_list) = &dep.foreach {
let inputs = Self::render_component(TemplateType::ForEach, &[&i.to_string(), "", &name], &with_list.r#in, handlebars, &combined_vars)?;
let template_path = ["jobs", &name, "requires", &i.to_string(), "foreach"];
let inputs = render_component(&template_path, &with_list.r#in, handlebars, &combined_vars)?;
let val_list = inputs.split(char::is_whitespace)
.filter(|v| !v.is_empty());
for val in val_list {
Expand All @@ -172,7 +178,8 @@ impl JobDescription {
}
}

let run = Self::render_component(TemplateType::Run, &["", "", &name], &self.run, handlebars, &combined_vars)?;
let template_path = ["jobs", &name, "run"];
let run = render_component(&template_path, &self.run, handlebars, &combined_vars)?;
let name = name.replace('\n', "");
let interactive = self.interactive;

Expand All @@ -185,17 +192,6 @@ impl JobDescription {
&self.args
}

fn render_component(tt: TemplateType, suffix: &[&str; 3], template: &str, handlebars: &mut Handlebars, context: &HashMap<String, String>) -> ZinnResult<String> {
let template_name = tt.to_name(suffix);
match handlebars.get_template(&template_name) {
Some(_) => Ok(handlebars.render(&template_name, context)?),
None => {
let template = handlebars::Template::compile_with_name(template, template_name.clone())?;
handlebars.register_template(&template_name, template);
Ok(handlebars.render(&template_name, context)?)
},
}
}
}

impl InnerJobRealization {
Expand Down
39 changes: 15 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,21 @@ impl Args {
}
}

#[derive(Clone, Copy, Debug)]
enum TemplateType {
Inputs,
InputListElem,
Outputs,
OutputListElem,
DependencyParam,
ForEach,
Run,
}
fn render_component(path: &[&str], template: &str, handlebars: &mut Handlebars, context: &HashMap<String, String>) -> ZinnResult<String> {
assert!(!path.is_empty());

if let Some(name) = path.iter().find(|c| c.contains(':')) {
return Err(ZinnError::ColonInTemplateName(name.to_string()));
}

impl TemplateType {
fn to_name(self, suffix: &[&str; 3]) -> String {
use TemplateType::*;
// prefixes must not be the same or similar
match self {
Inputs => format!("inputs-{}-{}-{}", suffix[0], suffix[1], suffix[2]),
InputListElem => format!("input-file-{}-{}-{}", suffix[0], suffix[1], suffix[2]),
Outputs => format!("outputs-{}-{}-{}", suffix[0], suffix[1], suffix[2]),
OutputListElem => format!("output-file-{}-{}-{}", suffix[0], suffix[1], suffix[2]),
DependencyParam => format!("dependency-param-{}-{}-{}", suffix[0], suffix[1], suffix[2]),
ForEach => format!("dependency-foreach-{}-{}-{}", suffix[0], suffix[1], suffix[2]),
Run => format!("run-{}-{}-{}", suffix[0], suffix[1], suffix[2]),
}
let template_name = path.join(":");
match handlebars.get_template(&template_name) {
Some(_) => Ok(handlebars.render(&template_name, context)?),
None => {
let template = handlebars::Template::compile_with_name(template, template_name.clone())?;
handlebars.register_template(&template_name, template);
Ok(handlebars.render(&template_name, context)?)
},
}
}

Expand Down Expand Up @@ -297,7 +287,8 @@ fn main() {
// parse constants
let mut constants = HashMap::new();
for (name, value) in &zinnfile.constants {
let realized = resolve(handlebars.render_template(value, &constants));
let template_path = ["constants", name];
let realized = resolve(render_component(&template_path, value, &mut handlebars, &constants));
constants.insert(name.to_owned(), realized);
}

Expand Down

0 comments on commit ed99f9d

Please sign in to comment.