summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2021-04-16 15:00:46 +0000
committerDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2021-04-16 15:00:46 +0000
commitec9652a72aa8ff47fa64b8a1b5ba9afe84be4f7e (patch)
tree78909e33d39a99dfd99360d6a29b35dd459d6c00
parent164666a8640d7a42949817f3d79c1f34b9e2ec24 (diff)
parent9ed32d57ef7e63c4d2d6fa21ca4f07d488e967da (diff)
downloadsubplot-ec9652a72aa8ff47fa64b8a1b5ba9afe84be4f7e.tar.gz
Merge branch 'nohow' into 'main'
refactor: don't use anyhow in the library part of the crate Closes #169 See merge request larswirzenius/subplot!149
-rw-r--r--src/codegen.rs23
-rw-r--r--src/error.rs6
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<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(())
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),