From 6959f556ccfa0ccfd409d36bfa4d40e004de0040 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Thu, 21 May 2020 17:18:54 +0100 Subject: refactor: make sp-meta structure based ready for json Signed-off-by: Daniel Silverstone --- src/bin/sp-meta.rs | 87 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 18 deletions(-) diff --git a/src/bin/sp-meta.rs b/src/bin/sp-meta.rs index cfa3b3d..cc4a7bd 100644 --- a/src/bin/sp-meta.rs +++ b/src/bin/sp-meta.rs @@ -1,5 +1,7 @@ use anyhow::Result; +use std::convert::TryFrom; use std::path::{Path, PathBuf}; + use structopt::StructOpt; use subplot::Document; @@ -12,32 +14,81 @@ struct Opt { filename: PathBuf, } -fn main() -> Result<()> { - let opt = Opt::from_args(); - let basedir = subplot::get_basedir_from(&opt.filename)?; - let mut doc = Document::from_file(&basedir, &opt.filename)?; +struct Metadata { + sources: Vec, + title: String, + binding_files: Vec, + function_files: Vec, + bibliographies: Vec, + scenarios: Vec, +} - for filename in doc.sources() { - println!("source: {}", filename.display()); +impl TryFrom<&mut Document> for Metadata { + type Error = subplot::SubplotError; + fn try_from(doc: &mut Document) -> std::result::Result { + let sources: Vec<_> = doc + .sources() + .into_iter() + .map(|p| filename(Some(&p))) + .collect(); + let title = doc.meta().title().to_owned(); + let binding_files = doc + .meta() + .bindings_filenames() + .into_iter() + .map(|p| filename(Some(&p))) + .collect(); + let function_files = doc + .meta() + .functions_filenames() + .into_iter() + .map(|p| filename(Some(&p))) + .collect(); + let bibliographies = doc + .meta() + .bibliographies() + .into_iter() + .map(|p| filename(Some(&p))) + .collect(); + let scenarios = doc + .scenarios()? + .into_iter() + .map(|s| s.title().to_owned()) + .collect(); + Ok(Self { + sources, + title, + binding_files, + function_files, + bibliographies, + scenarios, + }) } +} - println!("title: {}", doc.meta().title()); - - for filename in doc.meta().bindings_filenames() { - println!("bindings: {}", filename.display()); +impl Metadata { + fn write_list(v: &[String], prefix: &str) { + v.iter().for_each(|entry| println!("{}: {}", prefix, entry)) } - for filename in doc.meta().functions_filenames() { - println!("functions: {}", filename.display()); + fn write_out(&self) { + Self::write_list(&self.sources, "source"); + println!("title: {}", self.title); + Self::write_list(&self.binding_files, "bindings"); + Self::write_list(&self.function_files, "functions"); + Self::write_list(&self.bibliographies, "bibliography"); + Self::write_list(&self.scenarios, "scenario"); } +} - for bib in doc.meta().bibliographies().iter() { - println!("bibliography: {}", filename(Some(bib))); - } +fn main() -> Result<()> { + let opt = Opt::from_args(); + let basedir = subplot::get_basedir_from(&opt.filename)?; + let mut doc = Document::from_file(&basedir, &opt.filename)?; + let meta = Metadata::try_from(&mut doc)?; + + meta.write_out(); - for scen in doc.scenarios()? { - println!("scenario {}", scen.title()); - } Ok(()) } -- cgit v1.2.1