diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-04-02 08:58:48 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-04-10 15:27:08 +0300 |
commit | a3d768162e70004d207a40d803f5d17eeea8593a (patch) | |
tree | 3f65bc109526f0dc69861cd0ee995e2ca5ccefea | |
parent | 68c09d2407b329ac03fcd9f25ba293a3a6201689 (diff) | |
download | subplot-a3d768162e70004d207a40d803f5d17eeea8593a.tar.gz |
refactor: collect warnings for a document in its Document
Change places where warnings used to be written out so the warnings
are pushed into the list in Document instead.
Sponsored-by: author
-rw-r--r-- | src/doc.rs | 23 | ||||
-rw-r--r-- | src/error.rs | 36 | ||||
-rw-r--r-- | src/lib.rs | 2 |
3 files changed, 49 insertions, 12 deletions
@@ -13,6 +13,7 @@ use crate::ScenarioStep; use crate::Style; use crate::{bindings::CaptureType, parser::parse_scenario_snippet}; use crate::{Result, SubplotError}; +use crate::{Warning, Warnings}; use std::collections::HashSet; use std::default::Default; @@ -83,6 +84,7 @@ pub struct Document { meta: Metadata, files: DataFiles, style: Style, + warnings: Warnings, } impl<'a> Document { @@ -99,9 +101,15 @@ impl<'a> Document { meta, files, style, + warnings: Warnings::default(), } } + /// Return all warnings about this document. + pub fn warnings(&self) -> &[Warning] { + self.warnings.warnings() + } + fn from_ast<P>( basedir: P, markdowns: Vec<PathBuf>, @@ -344,7 +352,7 @@ impl<'a> Document { if matches!(step.types().get(name.as_str()), Some(CaptureType::File)) && !filenames.contains(&text.to_lowercase()) { - eprintln!("Found reference to unknown file {}", text); + self.warnings.push(Warning::UnknownEmbeddedFile(text.to_string())); okay = false; } } @@ -378,10 +386,7 @@ impl<'a> Document { } } for filename in filenames.iter() { - eprintln!( - "WARNING: embedded file is not used by any scenario: {}", - filename - ); + self.warnings.push(Warning::UnusedEmbeddedFile(filename.to_string())); } // We always succeed. Subplot's own subplot had valid cases of @@ -402,16 +407,10 @@ impl<'a> Document { trace!("Found {} scenarios", scenarios.len()); for scenario in scenarios { trace!("Checking that steps in scenario"); - let mut said_scenario = false; for step in scenario.steps() { if step.function().is_none() { - if !said_scenario { - eprintln!("Scenario: '{}'", scenario.title()); - eprintln!(" Template: '{}'", template); - said_scenario = true; - } - eprintln!(" Step missing implementation: '{}'", step.text()); trace!("Missing step implementation: {:?}", step.text()); + self.warnings.push(Warning::MissingStepImplementation(step.text().to_string())); okay = false; } } diff --git a/src/error.rs b/src/error.rs index bcee4ff..31fca60 100644 --- a/src/error.rs +++ b/src/error.rs @@ -334,3 +334,39 @@ impl SubplotError { /// A result type for this crate. pub type Result<T> = std::result::Result<T, SubplotError>; + +/// A warning, or non-fatal error. +/// +/// Errors prevent Subplot from producing output. Warnings don't do that. +#[derive(Debug)] +pub enum Warning { + /// Document refers to an embedded file that doesn't exist. + UnknownEmbeddedFile(String), + + /// Embedded file is not used by any scenario. + UnusedEmbeddedFile(String), + + /// Missing step implementation. + MissingStepImplementation(String), +} + +/// A list of warnings. +/// +/// Subplot collects warnings into this structure so that they can be +/// processed at the end. +#[derive(Debug, Default)] +pub struct Warnings { + warnings: Vec<Warning>, +} + +impl Warnings { + /// Append a warning to the list. + pub fn push(&mut self, w: Warning) { + self.warnings.push(w); + } + + /// Return a slice with all the warnings in the list. + pub fn warnings(&self) -> &[Warning] { + &self.warnings + } +} @@ -16,6 +16,8 @@ extern crate pandoc_ast_08 as pandoc_ast; mod error; pub use error::Result; pub use error::SubplotError; +pub use error::Warning; +pub use error::Warnings; pub mod resource; |