From d11605662b06ab6756404fab006ff91c4a251cae Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 13 Feb 2021 10:44:58 +0000 Subject: CLI: Refactor out document loading Signed-off-by: Daniel Silverstone --- src/bin/cli/mod.rs | 14 ++++++++++++++ src/bin/sp-codegen.rs | 10 ++++------ src/bin/sp-docgen.rs | 7 ++++--- src/bin/sp-extract.rs | 6 +++--- src/bin/sp-meta.rs | 6 +++--- 5 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 src/bin/cli/mod.rs 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>(filename: P, style: Style) -> Result { + 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 { -- cgit v1.2.1