summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2023-06-21 19:02:49 +0000
committerDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2023-06-21 19:02:49 +0000
commite5e0a4a3cb035bf0bace9487a182478f4fff20d0 (patch)
tree6c0e12feeacc6795726ee585625311c95b07d776
parent39601b16c513fe3f1f60cb260fae2e046f7eadaf (diff)
parent4694b6e7dcf29a01b47eee4a782e5b585cd80c39 (diff)
downloadsubplot-e5e0a4a3cb035bf0bace9487a182478f4fff20d0.tar.gz
Merge branch 'liw/refactor-doc' into 'main'
tidy up the Document type a little See merge request subplot/subplot!343
-rw-r--r--src/bin/cli/mod.rs4
-rw-r--r--src/bin/subplot.rs2
-rw-r--r--src/codegen.rs2
-rw-r--r--src/doc.rs36
4 files changed, 16 insertions, 28 deletions
diff --git a/src/bin/cli/mod.rs b/src/bin/cli/mod.rs
index dacf9b6..19e4fbf 100644
--- a/src/bin/cli/mod.rs
+++ b/src/bin/cli/mod.rs
@@ -13,7 +13,7 @@ use std::{collections::HashMap, convert::TryFrom};
use subplot::{Document, EmbeddedFile, Style, SubplotError};
pub fn extract_file<'a>(doc: &'a Document, filename: &str) -> Result<&'a EmbeddedFile> {
- for file in doc.files() {
+ for file in doc.embedded_files() {
if file.filename() == filename {
return Ok(file);
}
@@ -78,7 +78,7 @@ impl TryFrom<&mut Document> for Metadata {
.collect();
scenarios.sort_unstable();
let mut files: Vec<_> = doc
- .files()
+ .embedded_files()
.iter()
.map(|f| f.filename().to_owned())
.collect();
diff --git a/src/bin/subplot.rs b/src/bin/subplot.rs
index ced34cc..6c118d4 100644
--- a/src/bin/subplot.rs
+++ b/src/bin/subplot.rs
@@ -159,7 +159,7 @@ impl Extract {
let doc = load_linted_doc(&self.filename, Style::default(), None, self.merciful)?;
let files: Vec<&EmbeddedFile> = if self.embedded.is_empty() {
- doc.files()
+ doc.embedded_files()
.iter()
.map(Result::Ok)
.collect::<Result<Vec<_>>>()
diff --git a/src/codegen.rs b/src/codegen.rs
index 81a5f35..41b5556 100644
--- a/src/codegen.rs
+++ b/src/codegen.rs
@@ -32,7 +32,7 @@ fn context(doc: &mut Document, template: &str) -> Result<Context, SubplotError>
let mut context = Context::new();
let scenarios = doc.matched_scenarios(template)?;
context.insert("scenarios", &scenarios);
- context.insert("files", doc.files());
+ context.insert("files", doc.embedded_files());
let mut funcs = vec![];
if let Some(docimpl) = doc.meta().document_impl(template) {
diff --git a/src/doc.rs b/src/doc.rs
index 6fbf497..d417e1d 100644
--- a/src/doc.rs
+++ b/src/doc.rs
@@ -95,25 +95,6 @@ impl Document {
doc
}
- fn from_ast<P>(
- basedir: P,
- subplot: PathBuf,
- yamlmeta: &YamlMetadata,
- markdowns: Vec<Markdown>,
- style: Style,
- template: Option<&str>,
- ) -> Result<Document, SubplotError>
- where
- P: AsRef<Path> + Debug,
- {
- let meta = Metadata::from_yaml_metadata(basedir, yamlmeta, template)?;
- trace!("metadata from YAML: {:#?}", meta);
- let files = Self::all_files(&markdowns)?;
- let doc = Document::new(subplot, markdowns, meta, files, style);
- trace!("Loaded from JSON OK");
- Ok(doc)
- }
-
fn all_files(markdowns: &[Markdown]) -> Result<EmbeddedFiles, SubplotError> {
let mut files = EmbeddedFiles::default();
for md in markdowns {
@@ -146,7 +127,10 @@ impl Document {
markdowns.push(Markdown::load_file(&filename)?);
}
- let doc = Self::from_ast(basedir, filename.into(), &meta, markdowns, style, template)?;
+ let meta = Metadata::from_yaml_metadata(basedir, &meta, template)?;
+ trace!("metadata from YAML: {:#?}", meta);
+ let files = Self::all_files(&markdowns)?;
+ let doc = Document::new(filename.into(), markdowns, meta, files, style);
trace!("Loaded document OK");
Ok(doc)
@@ -261,7 +245,7 @@ impl Document {
}
/// Return list of files embeddedin the document.
- pub fn files(&self) -> &[EmbeddedFile] {
+ pub fn embedded_files(&self) -> &[EmbeddedFile] {
self.files.files()
}
@@ -291,7 +275,11 @@ impl Document {
// Check that all filenames for embedded files are unique.
fn check_filenames_are_unique(&self) -> Result<(), SubplotError> {
let mut known = HashSet::new();
- for filename in self.files().iter().map(|f| f.filename().to_lowercase()) {
+ for filename in self
+ .embedded_files()
+ .iter()
+ .map(|f| f.filename().to_lowercase())
+ {
if known.contains(&filename) {
return Err(SubplotError::DuplicateEmbeddedFilename(filename));
}
@@ -383,7 +371,7 @@ impl Document {
warnings: &mut Warnings,
) -> Result<bool, SubplotError> {
let filenames: HashSet<_> = self
- .files()
+ .embedded_files()
.iter()
.map(|f| f.filename().to_lowercase())
.collect();
@@ -420,7 +408,7 @@ impl Document {
warnings: &mut Warnings,
) -> Result<bool, SubplotError> {
let mut filenames: HashSet<_> = self
- .files()
+ .embedded_files()
.iter()
.map(|f| f.filename().to_lowercase())
.collect();