diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-10-19 19:30:37 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-11-19 20:18:50 +0000 |
commit | 5a92266170ae9879b0651e6074e538a4de0a214c (patch) | |
tree | d5ecb647701534d6374f441f2c790bfdd2dca848 /src/metadata.rs | |
parent | 572d3097770cd8e5cd22d7767c1d18e0d50b9a90 (diff) | |
download | subplot-5a92266170ae9879b0651e6074e538a4de0a214c.tar.gz |
various: Rework document to support multiple implementations
In order to eventually shift the document metadata to support
more than one template defined for the document this reworks
all the internal APIs to expect templates, and also the external
CLI to be able to provide it.
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/metadata.rs')
-rw-r--r-- | src/metadata.rs | 61 |
1 files changed, 46 insertions, 15 deletions
diff --git a/src/metadata.rs b/src/metadata.rs index 0ee0b1a..03d4eff 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -1,6 +1,7 @@ use crate::Result; use crate::{Bindings, TemplateSpec}; +use std::collections::HashMap; use std::fmt::Debug; use std::ops::Deref; use std::path::{Path, PathBuf}; @@ -16,14 +17,18 @@ pub struct Metadata { date: Option<String>, bindings_filenames: Vec<PathBuf>, bindings: Bindings, - functions_filenames: Vec<PathBuf>, - template: Option<String>, - spec: Option<TemplateSpec>, + impls: HashMap<String, DocumentImpl>, bibliographies: Vec<PathBuf>, /// Extra class names which should be considered 'correct' for this document classes: Vec<String>, } +#[derive(Debug)] +pub struct DocumentImpl { + spec: TemplateSpec, + functions: Vec<PathBuf>, +} + impl Metadata { /// Construct a Metadata from a Document, if possible. #[instrument(level = "trace", skip(doc))] @@ -57,14 +62,22 @@ impl Metadata { get_bindings(&bindings_filenames, &mut bindings, template.as_deref())?; event!(Level::TRACE, "Loaded all metadata successfully"); + let mut impls = HashMap::new(); + + if let (Some(template), Some(spec)) = (template, spec) { + let mut docimpl = DocumentImpl::new(spec); + for fname in functions_filenames { + docimpl.add_function_file(fname); + } + impls.insert(template, docimpl); + } + Ok(Metadata { title, date, bindings_filenames, bindings, - functions_filenames, - template, - spec, + impls, bibliographies, classes, }) @@ -85,17 +98,14 @@ impl Metadata { self.bindings_filenames.iter().map(|f| f.as_ref()).collect() } - /// Return filename where functions are specified. - pub fn functions_filenames(&self) -> Vec<&Path> { - self.functions_filenames - .iter() - .map(|f| f.as_ref()) - .collect() + /// Return the document implementation (filenames, spec, etc) for the given template name + pub fn document_impl(&self, template: &str) -> Option<&DocumentImpl> { + self.impls.get(template) } - /// Return the name of the code template, if specified. - pub fn template_name(&self) -> Option<&str> { - self.template.as_deref() + /// Return the templates the document expects to implement + pub fn templates(&self) -> impl Iterator<Item = &str> { + self.impls.keys().map(String::as_str) } /// Return the bindings. @@ -114,6 +124,27 @@ impl Metadata { } } +impl DocumentImpl { + fn new(spec: TemplateSpec) -> Self { + Self { + spec, + functions: Vec::new(), + } + } + + fn add_function_file(&mut self, function: PathBuf) { + self.functions.push(function); + } + + pub fn functions_filenames(&self) -> impl Iterator<Item = &Path> { + self.functions.iter().map(PathBuf::as_path) + } + + pub fn spec(&self) -> &TemplateSpec { + &self.spec + } +} + type Mapp = Map<String, MetaValue>; fn get_title(map: &Mapp) -> String { |