summaryrefslogtreecommitdiff
path: root/src/codegen.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-06-07 10:55:19 +0300
committerLars Wirzenius <liw@liw.fi>2020-06-07 10:55:19 +0300
commit86a9f568c410bed2f4503a2e8d43e2dc2a0d70b8 (patch)
treead676ee9e353131e7d0a04ef07c3ce83cfe8cb95 /src/codegen.rs
parentbba22e1f86967feebf94ebac327f7489c1029afd (diff)
downloadsubplot-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.
Diffstat (limited to 'src/codegen.rs')
-rw-r--r--src/codegen.rs22
1 files changed, 19 insertions, 3 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,
+ }
+ }
+}