diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-03-27 13:15:15 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-04-09 16:42:49 +0100 |
commit | f49c399f1dd8c391c756776b31f13cd5473bc885 (patch) | |
tree | 26dedd6727f03dadc82bf94de8c992874cc78d9e | |
parent | 316b90d5c645702336c08d1f5e7d716f993ca6a6 (diff) | |
download | subplot-f49c399f1dd8c391c756776b31f13cd5473bc885.tar.gz |
bin: Abstract metadata handling
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
-rw-r--r-- | src/bin/cli/mod.rs | 108 | ||||
-rw-r--r-- | src/bin/sp-meta.rs | 111 |
2 files changed, 111 insertions, 108 deletions
diff --git a/src/bin/cli/mod.rs b/src/bin/cli/mod.rs index 855a066..f3225db 100644 --- a/src/bin/cli/mod.rs +++ b/src/bin/cli/mod.rs @@ -3,9 +3,12 @@ #![allow(unused)] use anyhow::Result; +use serde::Serialize; use subplot::{DataFile, Document, Style, SubplotError}; +use std::convert::TryFrom; use std::path::Path; +use std::str::FromStr; pub fn load_document<P: AsRef<Path>>(filename: P, style: Style) -> Result<Document> { let filename = filename.as_ref(); @@ -23,3 +26,108 @@ pub fn extract_file<'a>(doc: &'a Document, filename: &str) -> Result<&'a DataFil } Err(SubplotError::EmbeddedFileNotFound(filename.to_owned()).into()) } + +#[derive(Serialize)] +pub struct Metadata { + sources: Vec<String>, + title: String, + binding_files: Vec<String>, + function_files: Vec<String>, + bibliographies: Vec<String>, + scenarios: Vec<String>, + files: Vec<String>, +} + +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(); + let files = doc + .files() + .iter() + .map(|f| f.filename().to_owned()) + .collect(); + Ok(Self { + sources, + title, + binding_files, + function_files, + bibliographies, + scenarios, + files, + }) + } +} + +impl Metadata { + fn write_list(v: &[String], prefix: &str) { + v.iter().for_each(|entry| println!("{}: {}", prefix, entry)) + } + + pub 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.files, "file"); + Self::write_list(&self.scenarios, "scenario"); + } +} + +fn filename(name: Option<&Path>) -> String { + let path = match name { + None => return "".to_string(), + Some(x) => x, + }; + match path.to_str() { + None => "non-UTF8 filename".to_string(), + Some(x) => x.to_string(), + } +} + +#[derive(Debug)] +pub enum OutputFormat { + Plain, + Json, +} + +impl FromStr for OutputFormat { + type Err = String; + + fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { + match s.to_ascii_lowercase().as_ref() { + "plain" => Ok(OutputFormat::Plain), + "json" => Ok(OutputFormat::Json), + _ => Err(format!("Unknown output format: `{}`", s)), + } + } +} diff --git a/src/bin/sp-meta.rs b/src/bin/sp-meta.rs index 33e6ca4..b907999 100644 --- a/src/bin/sp-meta.rs +++ b/src/bin/sp-meta.rs @@ -1,32 +1,14 @@ use anyhow::Result; use std::convert::TryFrom; -use std::path::{Path, PathBuf}; -use std::str::FromStr; +use std::path::PathBuf; -use serde::Serialize; use structopt::StructOpt; -use subplot::{Document, Style}; +use subplot::Style; mod cli; -#[derive(Debug)] -enum OutputFormat { - Plain, - Json, -} - -impl FromStr for OutputFormat { - type Err = String; - - fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { - match s.to_ascii_lowercase().as_ref() { - "plain" => Ok(OutputFormat::Plain), - "json" => Ok(OutputFormat::Json), - _ => Err(format!("Unknown output format: `{}`", s)), - } - } -} +use cli::{Metadata, OutputFormat}; #[derive(Debug, StructOpt)] #[structopt(name = "sp-meta", about = "Show Subplot document metadata.")] @@ -39,82 +21,6 @@ struct Opt { filename: PathBuf, } -#[derive(Serialize)] -struct Metadata { - sources: Vec<String>, - title: String, - binding_files: Vec<String>, - function_files: Vec<String>, - bibliographies: Vec<String>, - scenarios: Vec<String>, - files: Vec<String>, -} - -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(); - let files = doc - .files() - .iter() - .map(|f| f.filename().to_owned()) - .collect(); - Ok(Self { - sources, - title, - binding_files, - function_files, - bibliographies, - scenarios, - files, - }) - } -} - -impl Metadata { - fn write_list(v: &[String], prefix: &str) { - v.iter().for_each(|entry| println!("{}: {}", prefix, entry)) - } - - 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.files, "file"); - Self::write_list(&self.scenarios, "scenario"); - } -} - fn main() -> Result<()> { let opt = Opt::from_args(); let mut doc = cli::load_document(&opt.filename, Style::default())?; @@ -127,14 +33,3 @@ fn main() -> Result<()> { Ok(()) } - -fn filename(name: Option<&Path>) -> String { - let path = match name { - None => return "".to_string(), - Some(x) => x, - }; - match path.to_str() { - None => "non-UTF8 filename".to_string(), - Some(x) => x.to_string(), - } -} |