summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-05-24 19:46:20 +0300
committerLars Wirzenius <liw@liw.fi>2020-06-06 17:41:39 +0300
commit95bc7f84bbe909c69783f9ae379af2230ddc0739 (patch)
tree770947657121fe33a4786cf9c8d14262747a3a2c
parent7061fe6957774c80c8deedd2df80416aa7128bd7 (diff)
downloadsubplot-95bc7f84bbe909c69783f9ae379af2230ddc0739.tar.gz
fix: report name of missing bindings or functions file in error msg
-rw-r--r--src/bindings.rs3
-rw-r--r--src/codegen.rs6
-rw-r--r--src/error.rs8
-rw-r--r--subplot.md40
4 files changed, 54 insertions, 3 deletions
diff --git a/src/bindings.rs b/src/bindings.rs
index 9c0df9e..4233528 100644
--- a/src/bindings.rs
+++ b/src/bindings.rs
@@ -316,7 +316,8 @@ impl Bindings {
where
P: AsRef<Path>,
{
- let mut f = File::open(filename)?;
+ let mut f = File::open(filename.as_ref())
+ .map_err(|e| SubplotError::BindingsFileNotFound(filename.as_ref().into(), e))?;
let mut yaml = String::new();
f.read_to_string(&mut yaml)?;
self.add_from_yaml(&yaml)?;
diff --git a/src/codegen.rs b/src/codegen.rs
index db31765..9895783 100644
--- a/src/codegen.rs
+++ b/src/codegen.rs
@@ -1,4 +1,4 @@
-use crate::{Document, TemplateSpec};
+use crate::{Document, SubplotError, TemplateSpec};
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::{Read, Write};
@@ -44,7 +44,9 @@ fn context(doc: &mut Document) -> Result<Context> {
let funcs_filenames = doc.meta().functions_filenames();
let mut funcs = String::new();
for filename in funcs_filenames {
- funcs.push_str(&cat(filename)?);
+ let content =
+ cat(filename).map_err(|e| SubplotError::FunctionsFileNotFound(filename.into(), e))?;
+ funcs.push_str(&content);
}
context.insert("functions", &funcs);
diff --git a/src/error.rs b/src/error.rs
index 0057f37..f988c86 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -6,6 +6,14 @@ use thiserror::Error;
/// Define all the kinds of errors any part of this crate can return.
#[derive(Debug, Error)]
pub enum SubplotError {
+ /// Subplot could not find a file named as a bindings file.
+ #[error("binding file could not be found: {0}: {1}")]
+ BindingsFileNotFound(PathBuf, std::io::Error),
+
+ /// Subplot could not find a file named as a functions file.
+ #[error("functions file could not be found: {0}: {1}")]
+ FunctionsFileNotFound(PathBuf, anyhow::Error),
+
/// The simple pattern contains a stray { or }.
#[error("simple pattern contains a stray {{ or }}")]
StrayBraceInSimplePattern(String),
diff --git a/subplot.md b/subplot.md
index bcd4eb9..b2cdecf 100644
--- a/subplot.md
+++ b/subplot.md
@@ -1458,6 +1458,46 @@ and file mtime.html contains "Geoffrey Butler"
and file mtime.html contains "2020-02-26 07:53"
~~~
+### Missing bindings file
+
+If a bindings file is missing, the error message should name the
+missing file.
+
+~~~scenario
+given file missing-binding.md
+when I try to run sp-docgen missing-binding.md -o foo.htmlh
+then exit code is non-zero
+and stderr matches /: missing-binding.yaml:/
+~~~
+
+~~~{#missing-binding.md .file .markdown .numberLines}
+---
+title: Missing binding
+bindings: missing-binding.yaml
+...
+~~~
+
+### Missing functions file
+
+If a functions file is missing, the error message should name the
+missing file.
+
+~~~scenario
+given file missing-functions.md
+and file b.yaml
+when I try to run sp-codegen --run missing-functions.md -o foo.py
+then exit code is non-zero
+and stderr matches /: missing-functions.py:/
+~~~
+
+~~~{#missing-functions.md .file .markdown .numberLines}
+---
+title: Missing functions
+bindings: b.yaml
+functions: missing-functions.py
+...
+~~~
+
### Extracting metadata from a document
The **sp-meta** program extracts metadata from a document. It is