From 86a9f568c410bed2f4503a2e8d43e2dc2a0d70b8 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 7 Jun 2020 10:55:19 +0300 Subject: 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. --- src/codegen.rs | 22 +++++++++++++++++++--- templates/bash/template.sh | 7 ++++++- 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.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) -> tera::Result { )), } } + +#[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 %} ############################################################################# -- cgit v1.2.1