diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-04-02 09:50:39 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-04-10 15:57:57 +0300 |
commit | 85212e8ddc54c6ede71912127eaf84aac403f10d (patch) | |
tree | 3d6a86f55231ace670ecdf1a6b42b237db2632f8 /src/bin/subplot.rs | |
parent | a3d768162e70004d207a40d803f5d17eeea8593a (diff) | |
download | subplot-85212e8ddc54c6ede71912127eaf84aac403f10d.tar.gz |
feat! treat warnings as errors by default
Add a --merciful option to subcommands for which this matters. Adjust
tests to invoke subplot with that option as necessary.
Sponsored-by: author
Diffstat (limited to 'src/bin/subplot.rs')
-rw-r--r-- | src/bin/subplot.rs | 81 |
1 files changed, 54 insertions, 27 deletions
diff --git a/src/bin/subplot.rs b/src/bin/subplot.rs index 2ad2df1..6d098f3 100644 --- a/src/bin/subplot.rs +++ b/src/bin/subplot.rs @@ -6,8 +6,7 @@ use anyhow::Result; use log::{debug, trace}; use structopt::StructOpt; use subplot::{ - codegen, load_document, load_document_with_pullmark, resource, DataFile, Document, MarkupOpts, - Style, + codegen, load_document, resource, DataFile, Document, MarkupOpts, Style, SubplotError, }; use time::{format_description::FormatItem, macros::format_description, OffsetDateTime}; @@ -134,6 +133,10 @@ impl Resources { /// extract all embedded files. if the output directory /// is not specified then this will extract to the current directory. struct Extract { + /// Allow warnings in document? + #[structopt(long)] + merciful: bool, + /// Directory to write extracted files to #[structopt( name = "DIR", @@ -162,7 +165,7 @@ impl Extract { } fn run(&self) -> Result<()> { - let doc = load_document(&self.filename, Style::default(), None)?; + let doc = load_linted_doc(&self.filename, Style::default(), &None, self.merciful)?; let files: Vec<&DataFile> = if self.embedded.is_empty() { doc.files() @@ -249,6 +252,10 @@ impl Filter { /// document. This can then render that either as a plain text report for humans, /// or as a JSON object for further scripted processing. struct Metadata { + /// Allow warnings in document? + #[structopt(long)] + merciful: bool, + /// Form that you want the output to take #[structopt(short = "o", default_value = "plain", possible_values=&["plain", "json"])] output_format: cli::OutputFormat, @@ -264,7 +271,7 @@ impl Metadata { } fn run(&self) -> Result<()> { - let mut doc = load_document_with_pullmark(&self.filename, Style::default(), None)?; + let mut doc = load_linted_doc(&self.filename, Style::default(), &None, self.merciful)?; let meta = cli::Metadata::try_from(&mut doc)?; match self.output_format { cli::OutputFormat::Plain => meta.write_out(), @@ -279,6 +286,10 @@ impl Metadata { /// /// Process a subplot document and typeset it using Pandoc. struct Docgen { + /// Allow warnings in document? + #[structopt(long)] + merciful: bool, + /// The template to use from the document. /// /// If not specified, subplot will try and find a unique template name from the document @@ -309,26 +320,7 @@ impl Docgen { trace!("PDF output chosen"); style.typeset_links_as_notes(); } - let mut doc = load_document(&self.input, style, None)?; - trace!("Got doc, now linting it"); - doc.lint()?; - trace!("Doc linted ok"); - let meta = doc.meta(); - trace!("Looking for template, meta={:#?}", meta); - let template = self - .template - .as_deref() - .map(Ok) - .unwrap_or_else(|| doc.template()) - .unwrap_or(""); - let template = template.to_string(); - trace!("Template: {:#?}", template); - if !doc.check_named_files_exist(&template)? - || !doc.check_matched_steps_have_impl(&template) - || !doc.check_embedded_files_are_used(&template)? - { - eprintln!("Continuing despite warnings"); - } + let mut doc = load_linted_doc(&self.input, style, &self.template, self.merciful)?; let mut pandoc = pandoc::new(); // Metadata date from command line or file mtime. However, we @@ -348,7 +340,7 @@ impl Docgen { pandoc.add_option(pandoc::PandocOption::Standalone); pandoc.add_option(pandoc::PandocOption::NumberSections); - if Self::need_output(&mut doc, &template, &self.output) { + if Self::need_output(&mut doc, &self.template, &self.output) { doc.typeset(); pandoc.set_input_format(pandoc::InputFormat::Json, vec![]); pandoc.set_input(pandoc::InputKind::Pipe(doc.ast()?)); @@ -374,13 +366,13 @@ impl Docgen { time.format(DATE_FORMAT).unwrap() } - fn need_output(doc: &mut subplot::Document, template: &str, output: &Path) -> bool { + fn need_output(doc: &mut subplot::Document, template: &Option<String>, output: &Path) -> bool { let output = match Self::mtime(output) { Err(_) => return true, Ok(ts) => ts, }; - for filename in doc.sources(Some(template)) { + for filename in doc.sources(template) { let source = match Self::mtime(&filename) { Err(_) => return true, Ok(ts) => ts, @@ -458,6 +450,41 @@ impl Codegen { } } +fn load_linted_doc( + filename: &Path, + style: Style, + template: &Option<String>, + merciful: bool, +) -> Result<Document, SubplotError> { + let mut doc = load_document(filename, style, None)?; + trace!("Got doc, now linting it"); + doc.lint()?; + trace!("Doc linted ok"); + + let meta = doc.meta(); + trace!("Looking for template, meta={:#?}", meta); + let template = template + .as_deref() + .map(Ok) + .unwrap_or_else(|| doc.template()) + .unwrap_or(""); + let template = template.to_string(); + trace!("Template: {:#?}", template); + doc.check_named_files_exist(&template)?; + doc.check_matched_steps_have_impl(&template); + doc.check_embedded_files_are_used(&template)?; + + for w in doc.warnings() { + eprintln!("WARNING: {}", w); + } + + if !doc.warnings().is_empty() && !merciful { + return Err(SubplotError::Warnings(doc.warnings().len())); + } + + Ok(doc) +} + fn real_main() { trace!("Starting Subplot"); let argparser = Toplevel::clap(); |