summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2021-02-13 10:44:58 +0000
committerDaniel Silverstone <dsilvers@digital-scurf.org>2021-04-09 16:42:49 +0100
commitd11605662b06ab6756404fab006ff91c4a251cae (patch)
tree3bc187182839ae93c80f6d69a2512135e2db42ce
parent24b3b38b5ef929f4c2697db490c87a3b4cc98c36 (diff)
downloadsubplot-d11605662b06ab6756404fab006ff91c4a251cae.tar.gz
CLI: Refactor out document loading
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
-rw-r--r--src/bin/cli/mod.rs14
-rw-r--r--src/bin/sp-codegen.rs10
-rw-r--r--src/bin/sp-docgen.rs7
-rw-r--r--src/bin/sp-extract.rs6
-rw-r--r--src/bin/sp-meta.rs6
5 files changed, 28 insertions, 15 deletions
diff --git a/src/bin/cli/mod.rs b/src/bin/cli/mod.rs
new file mode 100644
index 0000000..1bb5ea3
--- /dev/null
+++ b/src/bin/cli/mod.rs
@@ -0,0 +1,14 @@
+//! CLI Functionality abstractions
+
+use anyhow::Result;
+use subplot::{Document, Style};
+
+use std::path::Path;
+
+pub fn load_document<P: AsRef<Path>>(filename: P, style: Style) -> Result<Document> {
+ let filename = filename.as_ref();
+ let base_path = subplot::get_basedir_from(filename)?;
+ let doc = Document::from_file(&base_path, filename, style)?;
+
+ Ok(doc)
+}
diff --git a/src/bin/sp-codegen.rs b/src/bin/sp-codegen.rs
index f83cf59..c316bea 100644
--- a/src/bin/sp-codegen.rs
+++ b/src/bin/sp-codegen.rs
@@ -4,16 +4,14 @@ use std::process::Command;
use anyhow::Result;
use structopt::StructOpt;
-use subplot::{
- generate_test_program, resource, template_spec, Document, Style, SubplotError, TemplateSpec,
-};
+use subplot::{generate_test_program, resource, template_spec, Style, SubplotError, TemplateSpec};
+
+mod cli;
fn main() -> Result<()> {
let opt = Opt::from_args();
opt.resources.handle();
- let basedir = subplot::get_basedir_from(&opt.filename)?;
- let style = Style::default();
- let mut doc = Document::from_file(&basedir, &opt.filename, style)?;
+ let mut doc = cli::load_document(&opt.filename, Style::default())?;
doc.lint()?;
if !doc.check_named_files_exist()? {
eprintln!("Unable to continue");
diff --git a/src/bin/sp-docgen.rs b/src/bin/sp-docgen.rs
index 61522ce..d633791 100644
--- a/src/bin/sp-docgen.rs
+++ b/src/bin/sp-docgen.rs
@@ -8,7 +8,9 @@ use structopt::StructOpt;
use anyhow::Result;
-use subplot::{get_basedir_from, resource, Document, Style};
+use subplot::{resource, Style};
+
+mod cli;
// Define the command line arguments.
#[derive(Debug, StructOpt)]
@@ -42,8 +44,7 @@ fn main() -> Result<()> {
style.typeset_links_as_notes();
}
- let basedir = get_basedir_from(first_file)?;
- let mut doc = Document::from_file(&basedir, &first_file, style)?;
+ let mut doc = cli::load_document(&first_file, style)?;
doc.lint()?;
if !doc.check_named_files_exist()? {
eprintln!("Continuing despite warnings");
diff --git a/src/bin/sp-extract.rs b/src/bin/sp-extract.rs
index cdd9fba..9be159f 100644
--- a/src/bin/sp-extract.rs
+++ b/src/bin/sp-extract.rs
@@ -6,6 +6,8 @@ use structopt::StructOpt;
use subplot::{DataFile, Document, Style, SubplotError};
+mod cli;
+
#[derive(Debug, StructOpt)]
#[structopt(name = "sp-meta", about = "Show Subplot document metadata.")]
struct Opt {
@@ -24,9 +26,7 @@ struct Opt {
fn main() -> Result<()> {
let opt = Opt::from_args();
- let basedir = subplot::get_basedir_from(&opt.filename)?;
- let style = Style::default();
- let doc = Document::from_file(&basedir, &opt.filename, style)?;
+ let doc = cli::load_document(&opt.filename, Style::default())?;
for filename in opt.embedded {
let file = get_embedded(&doc, &filename)?;
diff --git a/src/bin/sp-meta.rs b/src/bin/sp-meta.rs
index c633c59..33e6ca4 100644
--- a/src/bin/sp-meta.rs
+++ b/src/bin/sp-meta.rs
@@ -8,6 +8,8 @@ use structopt::StructOpt;
use subplot::{Document, Style};
+mod cli;
+
#[derive(Debug)]
enum OutputFormat {
Plain,
@@ -115,9 +117,7 @@ impl Metadata {
fn main() -> Result<()> {
let opt = Opt::from_args();
- let basedir = subplot::get_basedir_from(&opt.filename)?;
- let style = Style::default();
- let mut doc = Document::from_file(&basedir, &opt.filename, style)?;
+ let mut doc = cli::load_document(&opt.filename, Style::default())?;
let meta = Metadata::try_from(&mut doc)?;
match opt.output_format {