From e093e297f39e2c8602ae5ac5f60ffad1158c78a0 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 27 Mar 2021 13:15:27 +0000 Subject: bin: Add metadata subcommand to subplot Signed-off-by: Daniel Silverstone --- src/bin/subplot.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/bin/subplot.rs b/src/bin/subplot.rs index 94c4b53..fabb0f2 100644 --- a/src/bin/subplot.rs +++ b/src/bin/subplot.rs @@ -6,6 +6,7 @@ use anyhow::Result; use structopt::StructOpt; use subplot::{resource, DataFile, Document, Style}; +use std::convert::TryFrom; use std::fs::{write, File}; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; @@ -32,6 +33,7 @@ impl Toplevel { enum Cmd { Extract(Extract), Filter(Filter), + Metadata(Metadata), } impl Cmd { @@ -39,6 +41,7 @@ impl Cmd { match self { Cmd::Extract(e) => e.run(), Cmd::Filter(f) => f.run(), + Cmd::Metadata(m) => m.run(), } } } @@ -150,6 +153,34 @@ impl Filter { } } +#[derive(Debug, StructOpt)] +/// Extract metadata about a document +/// +/// Load and process a subplot document, extracting various metadata about the +/// 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 { + /// Form that you want the output to take + #[structopt(short = "o", default_value = "plain", possible_values=&["plain", "json"])] + output_format: cli::OutputFormat, + + /// Input subplot document filename + #[structopt(parse(from_os_str))] + filename: PathBuf, +} + +impl Metadata { + fn run(&self) -> Result<()> { + let mut doc = cli::load_document(&self.filename, Style::default())?; + let meta = cli::Metadata::try_from(&mut doc)?; + match self.output_format { + cli::OutputFormat::Plain => meta.write_out(), + cli::OutputFormat::Json => println!("{}", serde_json::to_string_pretty(&meta)?), + } + Ok(()) + } +} + fn main() { let args = Toplevel::from_args(); args.resources.handle(); -- cgit v1.2.1