From 67bd6febf2b8d229a217160829c201284f9e7503 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 10 Apr 2022 15:35:48 +0300 Subject: use Option<&str> instead of &Option Sponsored-by: author --- src/bin/cli/mod.rs | 2 +- src/bin/subplot.rs | 27 ++++++++++++++++----------- src/doc.rs | 2 +- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/bin/cli/mod.rs b/src/bin/cli/mod.rs index 2c43c39..a16df87 100644 --- a/src/bin/cli/mod.rs +++ b/src/bin/cli/mod.rs @@ -35,7 +35,7 @@ impl TryFrom<&mut Document> for Metadata { type Error = subplot::SubplotError; fn try_from(doc: &mut Document) -> std::result::Result { let mut sources: Vec<_> = doc - .sources(&None) + .sources(None) .into_iter() .map(|p| filename(Some(&p))) .collect(); diff --git a/src/bin/subplot.rs b/src/bin/subplot.rs index 6d098f3..6a41968 100644 --- a/src/bin/subplot.rs +++ b/src/bin/subplot.rs @@ -165,7 +165,7 @@ impl Extract { } fn run(&self) -> Result<()> { - let doc = load_linted_doc(&self.filename, Style::default(), &None, self.merciful)?; + let doc = load_linted_doc(&self.filename, Style::default(), None, self.merciful)?; let files: Vec<&DataFile> = if self.embedded.is_empty() { doc.files() @@ -271,7 +271,7 @@ impl Metadata { } fn run(&self) -> Result<()> { - let mut doc = load_linted_doc(&self.filename, Style::default(), &None, self.merciful)?; + 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(), @@ -320,7 +320,7 @@ impl Docgen { trace!("PDF output chosen"); style.typeset_links_as_notes(); } - let mut doc = load_linted_doc(&self.input, style, &self.template, self.merciful)?; + let mut doc = load_linted_doc(&self.input, style, self.template.as_deref(), self.merciful)?; let mut pandoc = pandoc::new(); // Metadata date from command line or file mtime. However, we @@ -340,7 +340,7 @@ impl Docgen { pandoc.add_option(pandoc::PandocOption::Standalone); pandoc.add_option(pandoc::PandocOption::NumberSections); - if Self::need_output(&mut doc, &self.template, &self.output) { + if Self::need_output(&mut doc, self.template.as_deref(), &self.output) { doc.typeset(); pandoc.set_input_format(pandoc::InputFormat::Json, vec![]); pandoc.set_input(pandoc::InputKind::Pipe(doc.ast()?)); @@ -366,7 +366,7 @@ impl Docgen { time.format(DATE_FORMAT).unwrap() } - fn need_output(doc: &mut subplot::Document, template: &Option, output: &Path) -> bool { + fn need_output(doc: &mut subplot::Document, template: Option<&str>, output: &Path) -> bool { let output = match Self::mtime(output) { Err(_) => return true, Ok(ts) => ts, @@ -453,7 +453,7 @@ impl Codegen { fn load_linted_doc( filename: &Path, style: Style, - template: &Option, + template: Option<&str>, merciful: bool, ) -> Result { let mut doc = load_document(filename, style, None)?; @@ -463,11 +463,16 @@ fn load_linted_doc( let meta = doc.meta(); trace!("Looking for template, meta={:#?}", meta); - let template = template - .as_deref() - .map(Ok) - .unwrap_or_else(|| doc.template()) - .unwrap_or(""); + + // Get template as given to use (from command line), or from + // document, or else default to the empty string. + let template = if let Some(t) = template { + t + } else if let Ok(t) = doc.template() { + t + } else { + "" + }; let template = template.to_string(); trace!("Template: {:#?}", template); doc.check_named_files_exist(&template)?; diff --git a/src/doc.rs b/src/doc.rs index bf06625..89b9eea 100644 --- a/src/doc.rs +++ b/src/doc.rs @@ -229,7 +229,7 @@ impl<'a> Document { /// /// The sources are any files that affect the output so that if /// the source file changes, the output needs to be re-generated. - pub fn sources(&mut self, template: &Option) -> Vec { + pub fn sources(&mut self, template: Option<&str>) -> Vec { let mut names = vec![]; for x in self.meta().bindings_filenames() { -- cgit v1.2.1