diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-05-21 17:18:54 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-05-21 17:18:54 +0100 |
commit | 6959f556ccfa0ccfd409d36bfa4d40e004de0040 (patch) | |
tree | a72c43db9ad9ae2e76246cea8035facc3a0b643f | |
parent | 576aa3bcbb6615f4fef73264029fc22af78fdfdc (diff) | |
download | subplot-6959f556ccfa0ccfd409d36bfa4d40e004de0040.tar.gz |
refactor: make sp-meta structure based ready for json
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
-rw-r--r-- | src/bin/sp-meta.rs | 87 |
1 files 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<String>, + title: String, + binding_files: Vec<String>, + function_files: Vec<String>, + bibliographies: Vec<String>, + scenarios: Vec<String>, +} - 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<Self, Self::Error> { + 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(()) } |