From 9ed32d57ef7e63c4d2d6fa21ca4f07d488e967da Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 14 Apr 2021 09:45:41 +0300 Subject: refactor: don't use anyhow in the library part of the crate Add one variant to the SubplotError enum and change src/codegen.rs to return Result<_, SubplotError> instead of anyhow::Result<_>. --- src/codegen.rs | 23 ++++++++++++----------- src/error.rs | 6 +++++- 2 files changed, 17 insertions(+), 12 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 { +pub fn template_spec(doc: &Document) -> Result { let template = doc .meta() .template_name() @@ -22,7 +20,6 @@ pub fn template_spec(doc: &Document) -> Result { 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 { +fn context(doc: &mut Document) -> Result { 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 { 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 { Ok(context) } -fn tera(tmplspec: &TemplateSpec) -> Result { +fn tera(tmplspec: &TemplateSpec) -> Result { // 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 { 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(()) diff --git a/src/error.rs b/src/error.rs index 73cfbb6..eb70e28 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,7 +12,7 @@ pub enum SubplotError { /// Subplot could not find a file named as a functions file. #[error("functions file could not be found: {0}: {1}")] - FunctionsFileNotFound(PathBuf, anyhow::Error), + FunctionsFileNotFound(PathBuf, std::io::Error), /// The simple pattern specifies a kind that is unknown. #[error("simple pattern kind {0} is unknown")] @@ -204,6 +204,10 @@ pub enum SubplotError { #[error("Couldn't find name of directory containing template spec: {0}")] NoTemplateSpecDirectory(PathBuf), + /// A code template has an error. + #[error("Couldn't load template {0}: {1}")] + TemplateError(String, tera::Error), + /// Unknown classes in use in document #[error("Unknown classes found in the document: {0}")] UnknownClasses(String), -- cgit v1.2.1