Skip to content

Commit

Permalink
Update j2 engine and better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lumasepa committed May 13, 2020
1 parent 479e3c5 commit e4798e3
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 196 deletions.
353 changes: 227 additions & 126 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ readme = "README.md"
license = "MIT"

[dependencies]
tera = "0.11.20"
serde = "1.0.99"
tera = "1"
serde = "1"
toml = "0.5.3"
serde_json = "1.0.40"
serde_json = "1"
serde_yaml = "0.8.9"
glob = "0.3.0"
base64 = "0.10.1"
molysite = { git = "https://github.com/evq/molysite" }
anyhow = "1.0.29"

[[bin]]
name = "j2_render"
Expand Down
2 changes: 1 addition & 1 deletion examples/ctx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"my_float": 1.2,
"my_array": [ "this is", "a list of", "three strings"],
"my_obj": {"a": "a", "b": "b"},
"comple_obj": {
"complex_obj": {
"1": 1,
"obj": {
"a": "a"
Expand Down
28 changes: 15 additions & 13 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::fs;
use std::path::Path;
use std::process::Command;
use tera::{Result, Value};
use anyhow::Context;

pub fn bash(piped_arg: Value, args: HashMap<String, Value>) -> Result<Value> {
pub fn bash(piped_arg: &Value, args: &HashMap<String, Value>) -> Result<Value> {
let data = if let Value::String(data) = piped_arg {
data
} else {
Expand All @@ -30,7 +31,7 @@ pub fn bash(piped_arg: Value, args: HashMap<String, Value>) -> Result<Value> {
return exec_cmd(&mut bash_cmd, command, &args);
}

pub fn sed(piped_arg: Value, args: HashMap<String, Value>) -> Result<Value> {
pub fn sed(piped_arg: &Value, args: &HashMap<String, Value>) -> Result<Value> {
let data = if let Value::String(data) = piped_arg {
data
} else {
Expand All @@ -53,7 +54,7 @@ pub fn sed(piped_arg: Value, args: HashMap<String, Value>) -> Result<Value> {
return exec_cmd(&mut bash_cmd, &command, &args);
}

pub fn file_glob(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
pub fn file_glob(piped_arg: &Value, _: &HashMap<String, Value>) -> Result<Value> {
let mut files_matched = vec![];

if let Value::String(path) = piped_arg {
Expand All @@ -73,7 +74,7 @@ pub fn file_glob(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
}
}

pub fn read_file(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
pub fn read_file(piped_arg: &Value, _: &HashMap<String, Value>) -> Result<Value> {
if let Value::String(path) = piped_arg {
match fs::read_to_string(path) {
Ok(contents) => return Ok(Value::String(contents)),
Expand All @@ -84,7 +85,7 @@ pub fn read_file(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
}
}

pub fn file_name(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
pub fn file_name(piped_arg: &Value, _: &HashMap<String, Value>) -> Result<Value> {
if let Value::String(path) = piped_arg {
let path = Path::new(&path);
let file_name = match path.file_name() {
Expand All @@ -101,7 +102,7 @@ pub fn file_name(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
}
}

pub fn file_dir(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
pub fn file_dir(piped_arg: &Value, _: &HashMap<String, Value>) -> Result<Value> {
if let Value::String(path) = piped_arg {
let path = Path::new(&path);
let file_name = match path.parent() {
Expand All @@ -118,15 +119,15 @@ pub fn file_dir(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
}
}

pub fn strip_line_breaks(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
pub fn strip_line_breaks(piped_arg: &Value, _: &HashMap<String, Value>) -> Result<Value> {
if let Value::String(lines) = piped_arg {
return Ok(Value::String(lines.replace("\n", "")));
} else {
return Err("strip_line_breaks: Invalid type, expected string".into());
}
}

pub fn remove_extension(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
pub fn remove_extension(piped_arg: &Value, _: &HashMap<String, Value>) -> Result<Value> {
if let Value::String(filename) = piped_arg {
let mut parts: Vec<_> = filename.split('.').collect();
parts.pop();
Expand All @@ -137,7 +138,7 @@ pub fn remove_extension(piped_arg: Value, _: HashMap<String, Value>) -> Result<V
}
}

pub fn b64encode(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
pub fn b64encode(piped_arg: &Value, _: &HashMap<String, Value>) -> Result<Value> {
if let Value::String(data) = piped_arg {
let encoded_data = base64::encode(&data);
return Ok(Value::String(encoded_data));
Expand All @@ -146,7 +147,7 @@ pub fn b64encode(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
}
}

pub fn b64decode(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
pub fn b64decode(piped_arg: &Value, _: &HashMap<String, Value>) -> Result<Value> {
if let Value::String(data) = piped_arg {
return base64::decode(&data)
.map_err(|e| format!("b64decode: decoding error : {}", e).into())
Expand All @@ -160,15 +161,16 @@ pub fn b64decode(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
}
}

pub fn str(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
pub fn str(piped_arg: &Value, _: &HashMap<String, Value>) -> Result<Value> {
return Ok(Value::String(piped_arg.to_string()));
}

pub fn from_json(piped_arg: Value, _: HashMap<String, Value>) -> Result<Value> {
pub fn from_json(piped_arg: &Value, _: &HashMap<String, Value>) -> Result<Value> {
if let Value::String(data) = piped_arg {
let value = data
.parse::<serde_json::Value>()
.expect(&format!("from_json: error parsing json : {}", data));
.context(format!("from_json: error parsing json : {}", data))
.map_err(|e| e.to_string())?;
return Ok(value);
} else {
return Err("from_json: Invalid type, expected string".into());
Expand Down
16 changes: 9 additions & 7 deletions src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::inners::exec_cmd;
use std::collections::HashMap;
use std::process::Command;
use tera::{Error, Result, Value};
use anyhow::Context;

pub fn bash(args: HashMap<String, Value>) -> Result<Value> {
pub fn bash(args: &HashMap<String, Value>) -> Result<Value> {
let command = if let Some(Value::String(command)) = args.get("command") {
command
} else {
Expand All @@ -16,7 +17,7 @@ pub fn bash(args: HashMap<String, Value>) -> Result<Value> {
return exec_cmd(&mut bash_cmd, command, &args);
}

pub fn tab_all_lines(args: HashMap<String, Value>) -> Result<Value> {
pub fn tab_all_lines(args: &HashMap<String, Value>) -> Result<Value> {
if let Some(Value::String(lines)) = args.get("lines") {
if let Some(Value::Number(num_spaces)) = args.get("num_spaces") {
let num_spaces = num_spaces.as_u64().ok_or(Error::from(
Expand All @@ -33,19 +34,20 @@ pub fn tab_all_lines(args: HashMap<String, Value>) -> Result<Value> {
}
}

pub fn str(args: HashMap<String, Value>) -> Result<Value> {
pub fn str(args: &HashMap<String, Value>) -> Result<Value> {
return Ok(Value::String(
args.get("value").expect("str: expected value argument").to_string(),
args.get("value").ok_or("str: expected value argument".to_owned())?.to_string(),
));
}

pub fn from_json(args: HashMap<String, Value>) -> Result<Value> {
pub fn from_json(args: &HashMap<String, Value>) -> Result<Value> {
let value = args
.get("value")
.expect("from_json: expected value argument")
.ok_or("from_json: expected value argument".to_owned())?
.to_string();
let value = value
.parse::<serde_json::Value>()
.expect(&format!("from_json: error parsing json : {}", value));
.context(format!("from_json: error parsing json : {}", value))
.map_err(|e| e.to_string())?;
return Ok(value);
}
12 changes: 9 additions & 3 deletions src/inners.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::process::Command;
use tera::{Result, Value};
use anyhow::Context;

pub fn exec_cmd(command: &mut Command, cmd_str: &str, env: &HashMap<String, Value>) -> Result<Value> {
for (k, v) in env.iter() {
Expand All @@ -13,10 +14,15 @@ pub fn exec_cmd(command: &mut Command, cmd_str: &str, env: &HashMap<String, Valu
}
let out = command
.output()
.expect(&format!("Error executing command : {}", cmd_str));
.context(format!("Error executing command : {}", cmd_str))
.map_err(|e| e.to_string())?;

let stdout = String::from_utf8(out.stdout).expect(&format!("bash: Error reading stdout of command {}", cmd_str));
let stderr = String::from_utf8(out.stderr).expect(&format!("bash: Error reading stderr of command {}", cmd_str));
let stdout = String::from_utf8(out.stdout)
.context(format!("bash: Error reading stdout of command {}", cmd_str))
.map_err(|e| e.to_string())?;
let stderr = String::from_utf8(out.stderr)
.context(format!("bash: Error reading stderr of command {}", cmd_str))
.map_err(|e| e.to_string())?;
if stderr != "" {
eprintln!("command {} stderr : {}", cmd_str, stderr);
}
Expand Down
Loading

0 comments on commit e4798e3

Please sign in to comment.