diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-06-07 10:55:19 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-06-07 10:55:19 +0300 |
commit | 86a9f568c410bed2f4503a2e8d43e2dc2a0d70b8 (patch) | |
tree | ad676ee9e353131e7d0a04ef07c3ce83cfe8cb95 | |
parent | bba22e1f86967feebf94ebac327f7489c1029afd (diff) | |
download | subplot-86a9f568c410bed2f4503a2e8d43e2dc2a0d70b8.tar.gz |
feat: supply source file name for functions files
The Python and Bash templates now insert the name of the source files
from where the function codes come from.
-rw-r--r-- | src/codegen.rs | 22 | ||||
-rw-r--r-- | templates/bash/template.sh | 7 | ||||
-rw-r--r-- | templates/python/template.py | 7 |
3 files changed, 31 insertions, 5 deletions
diff --git a/src/codegen.rs b/src/codegen.rs index 9895783..c1da2f8 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -2,10 +2,11 @@ use crate::{Document, SubplotError, TemplateSpec}; use std::collections::HashMap; use std::fs::File; use std::io::prelude::{Read, Write}; -use std::path::Path; +use std::path::{Path, PathBuf}; use base64::encode; +use serde::Serialize; use tera::{Context, Tera, Value}; use anyhow::Result; @@ -42,11 +43,11 @@ fn context(doc: &mut Document) -> Result<Context> { context.insert("files", doc.files()); let funcs_filenames = doc.meta().functions_filenames(); - let mut funcs = String::new(); + let mut funcs = vec![]; for filename in funcs_filenames { let content = cat(filename).map_err(|e| SubplotError::FunctionsFileNotFound(filename.into(), e))?; - funcs.push_str(&content); + funcs.push(Func::new(filename, content)); } context.insert("functions", &funcs); @@ -83,3 +84,18 @@ fn base64(v: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> { )), } } + +#[derive(Debug, Serialize)] +pub struct Func { + pub source: PathBuf, + pub code: String, +} + +impl Func { + pub fn new(source: &Path, code: String) -> Func { + Func { + source: source.to_path_buf(), + code, + } + } +} diff --git a/templates/bash/template.sh b/templates/bash/template.sh index 89958d4..1da4f5b 100644 --- a/templates/bash/template.sh +++ b/templates/bash/template.sh @@ -5,7 +5,12 @@ set -eu -o pipefail ############################################################################# # Functions that implement steps. -{{ functions }} +{% for func in functions %} +#---------------------------------------------------------------------------- +# This code comes from: {{ func.source }} + +{{ func.code }} +{% endfor %} ############################################################################# diff --git a/templates/python/template.py b/templates/python/template.py index 4ceeb51..af5b36e 100644 --- a/templates/python/template.py +++ b/templates/python/template.py @@ -1,7 +1,12 @@ ############################################################################# # Functions that implement steps. -{{ functions }} +{% for func in functions %} +#---------------------------------------------------------------------------- +# This code comes from: {{ func.source }} + +{{ func.code }} +{% endfor %} ############################################################################# |