summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-04-10 15:35:48 +0300
committerLars Wirzenius <liw@liw.fi>2022-04-10 15:58:26 +0300
commit67bd6febf2b8d229a217160829c201284f9e7503 (patch)
treede3f6d9f46b0706ad6796ad6ecdce9565a37b6c0
parent7f9a8727a1f8d7f66837322262ef24c64dc8ed5c (diff)
downloadsubplot-67bd6febf2b8d229a217160829c201284f9e7503.tar.gz
use Option<&str> instead of &Option<String>
Sponsored-by: author
-rw-r--r--src/bin/cli/mod.rs2
-rw-r--r--src/bin/subplot.rs27
-rw-r--r--src/doc.rs2
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<Self, Self::Error> {
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<String>, 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<String>,
+ template: Option<&str>,
merciful: bool,
) -> Result<Document, SubplotError> {
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<String>) -> Vec<PathBuf> {
+ pub fn sources(&mut self, template: Option<&str>) -> Vec<PathBuf> {
let mut names = vec![];
for x in self.meta().bindings_filenames() {