diff options
Diffstat (limited to 'src/doc.rs')
-rw-r--r-- | src/doc.rs | 56 |
1 files changed, 35 insertions, 21 deletions
@@ -11,8 +11,8 @@ use crate::PartialStep; use crate::Scenario; use crate::ScenarioStep; use crate::Style; +use crate::SubplotError; use crate::{bindings::CaptureType, parser::parse_scenario_snippet}; -use crate::{Result, SubplotError}; use crate::{Warning, Warnings}; use std::collections::HashSet; @@ -116,7 +116,7 @@ impl<'a> Document { mut ast: Pandoc, style: Style, template: Option<&str>, - ) -> Result<Document> + ) -> Result<Document, SubplotError> where P: AsRef<Path> + Debug, { @@ -141,7 +141,7 @@ impl<'a> Document { json: &str, style: Style, template: Option<&str>, - ) -> Result<Document> + ) -> Result<Document, SubplotError> where P: AsRef<Path> + Debug, { @@ -160,7 +160,7 @@ impl<'a> Document { filename: &Path, style: Style, template: Option<&str>, - ) -> Result<Document> { + ) -> Result<Document, SubplotError> { trace!( "Document::from_file: basedir={} filename={}", basedir.display(), @@ -201,7 +201,7 @@ impl<'a> Document { filename: &Path, style: Style, template: Option<&str>, - ) -> Result<Document> { + ) -> Result<Document, SubplotError> { trace!("Parsing document with pullmark-cmark from {:?}", filename); let filename = filename.to_path_buf(); let markdown = std::fs::read_to_string(&filename)?; @@ -215,7 +215,7 @@ impl<'a> Document { /// /// This is useful in a Pandoc filter, so that the filter can give /// it back to Pandoc for typesetting. - pub fn ast(&self) -> Result<String> { + pub fn ast(&self) -> Result<String, SubplotError> { let json = serde_json::to_string(&self.ast)?; Ok(json) } @@ -275,7 +275,7 @@ impl<'a> Document { } /// Check the document for common problems. - pub fn lint(&self) -> Result<()> { + pub fn lint(&self) -> Result<(), SubplotError> { trace!("Linting document"); self.check_doc_has_title()?; self.check_filenames_are_unique()?; @@ -285,7 +285,7 @@ impl<'a> Document { } // Check that all filenames for embedded files are unique. - fn check_filenames_are_unique(&self) -> Result<()> { + fn check_filenames_are_unique(&self) -> Result<(), SubplotError> { let mut known = HashSet::new(); for filename in self.files().iter().map(|f| f.filename().to_lowercase()) { if known.contains(&filename) { @@ -297,7 +297,7 @@ impl<'a> Document { } // Check that document has a title in its metadata. - fn check_doc_has_title(&self) -> Result<()> { + fn check_doc_has_title(&self) -> Result<(), SubplotError> { if self.meta().title().is_empty() { Err(SubplotError::NoTitle) } else { @@ -306,7 +306,7 @@ impl<'a> Document { } /// Check that all the block classes in the document are known - fn check_block_classes(&self) -> Result<()> { + fn check_block_classes(&self) -> Result<(), SubplotError> { let mut visitor = visitor::BlockClassVisitor::default(); // Irritatingly we can't immutably visit the AST for some reason // This clone() is expensive and unwanted, but I'm not sure how @@ -339,7 +339,7 @@ impl<'a> Document { /// Check that all named files (in matched steps) are actually present in the /// document. - pub fn check_named_files_exist(&mut self, template: &str) -> Result<bool> { + pub fn check_named_files_exist(&mut self, template: &str) -> Result<bool, SubplotError> { let filenames: HashSet<_> = self .files() .iter() @@ -372,7 +372,7 @@ impl<'a> Document { } /// Check that all embedded files are used by matched steps. - pub fn check_embedded_files_are_used(&mut self, template: &str) -> Result<bool> { + pub fn check_embedded_files_are_used(&mut self, template: &str) -> Result<bool, SubplotError> { let mut filenames: HashSet<_> = self .files() .iter() @@ -440,7 +440,7 @@ impl<'a> Document { } /// Return all scenarios in a document. - pub fn scenarios(&mut self) -> Result<Vec<Scenario>> { + pub fn scenarios(&mut self) -> Result<Vec<Scenario>, SubplotError> { let mut visitor = visitor::StructureVisitor::new(); visitor.walk_pandoc(&mut self.ast); @@ -458,7 +458,10 @@ impl<'a> Document { } /// Return matched scenarios in a document. - pub fn matched_scenarios(&mut self, template: &str) -> Result<Vec<MatchedScenario>> { + pub fn matched_scenarios( + &mut self, + template: &str, + ) -> Result<Vec<MatchedScenario>, SubplotError> { let scenarios = self.scenarios()?; trace!( "Found {} scenarios, checking their bindings", @@ -472,7 +475,7 @@ impl<'a> Document { } /// Extract a template name from this document - pub fn template(&self) -> Result<&str> { + pub fn template(&self) -> Result<&str, SubplotError> { let templates: Vec<_> = self.meta().templates().collect(); if templates.len() == 1 { Ok(templates[0]) @@ -487,7 +490,11 @@ impl<'a> Document { /// Load a `Document` from a file. /// /// This version uses Pandoc to parse the Markdown. -pub fn load_document<P>(filename: P, style: Style, template: Option<&str>) -> Result<Document> +pub fn load_document<P>( + filename: P, + style: Style, + template: Option<&str>, +) -> Result<Document, SubplotError> where P: AsRef<Path> + Debug, { @@ -512,7 +519,7 @@ pub fn load_document_with_pullmark<P>( filename: P, style: Style, template: Option<&str>, -) -> Result<Document> +) -> Result<Document, SubplotError> where P: AsRef<Path> + Debug, { @@ -531,7 +538,11 @@ where } /// Generate code for one document. -pub fn codegen(filename: &Path, output: &Path, template: Option<&str>) -> Result<CodegenOutput> { +pub fn codegen( + filename: &Path, + output: &Path, + template: Option<&str>, +) -> Result<CodegenOutput, SubplotError> { let r = load_document_with_pullmark(filename, Style::default(), template); let mut doc = match r { Ok(doc) => doc, @@ -575,7 +586,7 @@ impl CodegenOutput { } } -fn extract_scenario(e: &[visitor::Element]) -> Result<(Option<Scenario>, usize)> { +fn extract_scenario(e: &[visitor::Element]) -> Result<(Option<Scenario>, usize), SubplotError> { if e.is_empty() { // If we get here, it's a programming error. panic!("didn't expect empty list of elements"); @@ -623,7 +634,6 @@ fn extract_scenario(e: &[visitor::Element]) -> Result<(Option<Scenario>, usize)> mod test_extract { use super::extract_scenario; use super::visitor::Element; - use crate::Result; use crate::Scenario; use crate::SubplotError; @@ -635,7 +645,11 @@ mod test_extract { Element::Snippet(text.to_string()) } - fn check_result(r: Result<(Option<Scenario>, usize)>, title: Option<&str>, i: usize) { + fn check_result( + r: Result<(Option<Scenario>, usize), SubplotError>, + title: Option<&str>, + i: usize, + ) { assert!(r.is_ok()); let (actual_scen, actual_i) = r.unwrap(); if title.is_none() { |