From a3d768162e70004d207a40d803f5d17eeea8593a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 2 Apr 2022 08:58:48 +0300 Subject: 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 --- src/doc.rs | 23 +++++++++++------------ src/error.rs | 36 ++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/doc.rs b/src/doc.rs index 2ff1484..d6ab697 100644 --- a/src/doc.rs +++ b/src/doc.rs @@ -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

( basedir: P, markdowns: Vec, @@ -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 = std::result::Result; + +/// 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, +} + +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 + } +} diff --git a/src/lib.rs b/src/lib.rs index 314ad2a..4bef83a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; -- cgit v1.2.1