use crate::error::AndaxRes;
use rhai::{
plugin::{
export_module, mem, FnNamespace, ImmutableString, Module, NativeCallContext, PluginFunc,
RhaiResult, TypeId,
},
Dynamic, EvalAltResult as RhaiE, FuncRegistration,
};
type Res<T = ()> = Result<T, Box<RhaiE>>;
#[export_module]
pub mod ar {
use hcl::eval::Evaluate;
use std::io::Read;
use tracing::{debug, trace};
#[rhai_fn(return_raw, global)]
pub fn template(ctx: NativeCallContext, tmpl: rhai::Map, input: String) -> Res<String> {
let mut hcl = anda_config::context::hcl_context();
for (k, v) in tmpl {
let key = k.to_string();
let value = hcl::value::to_value(v).ehdl(&ctx)?;
let span = tracing::debug_span!("hcl.declare_var", ?key, ?value);
span.in_scope(|| {
hcl.declare_var(key, value);
});
}
println!("{:?}", ctx.source());
let template =
<hcl::template::Template as std::str::FromStr>::from_str(&input).ehdl(&ctx)?;
let res = template.evaluate(&hcl).ehdl(&ctx)?;
let res = res.replace("@{", "%{");
trace!(?res, "Template Result");
Ok(res)
}
#[rhai_fn(return_raw, global)]
pub fn template_file(ctx: NativeCallContext, map: rhai::Map, path: String) -> Res<String> {
let mut file = std::fs::File::open(&path).ehdl(&ctx)?;
let mut buf = String::new();
file.read_to_string(&mut buf).ehdl(&ctx)?;
debug!("Templating file: {:#?}", path);
debug!(?map, "Loading Template");
template(ctx, map, buf)
}
#[rhai_fn(return_raw, global)]
pub fn to_json(ctx: NativeCallContext, map: rhai::Map) -> Res<String> {
let json = serde_json::to_string(&map).ehdl(&ctx)?;
Ok(json)
}
#[rhai_fn(return_raw, global)]
pub fn from_json(ctx: NativeCallContext, json: String) -> Res<rhai::Map> {
let map = serde_json::from_str(&json).ehdl(&ctx)?;
Ok(map)
}
}