diff options
Diffstat (limited to 'src/codegen.rs')
-rw-r--r-- | src/codegen.rs | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/src/codegen.rs b/src/codegen.rs index 588fe1b..efb4081 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -9,10 +9,8 @@ use base64::encode; use serde::Serialize; use tera::{Context, Tera, Value}; -use anyhow::{Context as AnyhowContext, Result}; - /// Return the requested template specification. -pub fn template_spec(doc: &Document) -> Result<TemplateSpec> { +pub fn template_spec(doc: &Document) -> Result<TemplateSpec, SubplotError> { let template = doc .meta() .template_name() @@ -22,7 +20,6 @@ pub fn template_spec(doc: &Document) -> Result<TemplateSpec> { filename.push("template"); filename.push("template.yaml"); TemplateSpec::from_file(&filename) - .with_context(|| format!("Failed to read template file: {}", filename.display())) } /// Generate a test program from a document, using a template spec. @@ -30,14 +27,14 @@ pub fn generate_test_program( doc: &mut Document, spec: &TemplateSpec, filename: &Path, -) -> Result<()> { +) -> Result<(), SubplotError> { let context = context(doc)?; let code = tera(&spec)?.render("template", &context).expect("render"); write(filename, &code)?; Ok(()) } -fn context(doc: &mut Document) -> Result<Context> { +fn context(doc: &mut Document) -> Result<Context, SubplotError> { let mut context = Context::new(); context.insert("scenarios", &doc.matched_scenarios()?); context.insert("files", doc.files()); @@ -46,7 +43,7 @@ fn context(doc: &mut Document) -> Result<Context> { let mut funcs = vec![]; for filename in funcs_filenames { let content = resource::read_as_string(filename) - .map_err(|e| SubplotError::FunctionsFileNotFound(filename.into(), e.into()))?; + .map_err(|err| SubplotError::FunctionsFileNotFound(filename.into(), err))?; funcs.push(Func::new(filename, content)); } context.insert("functions", &funcs); @@ -54,7 +51,7 @@ fn context(doc: &mut Document) -> Result<Context> { Ok(context) } -fn tera(tmplspec: &TemplateSpec) -> Result<Tera> { +fn tera(tmplspec: &TemplateSpec) -> 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"); @@ -66,14 +63,18 @@ fn tera(tmplspec: &TemplateSpec) -> Result<Tera> { let helper_path = dirname.join(helper); let helper_content = resource::read_as_string(helper_path)?; let helper_name = helper.display().to_string(); - tera.add_raw_template(&helper_name, &helper_content)?; + 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())?; - tera.add_raw_template("template", &template)?; + tera.add_raw_template("template", &template) + .map_err(|err| { + SubplotError::TemplateError(tmplspec.template_filename().display().to_string(), err) + })?; Ok(tera) } -fn write(filename: &Path, content: &str) -> Result<()> { +fn write(filename: &Path, content: &str) -> Result<(), SubplotError> { let mut f: File = File::create(filename)?; f.write_all(&content.as_bytes())?; Ok(()) |