diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-03-27 13:15:27 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-04-09 16:42:49 +0100 |
commit | e093e297f39e2c8602ae5ac5f60ffad1158c78a0 (patch) | |
tree | 1225350bf4c3c1639863fc411772548b834f2a1d | |
parent | f49c399f1dd8c391c756776b31f13cd5473bc885 (diff) | |
download | subplot-e093e297f39e2c8602ae5ac5f60ffad1158c78a0.tar.gz |
bin: Add metadata subcommand to subplot
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
-rw-r--r-- | src/bin/subplot.rs | 31 |
1 files changed, 31 insertions, 0 deletions
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(); |