From 95bc7f84bbe909c69783f9ae379af2230ddc0739 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 24 May 2020 19:46:20 +0300 Subject: fix: report name of missing bindings or functions file in error msg --- src/bindings.rs | 3 ++- src/codegen.rs | 6 ++++-- src/error.rs | 8 ++++++++ subplot.md | 40 ++++++++++++++++++++++++++++++++++++++++ 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, { - 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 { 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 -- cgit v1.2.1